## How to detect collision for an animated deforming mesh?

m_miro
Posts: 3
Joined: Fri Jun 07, 2019 1:48 am

### How to detect collision for an animated deforming mesh?

Hello,

I would like to detect collisions for a 3d model that is being animated for a video game.
The mesh of the 3d model is being animated using skeletal rig which moves vertices according to their bone weights.

I'm not too familiar with the bullet physics engine, I know that bullet supports soft body physics and deformation but according to this blog:
http://digitalopus.ca/site/bullet-physi ... ed-meshes/
soft bodies are controlled by physics which can be used to match a higher resolution mesh that is rendered. Is it possible to control soft bodies with animation instead of physics?

I also looked at the bullet manual:
https://github.com/bulletphysics/bullet ... Manual.pdf

Which mentions a btGimpactTriangleMeshShape, a collision shape that can use convex or concave triangle meshes for collision, but its only mentioned once and I can't seem to find anything about it on the github repository.

drleviathan
Posts: 550
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

### Re: How to detect collision for an animated deforming mesh?

Does the animated thing need to "react" to collisions from other objects? Or should the animation continue unimpeded and only the other objects be affected?

If the animated thing does NOT need to react to outside collisions then its collision shape could be approximated using multiple keyframed RigidBodies.

If the animated thing is mostly a hierarchy of rigid meshes with minor blending at the joints (is this the case?) then you might also be able to approximate its moving shape using "animated" constraints between dynamic RigidBodies. However this won't work well for real-time simulation if the model has too many parts. I would guess: less than 10 might be fast enough, depending on various factors (hardware, shape complexities, desired accuracy).

There might also be a way to use Bullet's MultiBody, which can be used for articulated robots.

m_miro
Posts: 3
Joined: Fri Jun 07, 2019 1:48 am

### Re: How to detect collision for an animated deforming mesh?

Only the other object will be affected, the animated thing does not need to be affected.

What is multiple keyframed RigidBodies?

The animated thing is not rigid, it will be constantly moving with walking,running, and jumping.
Here is one of the 3d models I intend to use for testing:

hyyou
Posts: 96
Joined: Wed Mar 16, 2016 10:11 am

### Re: How to detect collision for an animated deforming mesh?

m_miro wrote:
Sat Jun 08, 2019 8:32 am
What is multiple keyframed RigidBodies?
You can change shape of the rabbit rigidbody (e.g. every 10 timesteps).

For example, when it is jumping, two legs may be pop out a lot from the main body. ➨ create an approximate compound shape "A"
When it is sitting, two legs may embed into the main body. ➨ create another compound shape that can roughly describe the sitting rabbit "B"

Then, when the rabbit is kinda sit, you set its shape to "B", etc.
Don't forget to remove/re-add the body to the world after you modify the shape.

It is a nice dirty trick that I have never used.
It can be very good for some situation.
I like rabbit.

drleviathan
Posts: 550
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

### Re: How to detect collision for an animated deforming mesh?

hyyou's idea does work fine if you don't need to pick up velocities from the moving parts. That is, if your rabbit is a slow statue and you want to be able to bounce fast balls off of its changing shape. The rabbit's leg, while changing position over time, won't have any inherent velocity to it so the balls will always bounce off of it as if it were moving at the same speed as the rabbit itself.

I'm sorry, I should have said kinematic instead of keyframed. The two terms are similar but slightly distinct. kinematic is the more correct one in this case.

If you want your walking rabbit to be able to kick a stationary ball forward then you might want to give the rabbit's leg correct velocities. In this case, you could give the rabbit's leg its own RigidBody, but make that body kinematic. Each substep the leg's new transform + velocities would need to be computed and set. Normally this would be done by giving the body a CustomMotionState which has a CustomMotionState::getWorldTransform(tTransform& worldTrans) override.

Bullet will not integrate a kinematic body forward, even if it has velocity. Instead: for each active kinematic object it will call its MotionState::getWorldTransform(tTransform& worldTrans) once every substep. It is the duty of the developer to derive CustomMotionState from btMotionState and implement CustomMotionState::getWorldTransform(tTransform& worldTrans) to do the Right Thing.

Note, the default API of btMotionState::getWorldTransform(tTransform& worldTrans) is to fill worldTrans with the new transform which then gets applied to the RigidBody. There is no room for setting velocities. So what you would have to do is give CustomMotionState a pointer to its RigidBody so it can slam the velocities in that context.

m_miro
Posts: 3
Joined: Fri Jun 07, 2019 1:48 am

### Re: How to detect collision for an animated deforming mesh?

I'll give your idea a shot drleviathan.