How to change force which is applied during collision?

General discussion about Box2D tutorials
Post Reply
jijik
Posts: 4
Joined: Wed Jan 09, 2013 8:28 pm

How to change force which is applied during collision?

Post by jijik »

I want to change force, which is given to bodies when collide and I want to do it only sometimes, so I do not want to solve it by setting different density etc.

To be more illustrative: I have bullet and some character. When bullet character collision occurs, character is given force in bullet direction. But on some special occasions I want this force to be, lets say, half.

So I think I should do it in presolve callback, but how can I properly modify it.

is

Code: Select all

contact->GetManifold()->localNormal *= 0.5f;
the proper way? I have tried it, but it not seems like it is half as much force.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: How to change force which is applied during collision?

Post by iforce2d »

I don't think there is any easy way to do that right now. This is probably the most necessary feature for Box2D to have in future releases, as I have seen many people asking on the forums about this problem, or similar things that could be solved by allowing each side of the collision to be affected differently.

I would propose something like this for the b2Contact class:

Code: Select all

void b2Contact::setReactionFactor( float weightA, float weightB );
This would scale the normal collision response by the given factor for each of the colliding bodies. Passing a value of 1 for each parameter would cause no change. Passing 0.5 for one of them would do what you are needing, etc.

Unfortunately, I don't have the skill to implement it... if the simulation only uses one position/velocity iteration for the Step() it may be fairly easy, but the problem is that usually multiple steps are performed, and somehow they must all work together to arrive at the desired final result. It also goes against the core principal of the collision response which is supposed to be correcting any overlap. Maybe we can persuade Erin to look at this sometime :)
jijik
Posts: 4
Joined: Wed Jan 09, 2013 8:28 pm

Re: How to change force which is applied during collision?

Post by jijik »

thank you, too bad it is not possible... How to fake it? What is the best way to find out what force was applied due to collision? Then I just could apply some half size opposite force...
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: How to change force which is applied during collision?

Post by iforce2d »

I think the closest thing you could do is to check what impulses were applied by the collision (in the PostSolve function you can get these (eg. http://www.iforce2d.net/b2dtut/sticky-projectiles "Deciding whether to stick") and apply some impulse of your own to try to counteract this, but in my experience it's not so simple. Actually for that 'sticky projectiles' tutorial I started to try that but gave up because I could not get it to work as expected.
Post Reply