Help with collision

Topics: Developer Forum, User Forum
Sep 19, 2011 at 6:49 AM

Hi everyone!

I'm not new to programming with C# but I am new in programming with the Farseer engine and I was just wondering how to use OnCollision. I've looked on some sites and even here but I'm still a little lost as what I am supposed to be doing with it.

What I'm trying to do in my game is that when two bodies collide to have a sound effect play. From what I have gathered looking around on sites and here my best bet would be to use the OnCollision, but I can't find any documentation or examples of how to really use it correctly. I've tried looking through the examples that are up on here but I wasn't able to find anything that seemed like it would help.

So if anyone has links, examples or where I could look in the examples from here it would really help!

 

Thanks again for any help anyone can give.

Sep 19, 2011 at 9:51 AM
Edited Sep 19, 2011 at 9:52 AM

it is very simple. (for 3.2)

Fixture.OnCollision += Collide;

public Collide(Fixture a, Fixture b, Contact contact)

{

    //play sound

}

Sep 19, 2011 at 6:40 PM

Thanks!

I'm using 3.3.1 and that worked only problem I have is that it plays the sound like 100 times a row and is slowing my game down. I figure the way I would solve that would be to put in a timer so that it can only play the sound once every second or two.That is unless you have a better idea for that. :P

Also I take it if I wanted to play different sounds when collision with other bodies has happened it's just a matter of adding in some If statement checks to check the different Fixture B's?

 

Again thanks for the help this was just driving me insane trying to figure it out.

Oct 19, 2011 at 3:06 PM

Thank you HellGate for your reply to Smurfx24, could you help me out with the same issue:

What do i need to do to get the same result as above, i tried your code with no luck. I want to say (if body a collides with body b then do xyz)

i start to type this in and get:

GrenadeLauncher.grenade[0].OnCollision +=new OnCollisionEventHandler(Game1_OnCollision);

could you write out what and how i should be structuring the Game1_OnCollision? i can see that its:

public void Game1_OnCollision(Fixture a, Fixture b, ContactManager ??)
        {

        }

Am i doing something wrong here?

 

thanks,

Mal

Oct 25, 2011 at 1:30 PM

Could anyone help here?

Oct 25, 2011 at 2:32 PM

GrenadeLauncher.grenade[0].OnCollision +=new OnCollisionEventHandler(

press tab here an it will create a basic function for you (sometimes you have to rewrite it from the += )