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

Simple Collision Event

Topics: User Forum
Feb 8, 2011 at 9:35 AM
Edited Feb 8, 2011 at 4:01 PM

I'm still trying to figure out collisions in the new engine, I had some experience with version 2 and used to call collisions with 

Geometry.OnCollision += OnCollision;

then had an OnCollision event that took 2 geometry arguments and a contact list

within this even I would put stuff like characters losing health and such.

I've been scouring the forums and looking at samples but I can't figure out how to recreate this in version 3.

I tried figuring out if fixture.OnCollision += OnCollision would work but I couldn't do it, forgive the basic question I'm still a beginner some help would be greatly appreciated. 

Feb 8, 2011 at 6:59 PM

Fixture.OnCollision is the same thing, however, there are some differences:

1. It gives you two fixtures, the first one being the one you registered the delegate on.

2. It is not continuously called - only once per collision on each fixture. It might get called several times each timestep due to the TOI solver.

Feb 8, 2011 at 11:16 PM
Edited Feb 9, 2011 at 8:41 AM

I'm confused.

So how do I call it?

I've tried Fixture.OnCollision += OnCollision; (on creation)

and the function


OnCollision(Fixture fix1, Fixture fix2)


//example interaction

	if(fix1.UserData == "enemy" && fix2.UserData == "bullet") //because I assigned all new fixtures appropriate names


		enemies[enemyBodies.IndexOf(fix1)].health -= 1;

		bullets[bulletBodies.IndexOf(fix2)].Active = false;




Which obviously doesn't work, I just need to know the proper way of calling the OnCollision event in version 3. Even just a simple sample code would help.

Feb 9, 2011 at 8:18 AM
You are very close. You need the last parameter of the OnCollision
method. It has two fixtures and a contact.

You could take a look at the CharacterCollision test from the Testbed project.
Feb 9, 2011 at 9:57 AM

Got it! thanks for the help it works great.

Feb 9, 2011 at 10:45 AM

Also shouldn't your code look something like this:

if(fix1.UserData as string == "enemy" && fix2.UserData as string == "bullet")

I'm not sure if it is casted to string automatically...

Feb 9, 2011 at 7:43 PM

its been working so far, when I created each fixture I assigned their user data like

fixture.UserData = "player";