Collision Detection Only

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Ryan
Posts: 1
Joined: Tue Feb 28, 2012 6:18 pm

Collision Detection Only

Post by Ryan »

I am working on a space combat game that requires very specific flight mechanics, something I don't want to use physical simulations for. However, I am very interested in using the collision detection features of BEPU Physics, and after floundering with it for a week or so (very new to physics engines) I'm beginning to think I'm not using the engine the proper way.

Essentially what I'd like to do is simply create a couple of collidable triangle meshes and hook into the collision event with my own handler. It seems as though this should be very easy with BEPU, but I feel like I'm doing something wrong. Currently what I'm doing is after loading my graphics model, I load a separate mesh representing the collision model, extract its vertices and indices, create a MobileMeshShape out of them, create a MobileMeshCollidable out of that, and then an Entity out of that. Then I add a collision handler to it and add it to the Space:

Code: Select all

private Entity _physicsMesh;

void LoadContent()
{
	Vector3 [] verts;
	int [] inds;
	Model physModel = EngineManager.ContentManager.Load<Model>(_phyicsModelFilename);
			
	TriangleMesh.GetVerticesAndIndicesFromModel(physModel, out verts, out inds);

	MobileMeshCollidable mmc = new MobileMeshCollidable(new MobileMeshShape(verts, inds, AffineTransform.Identity, MobileMeshSolidity.Counterclockwise));
	_physicsMesh = new Entity(mmc);
	_physicsMesh.CollisionInformation.Events.InitialCollisionDetected += HandleCollision;

	MyEngine.PhysicsSpace.Add(_physicsMesh);
}

public void HandleCollision(EntityCollidable sender, Collidable other, CollidablePairHandler pair)
{
	// Do stuff...
}
During the update loop, I set the _physicsMesh.WorldTransform equal to my manually-transformed graphics mesh's world matrix, and update the Space, expecting that when two of these meshes collide, it should call their respective HandleCollision methods:

Code: Select all

_physicsMesh.WorldTransform = _graphicsMesh.WorldMatrix;

// ...

MyEngine.PhysicsSpace.Update();
Except, of course, that this doesn't work. I suspect it's either because I'm creating the wrong kind of entity, or that I'm simply using BEPU fundamentally the wrong way. Any suggestions to get me pointed in the right direction would be greatly appreciated! Thanks in advance!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Collision Detection Only

Post by Norbo »

It appears that the mobile mesh entities are all kinematic (a perfectly reasonable choice for the externally controlled spaceships). Kinematic entities have effectively infinite mass and do not respond to collisions, so by default, they do not generate contacts with other kinematic or static objects. This behavior is defined in the CollisionRules.CollisionGroupRules. Clearing that dictionary or setting up other groups and rules would fix the issue.

More information about collision rules can be found here and in the BEPUphysicsDemos CollisionFilteringDemo (and a few others where it's used to set up the simulation, like the RobotArmDemo).

Side note 1: The explicit creation of the MobileMeshCollidable can be cut out. There exists an Entity constructor which takes a shape directly and configures the collidable automatically. The collidable constructor is useful in a few particular instances where the collidable needs to be configured beforehand.

Side note 2: MobileMeshes are rarely the best option to represent objects. They are substantially slower than other options like a compound shape of convex hulls or primitives by virtue of dealing with so many more individual objects (triangles). The alternatives are generally a bit more robust in collision detection too thanks to their relative simplicity.
Post Reply