n5ro / aframe-physics-system

Physics system for A-Frame VR, built on CANNON.js.

Home Page:https://n5ro.github.io/aframe-physics-system/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ammo-shape offset and halfExtents seemingly not updating even after setting values

msub2 opened this issue · comments

I'm currently attempting to make an ammo-shape follow the location of a player's head to properly position/adjust a capsule body collider for VR. When I set the values of the offset or halfExtents it doesn't affect the collider at all, which I can see with debug draw on. But if I log the values of either, they come back with the values I expect.

Relevant code:

let headPos = this.head.object3D.position;
let ammoShape = this.player.components['ammo-shape'].data;
ammoShape.offset.x = headPos.x;
ammoShape.offset.y = headPos.y / 2;
ammoShape.offset.z = headPos.z;
ammoShape.halfExtents.y = headPos.y / 2;

Full demo here: https://glitch.com/~aframe-vr-character-controller I'm at a bit of a loss on what could be causing this. If I'm somehow misunderstanding something or more information is needed, just let me know.

Okay I don't know the answer (yet) but my first instinct would be to try using a constraint like the "target" property to point the ammo-shape at the camera or player's head. That constraint or property seems to create a visual binding between two entities, which you can see in the Aframe Physics Examples.

Revisiting this one after a little bit. I tried using a constraint but it had no visible effect, and I'm not sure it would have worked to begin with given that the head is a child object of the body. All I've been able to determine is that the offsets/half-extents seem to update exactly once, and no matter what I try to modify the debug drawer shows that there's been no change. I even tried accessing the localTransform of the collisionShape and setting the origin from there, but still nothing.

EDIT:
I think I've figured out what the problem is. I don't think you're able to update the btCollisionShape you get from your three-to-ammo conversion during runtime; it only sets the offset/halfExtents when it's initialized. I tested this by removing and readding the ammo-shape component with updated values every tick, and the collider is finally behaving as expected. Code for reference:

this.player.removeAttribute('ammo-shape');
this.headPos.setFromMatrixPosition(this.head.object3D.matrixWorld);
this.player.setAttribute('ammo-shape', {
    type: 'capsule',
    fit: 'manual',
    halfExtents: `.15 ${this.headPos.y / 2} .15`,
    offset: `${this.headPos.x - this.player.object3D.position.x} ${this.headPos.y / 2} ${this.headPos.z - this.player.object3D.position.z}`
});

Updated Glitch project showing proper functionality: https://glitch.com/~aframe-physical-character-controller