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

Collision Event help

Topics: User Forum
May 20, 2007 at 4:47 AM
I've not been able to work out how to use/access the RigidBody Collision event.

Can anyone point me in the right direction?

May 21, 2007 at 5:34 PM
I think demo number 8, the one with the car uses the collision event.
May 23, 2007 at 4:50 AM
Thanks crashlander.

I've been unable to work out where in demo 8 this happens, probably because I don't know what I'm looking for.

This is the solution I've hacked up from the C# help files:

myRBPhysicsObject.Collision += new EventHandler<CollisionEventArgs>(myCollision);

public void myCollision(object sender, CollisionEventArgs e)
//argh! I've been hit by e.Body2

I'm not sure if this is how it's supposed to be done, but it works.


Jun 17, 2007 at 6:32 PM
Edited Jun 18, 2007 at 7:23 PM
Hi Crashlander and vim,

I'm trying to add an eventHandler to a PointRigidBody but am having difficulty doing so. The context of my code is that I'm writing a bullet_manager, when the trigger is pressed a preloaded PointRigidBody is added to the bullet array, and when the bullet explodes or its time expires then I want it to remove itself from the array,

Here is my code...

physicsBody = new RigidBody{"[max_amount_of_bullets];
for (int i = 0; i < maxamountof_bullets; i++)
physicsBody[i] = new PointRigidBody(1f);
physicsBody[i].LinearDragCoefficient = 0f;
physicsBody[i].Position = new Vector2(positionm[i].X, positionm[i].Z);
physicsBody[i].Collision += new EventHandler<CollisionEventArgs>(remove_bullet);

public void remove_bullet(object sender, CollisionEventArgs e)

This code isn't complete, I need to pass in the right array index, all I'm trying to do first is to get the event to fire.

Am I doing the code correctly? Maybe I've missed something obvious?

Thanks in advance,

I've also got an add_bullet method that includes the Farseer.Physics.Add(this.physicsBody[0]) method in it.
Jun 18, 2007 at 8:53 PM
I've been investigating further and think I may have a similiar issue with Zimbarbo.

I changed the physics body that I was using like so...

//physicsBodyi = new PointRigidBody(1f); OLD

physicsBodyi = new RectangleRigidBody(5f, 5f, 1f); // NEW

And now the bullets are firing off the collision events when they bang into other bodies on the screen.

So I guess that this is just something that I will have to live with? I found that when I set the rectangle rigid body to 3f x 3f that the events started to fire off only half of the time.

I keep answering my own questions. One day I will think about these problems before I post them. Anyway, great physics engine, looking forward to the new release.

Kind regards,