I have a map (a collection of gltf meshes that all belong to the same parent root node) that gets loaded and a mesh for the players character. I am able to move the player mesh around as expected, and it seems to be working nicely with the ammo js physics engine / plugin.
The meshes of the map however, are an entirely different matter.
I’ve noted that each of the following map mesh position values are different:
- mesh.position
- mesh.physicsImposter.getObjectCenter()
- mesh.physicsImposter.physicsBody position using this code:
export const getPositionFromPhysicsBody = (physicsBody: PhysicsBody, Ammo) => {
tmpTrans = new Ammo.btTransform()
const motionState = physicsBody.getMotionState()
motionState.getWorldTransform(tmpTrans)
return tmpTrans.getOrigin()
}
Further still, using mesh.physicsImposter.setDeltaPosition(new Vector3(0, 10, 0))
, or any xyz valued vector, has no affect on any of the aforementioned positional values.
The map meshes all have their physics imposter mass set to zero, and are static bodies. I don’t use any physics root on any of the map meshes, and I set them up like so:
export const convertToImposter = (
{
meshes,
scene,
mass,
isGround = false
},
imposterType = 'BoxImpostor',
) => {
const rigidBodies: Array<PhysicsBody> = []
const staticBodies: Array<PhysicsBody> = []
let rootNodeMass
if (isGround) {
rootNodeMass = 0
} else if (mass !== undefined) {
rootNodeMass = mass
} else {
rootNodeMass = 3.0
}
const childMass = isGround ? 0 : 0.1
let mesh:Mesh
for (let i = 0; i < meshes.length; i++) {
mesh = meshes[i] || new Mesh('appease-typescript')
if (mesh.id === '__root__') {
continue
}
//@ts-ignore
const parent = mesh.parent
//@ts-ignore
mesh.parent = null
mesh.physicsImpostor = new PhysicsImpostor(
mesh,
PhysicsImpostor[imposterType],
{ mass: childMass },
scene
)
const {x, y, z } = mesh.physicsImpostor.getObjectCenter()
mesh.physicsImpostor.setDeltaPosition(new Vector3(x, y, z))
if (isGround === false) {
rigidBodies.push(mesh.physicsImpostor)
} else {
staticBodies.push(mesh.physicsImpostor)
mesh.position.x = x
mesh.position.y = y
mesh.position.z = z
}
mesh.parent = parent
}
return { rigidBodies, staticBodies }
}
The last puzzling thing I noticed, is that if I scale the map meshes by some scalar value, say 10, then the physics bodies have their position coordinates multiplied by -10???
I’m pretty confused, and would appreciate any help at this point.