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

Collision detection

Topics: Developer Forum, User Forum
Dec 18, 2011 at 2:55 AM
Edited Dec 18, 2011 at 3:19 AM

hello everyone,

So, I'm trying to check collisions in mine game like if the body(Character) touch on the border he has to start again.

I've tried to use this:

        objPersonagem.OnCollision += new OnCollisionEventHandler(objPersonagem_OnCollision);

(i have to use this on the update() method right?)

with the prop tab-tab(I think is it)and they gave this handler:

        bool objPersonagem_OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
            throw new NotImplementedException();

And here is the question: What do I have to insert in this handler to do what I want?

(which type of clause do i have to put in place of "

throw new NotImplementedException();


Please help me.

Dec 18, 2011 at 12:49 PM

You could write this for example:

        private void OnCollision(Fixture fixA, Fixture fixB, Contact contact)
            if (contact.IsTouching())
                if (fixA.Body.GetType() == typeof(PlayerBody) & fixB.Body.GetType() == typeof(GroundBody))
I don´t know exactly if "contact.IsTouching()" is required. You just have to check whether the objects are Ground & Player, then reset the game.
You also can use the "UserData"-Property in Fixture/Body to specify which object this Body/Fixture represents, so you don´t have to inherit a new class from Body.

Dec 18, 2011 at 6:22 PM
Edited Dec 18, 2011 at 6:48 PM

And I can call this method in update() method, Setting the fixA as the fixture of the Player, and the fixB as the fixture of the ground?

Like this here:


OnCollision(fixPlayer, fixGround, Contact contact);


It's Right?

So, thanks for the help so far.



Edited: I've tried to do this but it gaves me this error:

Dec 18, 2011 at 7:21 PM

Yes you can, but that´s not the sense of the thing! It´s an event handler, so it gets called automatically by Farseer everytime a two bodies/fixtures collide.

How experienced are you at programming with C#? Apparently you don´t know what events are...

And of course, if you call the handler without a real contact, there comes an error, because the reference of contact-parameter is missing.

Dec 18, 2011 at 8:14 PM

Yes I've started programming about 10/11 months i think...

So, I don't have to much experience with it, and i didn't know how a event are. XD

But I think I've got the idea, and i found a way to use it. thank you.

Just for show you how i used it and you say if it's corretc:

        protected override void LoadContent()
            objPersonagem.OnCollision+= new OnCollisionEventHandler(OnCollision);

        bool OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
            if (contact.IsTouching())
                if (fixtureA.Body.GetType() == objPersonagem.GetType() & fixtureB.Body.GetType() == objPlataforma.GetType())
                    objPersonagem.SetTransform(new Vector2(300f, 100f), 0f);
            return true;

 It Works.

Thank you so much, and one more question: I can use this same method with other bodies, is just I add the clause on the first one(if(contact.IsTouching){}) and add the event on the on collision of the body on the LoadContent(). That's Right?