Page 1 of 1

Weird MobileMesh Issue

Posted: Sun Jun 26, 2011 2:06 pm
by drjaydenm
Hi again Norbo,

I have gotten around to using the MobileMesh as my terrain as its better suited my engine to use all entities. When I create the mobile mesh, the whole mesh seems to be about 10 units or so down the Y axis and a few units on the X and Z axis. The weird thing is that the mesh will collide perfectly fine as a static mesh but not as a mobile mesh. I have checked whether there are any scaling or position changes happeing but there are not. To make the issue even weirder, the debug drawer draws the wireframe over the 3d model in the correct position. So you would think that as the debug drawer shows the collision mesh in the correct position, it would work but no, it is offset. I have played around with the affine transform as well, setting it to a zero vector3 and the objects position but they make the offsets even worse. Thanks for any help :)

P.s. If you cannot think of any reasons as to why this would be happening, I can spend some time to make a repo app.

Re: Weird MobileMesh Issue

Posted: Sun Jun 26, 2011 4:23 pm
by Norbo
As an entity, the mobile mesh is transformed using the Entity.WorldTransform property which locates the center of mass of the mesh. Since the center of mass is very likely not the origin of the mesh defined at content time, it will appear offset without applying the appropriate local transform.

Since the entity's Position property will reflect the center of mass immediately after initialization, you can use it to figure out what the offset from the content origin is. Also note that the MobileMeshShape provides additional constructors which explicitly output the center and other information if you are not using the MobileMesh prefab entity type.

The MobileMesh case for the ModelDrawer should handle this correctly. Make sure that the MobileMesh entity itself is being passed into the ModelDrawer, and not the MobileMesh.CollisionInformation.Shape.TriangleMesh or something.

Re: Weird MobileMesh Issue

Posted: Mon Jun 27, 2011 12:43 am
by drjaydenm
I played around with the entity sent to the model drawer and it is now displaying the collision mesh correctly :) But now to fix the other half of the problem. How can I accurately change the center of mass to match that of the center of my model? Thanks

Re: Weird MobileMesh Issue

Posted: Mon Jun 27, 2011 12:50 am
by Norbo
The position of the MobileMesh entity is put at the center of mass of the mesh when the entity is constructed; this is the offset. I would recommend transforming the graphical model to the collision model, instead of the other way around.

You can also get the offset from the MobileMeshShape constructor if the entity is being constructed without the use of the MobileMesh prefab entity type.

Re: Weird MobileMesh Issue

Posted: Mon Jun 27, 2011 1:51 am
by drjaydenm
Sorry to bother you again but what code would you use to gain access to the center of mass offset from the original center position?

Re: Weird MobileMesh Issue

Posted: Mon Jun 27, 2011 2:30 am
by Norbo
The center of mass of an entity is its Position property. The original position would be the origin (0,0,0). The offset is just mobileMesh.Position - origin, so mobileMesh.Position (right after initialization, anyway).

Re: Weird MobileMesh Issue

Posted: Mon Jun 27, 2011 4:53 am
by drjaydenm
To properly align the model with the mobile mesh, i had to retrieve the affine transform for the mobile mesh and then apply that to the world transform of my model which made it finally properly align. This is still slightly offset by tiny amounts (like 2mm) due to float rounding I think. This is the code I used to retrieve it.

Code: Select all

MobileMeshShape shape = PhysicsObject.Entity.CollisionInformation.Shape as MobileMeshShape;
ModelOffset = shape.Transform.Translation;
For some reason the affine transform is not zero, even though it is set as a zero vector3 when the mobile mesh is created. I am guessing this affine transform is being set to the offset from the original origin to the current center of mass as this correction is aligning the model and the mobile mesh. Is there a cleaner way to align the model and mesh or is this the only way? Thanks

Re: Weird MobileMesh Issue

Posted: Mon Jun 27, 2011 6:57 pm
by Norbo
For some reason the affine transform is not zero, even though it is set as a zero vector3 when the mobile mesh is created. I am guessing this affine transform is being set to the offset from the original origin to the current center of mass as this correction is aligning the model and the mobile mesh.
All shapes do recenter themselves onto their local center of mass, yes. That's why the mobileMesh.Position is not at zero- to guarantee that the final collision location is the same. Note that the mobileMesh.Position is equal to the negative translation of the transform.

Chances are, that tiny gap you're seeing is actually a collision margin. That's expected; you can hide it with graphics if you'd like. An explanation of collision margins can be found here: http://www.bepu-games.com/forums/viewto ... ?f=4&t=409 That's a bit old, but the summary is that the collision margin helps precision and allows the use of some speedy algorithms. Note that some shapes have 'internal' collision margins, which means they are contained in the dimensions of the shape rather than expanding outward. These shapes include Box, Sphere, Capsule, and Cylinder. The default collision margin for convex shapes is 0.04 units.

Re: Weird MobileMesh Issue

Posted: Mon Jun 27, 2011 10:15 pm
by drjaydenm
Thanks for the really detailed explanation, it really helps to understand, big companies should aspire to provide tour level of support :D The 0.04 does sound right but the does not matter as if you don't have debug drawing on, you will not notice it.