n5ro/aframe-physics-system

version 4.0.1 dynamic bodies don't inherit position from parent

Interactimation opened this issue ยท 2 comments

In version 4.0.1 of the A-Frame Physics Engine, static bodies DO but dynamic bodies DO NO inherit position information from parent entities

EXAMPLE: https://glitch.com/~aps-401-parent-bug
the boxes and spheres are all children of a "control entity" with a
position="0 0 -25"
static-body boxes therefore have a position z=0
BUT
dynamic-body spheres have to have their position z=-25 to match

I know that this was not an issue a year ago, today - what happened?

@Interactimation thanks so much for raising this issue. I've not been able to reproduce the behaviour you described where dynamic bodies did inherit position information from parent entities. I have tried this with v3.0.0

https://unpkg.com/aframe-physics-system@3.0.0/dist/aframe-physics-system.min.js

which was published over a year ago and it still has the same behaviour where dynamic bodies no not inherit position information. If you can, could I please ask you to provide an example of the previous behaviour you said existed a year ago, thanks ๐Ÿ˜Š

In general though I agree that irrespective of the previous behaviour, this does look like a bug. Static bodies and dynamic bodies should inherit consistently. I'm looking into this now and will update this issue shortly, thanks again for raising this ๐Ÿ˜Š

I have a candidate solution for this issue. If we take a look at how physics bodies are initialised

var obj = this.el.object3D;
var pos = obj.position;
var quat = obj.quaternion;
this.body = new CANNON.Body({
mass: data.type === 'static' ? 0 : data.mass || 0,
material: this.system.getMaterial('defaultMaterial'),
position: new CANNON.Vec3(pos.x, pos.y, pos.z),
quaternion: new CANNON.Quaternion(quat.x, quat.y, quat.z, quat.w),
linearDamping: data.linearDamping,
angularDamping: data.angularDamping,
type: data.type === 'dynamic' ? CANNON.Body.DYNAMIC : CANNON.Body.STATIC,
});

we see that they simply take on the local position and rotation. It seems for static bodies the particular path through the code in src/components/body/body.js ends up correcting this issue, so you end up with the static body in the correct place inheriting from the parent. However for dynamic bodies, the path is slightly different and it ends up maintaining this initialised position which does not inherit from the parent.

I have a simple candidate fix using updateWorldMatrix that resolves this issue for me locally and is similar to how the position and rotation of the body is normally (and correctly) synced to the physics engine

el.object3D.getWorldQuaternion(q);
body.quaternion.copy(q);
el.object3D.getWorldPosition(v);
body.position.copy(v);

I need to write up a test or two for this to prevent future regressions, but this should be on master in the next 1-2 days and a published version shortly after that ๐Ÿ˜Š I'll update with progress as it comes.