PhysicsBody controlled by animation

Hello,

I am a bit confused about the setup for character animation.

I created basic low poly character in blender, together with armature. Rigged it and created few animations. For the sake of collision detection, I created additional meshes, all of them boxes, to use them as colliders. All of these colliders are attached to the same skeleton that the rest of the character is. Then exported this setup to .glb and imported to babylon scene.

I can successfully play the animations and I see both the character model and colliders move accordingly.

The question I have, is how do I now integrate both the animation and physics?
From what I understand, I should now make each one of collider meshes a separate PhysicsBody with its own shape. I do that in following way:

colliderMeshes.forEach(mesh => {
        const body = new PhysicsBody(mesh, PhysicsMotionType.ANIMATED, false, scene)
        const shape = new PhysicsShapeConvexHull(mesh as Mesh, scene)
        body.shape = shape
})

This unfortunately results in none of the collider meshes being affected by skeleton animation anymore. I think it is because they are now controlled by physics, not by the skeleton animation.

How do I then make meshes that are both controlled by the animation AND can interact with other physics objects in the scene? If I am right, this is what PhysicsMotionType.ANIMATED is meant for.

Have you tried it

body.disablePreStep = false;

By default, updating the position of a physics body from a Transform Node is disabled because it can be a waste of performance if it is not an animated rigid body.

2 Likes

It seems to indeed return the control of the PhysicsBody to the bone it is attached to. Thanks for that. However, what I noticed is, even though these meshes now follow the position of the bone, they stay a bit behind. Like they were few frames of animation behind. Do you know what could be causing that?

Also I’m a bit scared about the note that this decreases performance. Is there any other, more performant way, of achieving the effect I need?

You don’t have to worry about the performance It’s just a operation that’s necessary for positioning the physics body from the transform node

I described it as a waste of performance because, generally the physics body manipulates the transform node.

And the problem that “body follows one frame later” you mentioned is probably caused by the delay in updating the World Matrix

For resolve this issue try call the computeWorldMatrix before the physics step to all the nodes with the physics body attached

scene.onBeforePhysicsObservable.add(() => {
    colliderMeshes.forEach(mesh => mesh.computeWorldMatrix(true));
});
2 Likes

Thanks,

I think I managed to make my example work. The problem could be related to the lack of material assigned to some meshes and some normals being flipped. I fixed that and now there seems to be no lag between bone and colliders positions. Not sure why, however xd

I will keep your fix in mind and will try it if I spot any additional problems. Thanks for your help. You really helped me a lot.

1 Like

I’m glad it’s settled ^ㅅ^