Autogenerate Collision

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Keta
Posts: 8
Joined: Tue May 22, 2012 6:56 pm

Autogenerate Collision

Post by Keta »

Hey everyone

Now, I'm not exactly 100% sure, but it seems that "CompountBody" can be build up from a lot of smaller objects, to create one complex collision object... Given this, I'm sure it would be possible for the more experienced of you people to write a "Auto Generate Collision" class, which, as it states, automatically generates the collision, based on whatever model you pass into the class. Am I wrong?
If not, would someone be kind enough to do this, seeing as that is basically all I need to get the game I'm working on ready to release a beta.

Thanks
Keta
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Autogenerate Collision

Post by Norbo »

If you want to use a mesh, there exists a MobileMesh class which will represent every triangle in a source model with greater speed than the direct equivalent CompoundBody.

However, approximations are usually preferred for performance reasons. Approximations usually involve either a set of simple primitive shapes like BoxShapes or a slightly greater level of fidelity with multiple ConvexHullShapes. There is no current automatic generator for approximations. There do exist some algorithms out there which can be used to create approximate convex hull decompositions of models (like HACD), but I have not had the time to implement such a thing.
Keta
Posts: 8
Joined: Tue May 22, 2012 6:56 pm

Re: Autogenerate Collision

Post by Keta »

Ah, yeah I guess that makes sense... I do have another question though, which I am honestly a bit embarrassed to ask, cause of how simple I'm sure it is... How do I relocate the center of the collision box, as well as scaling it to fit my object? xP Because I have tried a lot of different things, but none really seems to do it. They all seem to also deform the object itself >.<

Thanks
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Autogenerate Collision

Post by Norbo »

How do I relocate the center of the collision box
If you want to move an entity, set the entity.Position.

If you want to offset a collision shape from the entity.Position (which also acts as the center of rotation), use the entity.CollisionInformation.LocalPosition. That will offset the collision shape in local space. So, setting a LocalPosition of (0,2,0) would offset the collision shape in world space by entity.OrientationMatrix.Up * 2 away from the entity.Position.
as well as scaling it to fit my object?
Each entity has different ways to change its scale. Boxes have width, length, and height, spheres have radius, cylinders have length and radius, etc. A convex shape can be put inside a TransformableShape to perform arbitrary linear transforms on the shape. This prevents collision detection special cases from being used, so it can be a little slower.
Because I have tried a lot of different things, but none really seems to do it. They all seem to also deform the object itself >.<
I would recommend playing around in the BEPUphysicsDemos (available in the main source download) so that the effects of changes can be seen immediately through the debug visualizer without worrying about what might be affecting graphics. Once you can change the collision representation in isolation, it becomes easier to adapt graphics to match.

Also, for more complicated shapes like ConvexHullShapes and others, beware of shape recentering. This is a common source of problems in aligning graphics and collision shapes.
Keta
Posts: 8
Joined: Tue May 22, 2012 6:56 pm

Re: Autogenerate Collision

Post by Keta »

Alright I now managed to get the center of the box relocated, however... I still can't seem to edit the size of the collision box without deforming the model itself :/

Box GroundBox = space.Entities[0] as Box;
GroundBox.CollisionInformation.Shape.Height += 1.0f;
Matrix scaling = Matrix.CreateScale(GroundBox.Width, GroundBox.Height, GroundBox.Length);
Components.Add(new EntityModel(space.Entities[0], Cube.TestModel, scaling, this));

That affects the model itself :s And I don't see how/where else I'm supposed to edit the size of the collision box...

Thanks
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Autogenerate Collision

Post by Norbo »

Graphics are completely separate from physics in implementation and concept. Any graphical model can be used represent any physical collision shape. There's nothing special about an EntityModel in the GettingStartedDemo, it's just a simplistic box model drawer. If the graphical model should not scale based on the box collision scale, then do not apply the scale transform to the graphical model :)
Keta
Posts: 8
Joined: Tue May 22, 2012 6:56 pm

Re: Autogenerate Collision

Post by Keta »

Well, this is really embarrassing. Thanks a ton, got it working now! xD

Edit:
Now I got the model moving when buttons are press. However... How do I make it move "forward" after I've rotated it? Currently, when I press the Up key, it moves forward. But if I turn it with Left or Right key, it still keeps going the same way as before when pressing Up.
In other words, it follows one axis, even though I rotate it. So, question is, how would I go about making it move in the direction it is facing?

Thanks
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Autogenerate Collision

Post by Norbo »

The entity.OrientationMatrix.Forward property returns the current forward direction based on the orientation. (Some background: this value is extracted directly from the matrix since it is aligned with one of the three axes of in the orthonormal basis composing the rotation matrix. However, you can get the exact same direction by transforming a 'local space' Vector3.Forward by the entity.OrientationMatrix to compute a 'world space' forward, it's just faster and easier to grab it directly!)
Keta
Posts: 8
Joined: Tue May 22, 2012 6:56 pm

Re: Autogenerate Collision

Post by Keta »

Norbo wrote:The entity.OrientationMatrix.Forward property returns the current forward direction based on the orientation. (Some background: this value is extracted directly from the matrix since it is aligned with one of the three axes of in the orthonormal basis composing the rotation matrix. However, you can get the exact same direction by transforming a 'local space' Vector3.Forward by the entity.OrientationMatrix to compute a 'world space' forward, it's just faster and easier to grab it directly!)
Could you elaborate that a bit more please...? I don't quite understand what you mean. What I get out of those two lines are exactly the same :s

Thanks
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Autogenerate Collision

Post by Norbo »

The 'grab it directly' refers to the entity.OrientationMatrix.Forward, if that's where the confusion was.

Here's the two distinct approaches mentioned to get the same result:
1) Vector3 worldForward = Matrix3X3.Transform(Vector3.Forward, entity.OrientationMatrix);

To figure out what direction 'forward' is, the entity's orientation must be considered. If we start in the entity's local space where Vector3.Forward is aligned with what we call the forward direction of the shape, and then we transform the shape using some world space transform that includes an orientation, we must apply that same orientation to bring the local space forward direction into world space.

2) Vector3 worldForward = entity.OrientationMatrix.Forward;

This version takes advantage of the fact that an orientation matrix contains the three axes that define the orthonormal basis. Vector3.Forward in local space is transformed to entity.OrientationMatrix.Forward in world space. It's a special case; the axis we want is already right there in the matrix, we can just pull it out without any extra work.
khaled
Posts: 1
Joined: Sun Nov 23, 2014 9:18 pm

Re: Autogenerate Collision

Post by khaled »

Norbo wrote:If you want to use a mesh, there exists a MobileMesh class which will represent every triangle in a source model with greater speed than the direct equivalent CompoundBody.

However, approximations are usually preferred for performance reasons. Approximations usually involve either a set of simple primitive shapes like BoxShapes or a slightly greater level of fidelity with multiple ConvexHullShapes. There is no current automatic generator for approximations. There do exist some algorithms out there which can be used to create approximate convex hull decompositions of models (like HACD), but I have not had the time to implement such a thing.
A new version of the HACD library is available here http://kmamou.blogspot.ca/2014/11/v-hac ... -here.html
Please, give it a try and let me know what you think.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Autogenerate Collision

Post by Norbo »

Very nice work! I noticed the discussion over on the Bullet forums :)

For anyone seeking automatic decomposition for use with BEPUphysics: while I doubt I will find the time to do a first party integration, using this library to generate decompositions offline and then importing the resulting hulls will work quite well.
Post Reply