noob simple collision detection

Topics: Developer Forum, User Forum
Jan 26, 2009 at 9:40 PM
I currently have one player object and a bunch of other "terrain objects" the player interacts with. I would like the player to only be able to perform an action when it is colliding with any of the other "terrain objects". I'm not quite sure how to do this even after reading up a bunch of posts. A simple isCollide function would be nice that returns a bool if the player is colliding with anything. How can i go about accomplishing this? Ive tried to figure out OnCollide but couldn't understand how to use it?
Coordinator
Jan 27, 2009 at 8:35 AM
It may seem complicated, but it's actually rather easy. :)

We have 2 forms of collision feedback systems:

circleGeom.Collide(rectangleGeom);

This line of code returns true (if colliding) or false. You can check for this collision once or all the time in your update loop.

---------------------------------------------
circleGeom.OnCollision += OnCollision;

private bool OnCollision(Geom geom1, Geom geom2, ContactList list)
{
    return true;
}
---------------------------------------------
Those lines of code uses the other system that works by using a C# delegate. What this means is that when a collision occurs inside Farseer Physics, it automatically calls your method you just created. In normal terms you "subscribe" to the OnCollision event (delegate). This way is really easy to use once you get it going.

If you have several objects you would like to subscribe to, you can do something like this:

---------------------------------------------
circleGeom.OnCollision += OnCollision;
rectangleGeom.OnCollision += OnCollision;
---------------------------------------------
Essentially targeting the same method.
Or perhaps make it in a loop.



Jan 28, 2009 at 2:35 PM
maybe this helps: http://www.codeplex.com/FarseerPhysics/Thread/View.aspx?ThreadId=44960
Jan 28, 2009 at 3:45 PM
genboxs reply worked quite well. Problem was that it seemed to call the function multiple times i think due to the fast frame rate after my character would jump from the ground. This resulted in my character jumping higher then he should (if that made any sense). To fix this i added a "elapsedTime" that only let you jump once every second.
PROBLEM SOLVED!!!

now i would please like an explination on how the following works
==========================================
circleGeom.OnCollision += OnCollision;

private bool OnCollision(Geom geom1, Geom geom2, ContactList list)
{
    return true;
}
==========================================

Right now it just seems like a magical function that is somehow overloaded?
and knows when to be called even tho i never specified a second geom or a "contactlist"?

Sorry I'm pretty new to programming, so i get confused easily :)

Coordinator
Jan 28, 2009 at 4:47 PM
Just a little magic is involved ;)