PreCollision Detector

Topics: Developer Forum, Project Management Forum, User Forum
Apr 22, 2008 at 2:45 AM
So what I want to do is create a circle around a Game Body to detect whether or not there is another Game Body nearby. The purpose for this is to create an AI for all the enemy Game Bodies in my game. My question is how do I do this, I have the Game Bodies all set up, all I need now is the outer circles. The problem is I don't know how to detect whether a collision has occurred, I've heard this is possible with the CollisionHandler, but I don't know where to look does one of the demos have it? Also I want to know how to make it so when something collides with outer circle it doesn't actually affect the motion of that object, obviously with collision categories, but something like this thread: http://www.codeplex.com/FarseerPhysics/Thread/View.aspx?ThreadId=15330.

Thanks in advance.
Apr 22, 2008 at 7:28 AM
In your GameBody class define a boolean variable named Collided:
bool Collided = false;

Define a function like this in your code:

private bool HandleCollision(Geom g1, Geom g2, ContactList contactList)
{
Collided = true;
return false; // this lets the physicssimulator ignore this collisoin
}


And in your Update method, set Collided to false before you call pysicsSimulator Update.


Now, when you create your GameBodys body and geoms, do it like that:

body = BodyFactory.Instance........ // CreateBody
geom1 = GeomFactory.Instance..... // Create your normal Body geom
geomCircle = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, body, 10, 32, new Vector2(0, 10), 0);
geomCircle.Collision += HandleCollision; // Set the above Collisionhandler to trigger whenever the circle collides...

Now you know when the circle collides with anything else... thats when the variable collided is true.



Apr 24, 2008 at 5:11 PM
If you just want to see if one thing near another, I'd suggest that you just find the distance between the bodys' position values. (Subtract one of the Vector2s from the other, and call Length() on the result.) There isn't really any need to calculate the physics for invisible intangible objects.
Apr 24, 2008 at 9:54 PM
The problem that I have is I have a bunch of objects so I would have to run through each object checking to see if they're near any other object. I think it would we easier just to use Farseer and look for nearby collisions.