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

Collision boxes and enemies

Topics: Developer Forum
Feb 16, 2010 at 4:36 PM


So I'm still working on my enemies and right now, I'm working with collisionboxes. The enemies need a collision box on the "top" "feet" "left" and "right" of itself. I've tried to put them on an offset from a "General" body/geom, by setting their position everytime in the update. However, this makes my player, when getting in contact, bounce away from them at a rapid speed. How do i solve this? I guess it has something to do with me setting the bodies every update, but it seemed like my optimal solution. I guess checking normals could probably work too in some way? But I don't know how.

So anway, someone could give me some hints on what I'm doing wrong or something? Any help is appreciated.


Feb 17, 2010 at 8:03 AM

So for each "collision box", you have a body right?

Anyway, for your problem you have two approaches (maybe more, but two come to my mind right now).

Collision Boxes:

You should have only one body, and each collision box should be a geom attached to this body. This way, the position of the collision boxes will update automatically depending on the body's position. When you create a new geom, you attach it to a body, and also you can set an offset (take a look at the geom constructor), this way you could create 4 different geoms for each of the 4 sides of your character.


You can check which side of your enemy has been collided by checking the normals. For this you have to register an OnCollision method for the geom you want to check the side it has been hit, in your load method (after you create your geom) add this:

GeomSensor.OnCollision += OnCollision;

Then, somewhere in your code add the method OnCollision:

private bool OnCollision(Geom enemyGeom, Geom hitGeom, ContactList contactList)
	foreach( var contact in contactList )
		Vector2 normal = contact.Normal; // Here are the normals
	return true;
Hope this helps :)