Which one to use? dynamic or kinematic body?

minas1
Posts: 3
Joined: Wed Nov 18, 2015 9:02 pm

Which one to use? dynamic or kinematic body?

Post by minas1 »

My situation is as follows:

I'm making a game in which the player (dynamic body) can fire blasts. What I want is to:
1. Be able to know if a blast hits a player in order to decrease his health.
2. The blast should not disposition any body it hits.
3. The blast should collide with static bodies.

Which one should I use, dynamic or kinematic bodies? I have read the user manual but it does not explain when and why I should choose each.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Which one to use? dynamic or kinematic body?

Post by drleviathan »

Because of requirement (2) you would make the character kinematic and the blasts dynamic (will satisfy (3)).

Kinematic object transform and velocities are set via some external logic (typically using a custom MotionState). They can update their transform, and they can have non-zero velocity (for correct collision but -- but the physics engine will not integrate them forward), but they move entirely by external logic. Dynamic objects will bounce off of kinematic ones, but kinematic objects will move as if they have infinite mass (collisions do not change their position or velocity).

As for (1)... one way to do it would be to monitor collision events and analyze the collision info when characters are hit. Utilities for detecting collision-start and collision-end events do not exist in Bullet proper, however there are a few examples floating around. Some may be in the Demos, but there are also a few good examples in old threads of this forum.
minas1
Posts: 3
Joined: Wed Nov 18, 2015 9:02 pm

Re: Which one to use? dynamic or kinematic body?

Post by minas1 »

What if I additionally want the player to collide with static geometry as well?

Is it a bad solution to make both dynamic and set a small mass for the blast?
minas1
Posts: 3
Joined: Wed Nov 18, 2015 9:02 pm

Re: Which one to use? dynamic or kinematic body?

Post by minas1 »

drleviathan wrote:Because of requirement (2) you would make the character kinematic and the blasts dynamic (will satisfy (3)).

Kinematic object transform and velocities are set via some external logic (typically using a custom MotionState). They can update their transform, and they can have non-zero velocity (for correct collision but -- but the physics engine will not integrate them forward), but they move entirely by external logic. Dynamic objects will bounce off of kinematic ones, but kinematic objects will move as if they have infinite mass (collisions do not change their position or velocity).

As for (1)... one way to do it would be to monitor collision events and analyze the collision info when characters are hit. Utilities for detecting collision-start and collision-end events do not exist in Bullet proper, however there are a few examples floating around. Some may be in the Demos, but there are also a few good examples in old threads of this forum.
What I am not sure about:
If the player is a kinematic body, can I set his linear velocity through the motion state? Or I have to manually move it by changing his position?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Which one to use? dynamic or kinematic body?

Post by drleviathan »

If the character is kinematic then you must explicitly set its position. The right way to do this is to implement CharacterMotionState::getWorldTransform() to compute the correct transform (using "game data" external to Bullet itself). It can also set the velocity correctly in that call, but that is only useful for allowing Bullet to produce more correct collisions -- Bullet will not integrate Kinematic objects forward.

In order for kinematic objects to collide against other objects (static, kinematic, or dynamic) you would use the btKinematicCharacterController which handles such collisions by sweeping the character through space and figuring out where to leave it at the end of all the sweeping. However if you choose this route I strongly recommend that you implement your own KinematicCharacterController: start with the provided example and modify it to suite your needs, and this would mean that you'll have to study how a KinematicCharacterController works (or fails to work) in great detail, and you'll probably encounter performance problems if you try to sweep the character near some content (such as btCompoundShapes with many children).

Another, simpler way to go would be to let your character be dynamic but satisfy condition (2) by giving your character high mass and the blast object low mass. A fast moving blast might bump the character a little bit but not much -- in theory.