Building a ConvexHull out of an XNA ModelMeshPart

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
speciesUnknown
Posts: 20
Joined: Mon Aug 01, 2011 12:31 am

Building a ConvexHull out of an XNA ModelMeshPart

Post by speciesUnknown »

Hi,

I'm trying to build a convex hull using the data from a ModelMeshPart. I am taking the Model and for each part of the Model, generate a convex hull. So far, i have done this:

Code: Select all

		public ConvexHullShape getConvexHull()
		{
			if (this.convex_hull == null)
			{
				List<Vector3> verts = new List<Vector3>(1000);
				foreach (var i in this.renderable_mesh.MeshParts)
				{
					Vector3[] lulz;

					lulz = new Vector3[i.NumVertices];

					i.VertexBuffer.GetData<Vector3>(lulz, i.VertexOffset, i.NumVertices);

					foreach (var point in lulz)
					{
						verts.Add(point);
					}
					
				}

				Vector3 computed_origin;
				this.convex_hull = new ConvexHullShape(verts, out computed_origin);
			}

			return this.convex_hull;
		}
For some odd reason, the computed origin is offset in the X dimension by 0.5, even though the model I am generating this from is totally symmetrical (i modelled half of the vehicle and then mirrored all the geometry) and is centered around the origin, which leads me to believe that I'm only getting half the vertices.

This causes a problem when I try to use the ConvexHull on a vehicle, as it crushes all the suspension on one side and the vehicle tips over.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Building a ConvexHull out of an XNA ModelMeshPart

Post by Norbo »

The BEPUphysicsDrawer (within the BEPUphysics solution alongside the BEPUphysicsDemos project) can be used to draw physics objects. I would recommend visualizing the convex hull to see exactly where it is and what it looks like.

There's also a convenience method for grabbing the vertices from a Model object built-in: TriangleMesh.GetVerticesAndIndicesFromModel. Checking the results against it may provide some more information.
speciesUnknown
Posts: 20
Joined: Mon Aug 01, 2011 12:31 am

Re: Building a ConvexHull out of an XNA ModelMeshPart

Post by speciesUnknown »

using TriangleMesh.AddMesh seems to have done the trick - it fills the list<Vector3> with the points correctly translated from the parent object. Now my tank drives like it should once again. This method has been so useful when building a convex hull, I would recommend it be moved somewhere more general; its purpose Isn't solely limited to triangle meshes. If you hadn't suggested GetVerticesAndIndicesFromModel then I would never have found it.

I'm getting more and more impressed by BEPU as I continue to use it.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Building a ConvexHull out of an XNA ModelMeshPart

Post by Norbo »

I would recommend it be moved somewhere more general
I suppose I could put it somewhere like an XNAModelHelper static class; that may make it easier for people porting the engine to non-XNA platforms to prune it out as well. Or, possibly, the Toolbox, though it would just be one of many, many different miscellaneous methods then.
I'm getting more and more impressed by BEPU as I continue to use it.
Thanks :)
Post Reply