This project has moved. For the latest updates, please go here.

Calculate Impulse in OnCollision Method

 Topics: User Forum Wiki Link: [discussion:389450]
 macgeek911 Jul 29, 2012 at 2:11 PM Farseer Newbie question: I am trying to to figure out how to use Contact that is provided in OnCollision event to calculate the collision impact. I am trying to create an Asteroids game to learn Farseer Engine.  I have a Rock class that has a body_OnCollision(Fixture, Fixture, contact) method. Using the contact I should be to calculate the impact each rock will create on collision and Apply it to FixtureA using fixtureA.Body.ApplyLinearImpulse(impulse); Could someone please advise me how to accomplish this. Also is using ApplyLinearImpulse the correct method that I should use to apply the collision impulse to. ```private bool body_OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact) { if ( (ObjectType)fixtureB.Body.UserData == ObjectType.ROCK ) { //using contact how do I calculate impulse of collision between two rocks //Vector2 impulse = calculation of collision impulse fixtureA.Body.ApplyLinearImpulse(impulse); } if ( (ObjectType)fixtureB.Body.UserData == ObjectType.BULLET ) { //do something.... } return true; } ``` jerrysb Jul 29, 2012 at 6:56 PM Are you implementing a custom collision response because the default one is not good for you? Normally the solver calculates the impulses. You can get the result in PostSolve/AfterCollision but there they have already been applied. macgeek911 Jul 29, 2012 at 9:55 PM At the moment when rocks in my scene collide they stick to each other, sometimes they disperse their merry way but most of the time they stick onto each other. What I did earlier on collision, set their Restitution = 1f so they would bounce off each other, and in OnSepration event, set Restitution = 0f.  Unfortunately, they are not bouncing off each other as I had thought. I think what I probably need is based on 'contact' somehow calculate the impact, then subtract it from its current LinearVelocity or somehow change body.Rotation of each Fixture so it after collision it looks natural that the two rocks changed their direction path on impact.   Any ideas how I could use the contact and calculate the change in Rotation or LinearVelocity of the two fixtures to accomplish the desired effect I am trying to achieve? jerrysb Jul 29, 2012 at 10:35 PM Just a note regarding  the problem you describe. If you're getting a perfectly inelastic collision either the restitution is wrongly 0 despite you trying to set it to 1 or you might have a scale issue. I'd bet it's the former.  As for the latter if the velocity is below a certain threshold the engine will treat the collision as an inelastic collision. This is done to prevent bodies standing on top of each other to bounce constantly due to gravity and the like. And this will also make bodies stick to each other mid-air for no apparent reason.  The setting that controls this is in Settings.VelocityThreshold. macgeek911 Jul 30, 2012 at 11:31 PM You are right, my Linear Velocity is between 0.2 and -0.2, but not sure how the -ve came to play. Printed string outputs to track it. I tried to lower the Velocity Threshold to 0.2, but could not get the result I was expecting. Just might have to take the Box2D route of using BeginContact/EndContact and PreSolve/PostSolve approach to fine tune control the collision impact to the way I would like to make it happen. I'll look at some college physics books to learn about collision impact calculation. Let me know if you have any good game physics resources that might help me in this pursuit. Once I figure it out, I'll post it on the forum, so others won't have to take as along as it is taking me. At some point in time, I do have to learn box2D so I can port my Farseer learnt skills to Android and iOS development. So better suck it up and start learning.