Inclusion / Exclusion

Jan 3, 2008 at 6:02 AM
Edited Jan 3, 2008 at 6:06 AM
I've been sifting through your physics engine, I would like to think it will serve the purposes i need pretty well right out of the gate. So thank you for that!
I just have one question, is there an enumeration or the like where you can assign objects a "type" and tell other objects that they should test for collision with or not with?
Cheers
Kyle
Coordinator
Jan 3, 2008 at 2:55 PM
Edited Jan 3, 2008 at 2:56 PM
Thanks for the interest in my project! To filter collisions of objects, you can use the CategoryFlags and CollisionFlags fields of the CollisionShape/CollisionSphere/CollisionBox/CollisionPlane as bit masks. for example, you could have a enumeration like so:

public enum GameObjects
{
	Player = 1,
	Enemy = 2,
	World = 4
}

...and a player class that has a collision shape with the designated category and collision flags:
	this.collisionShape.CategoryFlags = (int)GameObjects.Player;
	this.collisionShape.CollisionFlags = (int)(GameObjects.Enemy | GameObjects.World);

... and then a enemy class defined like so:
	this.collisionShape.CategoryFlags = (int)GameObjects.Enemy;
	this.collisionShape.CollisionFlags = (int)(GameObjects.Player | GameObjects.Enemy | GameObjects.World);

...and then the world:
	this.collisionShape.CategoryFlags = (int)GameObjects.World;
	this.collisionShape.CollisionFlags = (int)(GameObjects.Enemy | GameObjects.Player);

The above code would let players collide with enemies but not with other players and let enemies collide with players and other enemies. Hope that helps.
Jan 3, 2008 at 7:10 PM
Edited Jan 3, 2008 at 8:35 PM
Thats awesome thanks. I was also wondering about some other things as I wonder about the code;

On the same thought as the last question, is there a way to make a body ignore another very specific body? Like Tag.rigidBody? For example bullets not colliding with the guy that fired them because its intersecting with some large sphere on the way from a gun?

Can you build compound objects, I see a composite object, is that the same? HOW?

Also, how do you use the collision callback, I think I'm having some trouble with it.

Thanks again

Kyle


Coordinator
Jan 4, 2008 at 4:58 PM
You could handle invalidating collisions between a player and his/her bullets at the time of the shot fired in a number of ways. The CollisionFlags field of the CollisionShape class can be changed at any time. It's not just an initialization value. Extending the previous post, start with GameObjects.Player removed from the bit mask of the CollisionFlags field of the bullet. Then, you just need to determine when to add it back in. you can also prevent a collision using the collision callback features of the framework, which I'll discuss in a second.

You can build a compound object, but it's currently a bit difficult to get set up. I'm planning on remedying this in the future. For now, to get it working, you essentially need to create multiple CollisionShape instances that are associated with one RigidBody instance (i.e., all CollisionShape instance's RigidBody property are set to the same RigidBody instance.) Then, offset each CollisionShape instance using it's Offset property - a matrix that defines the CollisionShape's relative rotation/translation from the RigidBody's Position and Orientation. The position of the RigidBody should essentially be the center of mass of the composite shape (to keep things simple), so really creating good offsets for the CollisionShapes is crutial.

To keep things simple, you may want to use one of the MatrixExtension.CreateXXXInertiaTensor functions that most closely matches your composite shape to set the RigidBody's inertia tensor.

Using the ICollisionCallback is pretty simple. Just implement it with a class (normally, your Game class) and pass that class to the "callback" parameter of the PhysicsComponent's constructor. For an example, look at MainMenuScreen.cs. To actually respond to collisions, such as adding sounds when two shapes collide, take a look at a few of the other discussions here. The PhysicsComponent generates a lot of collisions and you probably only want to react to collisions that have a high relative velocity or meet a certain time or velocity threshhold.

Good luck!