Adding forces on collision callback for CCD:

Post Reply
rolfvdhulst
Posts: 7
Joined: Sun Oct 06, 2019 9:10 pm

Adding forces on collision callback for CCD:

Post by rolfvdhulst »

My problem is as follows: I have a ball of which I need fairly accurate, but fast, physics (basically a CCD solution)

The ball rolls at fairly high speed towards a box (part of compound body). On collision, I want a force to be applied on both the ball and the box (NOT along the normal) ; but the collision itself also has a restitution coefficient. I essentially want the force to be added immediately after the collision is processed e.g. the restitution is taken into account. I've tried directly adding a force in the callback, but this resulted in the colliding contact impulse computed by bullet being very small as the artificial force I added is already sufficient to seperate the bodies.

I'm using btMultiBody with CCD enabled for the ball.
Right now, I use the gContactAddedCallback to register the collision, and then set a flag for the next tick to add the
forces. This works better, as then bullet processes the entire collision and then only adds the forces for the next physics tick. This does however mean that the ball/sphere moves a bit after the collision is done at a velocity which is too low (and more importantly, in the wrong direction).

Is there any way to solve this problem nicely? Or do I just have to deal with it?
Unfortunately, the ball is often also touching the ground (for which all friction types are enabled) and in various states of spinning/sliding/rolling, so I cannot easily write an algorithm myself that from the hitfraction does this small extrapolation.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Adding forces on collision callback for CCD:

Post by drleviathan »

One idea that occurs to me is: set the flag in the collision callback but harvest it in an Action within the same substep. If you look inside btDiscreteDynamicsWorld::internalSingleStepSimulation() you will see: the collisions are computed and processed before the actions are updated.
rolfvdhulst
Posts: 7
Joined: Sun Oct 06, 2019 9:10 pm

Re: Adding forces on collision callback for CCD:

Post by rolfvdhulst »

That's a good idea, but unfortunately doesn't work, because integrateTransforms() is already called before that, moving the body. I already tried this with using the post-tick callback, but that doesn't work for exactly the same reason.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Adding forces on collision callback for CCD:

Post by drleviathan »

Ok.

The good news is: btDiscreteDynamicsWorld::internalSingleStepSimulation() is virtual. You could derive MyDiscreteDynamicsWorld from btDiscreteDynamicsWorld and override that method to do what you want. In other words: add doMySpecialStuff() between solveConstraints() and integrateTransforms().
Post Reply