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

Getting information which side of an rectange(body) collides!

Topics: Developer Forum
May 19, 2012 at 9:16 AM


I need the information which side of my rectangle collides! How can i get it?




May 22, 2012 at 7:19 AM

Check the contacts properties, there you should have the position where the collision has been detected.

May 22, 2012 at 1:06 PM

I can't find there an option where you can get the information, which site of my rectangle(created with the bodyfactory) collides.

May 22, 2012 at 1:22 PM ;)

May 22, 2012 at 4:47 PM

Great! Thanks.

I am a beginnner and so its difficult to follow, although it looks like that its made with an older version of Farseer. I tried to import it in my project but it don't work.

Creation of body:

body = BodyFactory.CreateRectangle(world, (float)ConvertUnits.ToSimUnits(playertexture.Width), (float)ConvertUnits.ToSimUnits(playertexture.Height), 1f);
            body.BodyType = BodyType.Dynamic;
            body.Position = new Vector2(ConvertUnits.ToSimUnits(position.X), ConvertUnits.ToSimUnits(position.Y));
            body.Mass = 1f;
            body.OnCollision += new OnCollisionEventHandler(body_OnCollision);


On Collision:

 bool body_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
            Vector2 normal = contact[0].Normal;  !!!Error!!!

            //Needed to reverse the normal depending on the first created geometry.
            if (fixtureA < fixtureB) !!!Error!!!
                normal *= -1;

            //Create rotation matrix
            Matrix r = Matrix.CreateRotationZ(- body.Rotation);

            //Transform the normal using the rotation matrix
            Vector2 rotatedVector = Vector2.Transform(normal, r);

            //Use Atan2() to get the angle between the vector and the positive x-axis.
            //Note that this function returns -Pi to Pi
            float radians = (float)Math.Atan2(rotatedVector.Y, rotatedVector.X);

            //Lookup the angle in the intervals
            _side = GetSideFromRadians(radians);

            return true;

May 23, 2012 at 1:37 PM

Hi Michixxx. Remove everything from the body_OnCollision method, just leave the "return true" (so you will have the collision reaction active).

Now, check the contact's variables properties. Right now I can't remember which one is, but one of them will give you the coordinates of the collision point.

May 23, 2012 at 1:45 PM

I have already looked for a propetie that gives me the point of collision, but I haven't find one.

May 24, 2012 at 2:46 PM

Sorry Michixxx, I'll have to check it on the code. Right now I'm at work, but as soon as I get home, I'll check it out for you. Cheers.

May 25, 2012 at 2:07 PM

Have you found it?


May 25, 2012 at 6:36 PM

The Contact has a property called Manifold which contains the contact points information and the normal.

May 27, 2012 at 7:29 AM

Have you actually tried that though?  Every time I do it doesn't seem to give what I would think would be the right direction.  I also haven't been able to figure out if it takes into account multiple contacts.  If your square (in my case a character) is walking on a floor and runs into the wall at the same time, then which does it give you?

You may have to use that onColission (or in my case, I was using the Begin Contact override function from the sensor test example) to make a list of each contact each frame.  Then check each one in that list against whatever your question may be.