This project has moved and is read-only. For the latest updates, please go here.

OnCollision & ContactList help

Topics: User Forum
Apr 3, 2009 at 12:54 AM

Let me start by saying how amazing Farseer is. After spending many days and nights attempting to “cook” up my own form of collision detection and simulated physics, Farseer saved the day. However, after searching through the forums I’ve been unable to find a solution to my problem. In a nutshell, what I’m looking for is a way to detect where a collision occurred. While the collisions are handled automatically through the use of Geometry, I need more specific results. 

Initially, I thought that I could accomplish this via the Geom.OnCollision event, but I’ve been unable to find any resources on how to extract collision points from the ContactList, if this is in fact the correct way of finding out collision locations. Can someone please shed some light on this with a description and possibly some sample code? Any help would be greatly appreciated! 

Keep up the great work! 

Apr 3, 2009 at 2:55 AM
Edited Apr 3, 2009 at 3:10 AM
Hang on lemme go check some things *scurries off to visual studio*... Ok heres the deal. When you write your oncollision method, you need to go
foreach Contact contact in contactList do
    insert contact.Position into ContactPositions
You may need to add the geom position to the contact position, genbox will have to tell you whether the position is local to the geom or not.
Apr 3, 2009 at 8:28 AM
Edited Apr 3, 2009 at 8:30 AM
Thanks for the reply Rogue! As soon as I read your reply, I hit up Visual Studio and got down to business. After some data collection, I came to understand that the Contact.Positon(s) are actually offset by the objects Origin/Body Center. Looking back to the Farseer documentation, this makes perfect sense. In the end, I came up with the following solution which checks for any collision along the bottom edge of a given body:

foreach (Contact contact in contactList)


if (contact.Position.Y - player.v2Origin.Y == player.body.Position.Y) 
// Do work  


Without a doubt this works flawlessly. And then there were more problems. For some very strange reason, when I’m jumping into an object ( platform style ) the player bounces off the sides with incredible force, and that pushback stays until the player hits the ground. Even more interesting, if the player falls off the edge and pushes into the object, nothing happens. It only happens while jumping. 
This might bring up some questions regarding my jump code. Even thought I have a custom code to handle the jump actions, simply all it does is 

Body.position +=v2JjumpVelocity; 

Any ideas?

Apr 3, 2009 at 10:06 AM
Ah! there's the problem... try body.ApplyImpulse (or body.ApplyForce if thats too strong) instead of directly affecting your position.