protected override void LoadContent() { model = Game.Content.Load<Model>("convexhull"); if (cvhs == null) { List<JVector> jvecs = new List<JVector>(); List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>(); ExtractData(jvecs, indices, model); int[] convexHullIndices = JConvexHull.Build(jvecs, JConvexHull.Approximation.Level6); List<JVector> hullPoints = new List<JVector>(); for (int i = 0; i < convexHullIndices.Length; i++) { hullPoints.Add(jvecs[convexHullIndices[i]]); } cvhs = new ConvexHullShape(hullPoints); } body = new RigidBody(cvhs); body.Tag = BodyTag.DontDrawMe; }
private Shape CreateShape() { Shape shape = null; boundingBox = new BoundingBox(); List<Geometry> geom = RenderUtil.GatherGeometry(GameObject); List<Mesh> meshes = new List<Mesh>(); List<Matrix4f> matrices = new List<Matrix4f>(); Vector3f mytrans = GameObject.GetUpdatedWorldTranslation(); if (GameObject is Geometry) { Geometry g_obj = (Geometry)GameObject; Mesh m = g_obj.Mesh; meshes.Add(m); Transform ttransform = new Transform(); ttransform.SetTranslation(g_obj.GetUpdatedWorldTranslation()); ttransform.SetRotation(g_obj.GetUpdatedWorldRotation()); ttransform.SetScale(g_obj.GetUpdatedWorldScale()); Matrix4f matrix = ttransform.GetMatrix(); BoundingBox tmpBB = m.CreateBoundingBox(); matrices.Add(matrix); boundingBox.Extend(tmpBB); } for (int i = 0; i < geom.Count; i++) { Geometry g = geom[i]; if (g != GameObject) { Mesh m = g.Mesh; meshes.Add(m); Transform ttransform = new Transform(); ttransform.SetTranslation(g.GetUpdatedWorldTranslation().Subtract(mytrans)); ttransform.SetRotation(g.GetUpdatedWorldRotation()); ttransform.SetScale(g.GetUpdatedWorldScale()); Matrix4f matrix = ttransform.GetMatrix(); BoundingBox tmpBB = m.CreateBoundingBox(matrix); matrices.Add(matrix); boundingBox.Extend(tmpBB); } } if (physicsShape == PhysicsWorld.PhysicsShape.StaticMesh) { List<JVector> jvec = new List<JVector>(); List<TriangleVertexIndices> tv = new List<TriangleVertexIndices>(); List<Vector3f> vertexPositions = new List<Vector3f>(); for (int m = 0; m < meshes.Count; m++) { for (int v = 0; v < meshes[m].vertices.Count; v++) { Vector3f myvec = meshes[m].vertices[v].GetPosition().Multiply(matrices[m]); jvec.Add(new JVector(myvec.x, myvec.y, myvec.z)); } for (int i = 0; i < meshes[m].indices.Count; i += 3) { tv.Add(new TriangleVertexIndices(meshes[m].indices[i + 2], meshes[m].indices[i + 1], meshes[m].indices[i])); } } Octree oct = new Octree(jvec, tv); TriangleMeshShape trimesh = new TriangleMeshShape(oct); shape = trimesh; oct = null; jvec.Clear(); tv.Clear(); } else if (physicsShape == PhysicsWorld.PhysicsShape.ConvexMesh) { List<JVector> jvec = new List<JVector>(); List<Vector3f> vertexPositions = new List<Vector3f>(); for (int m = 0; m < meshes.Count; m++) { for (int v = 0; v < meshes[m].indices.Count; v++) { Vector3f vec = meshes[m].vertices[meshes[m].indices[v]].GetPosition().Multiply(matrices[m]); jvec.Add(new JVector(vec.x, vec.y, vec.z)); } } ConvexHullShape hullShape = new ConvexHullShape(jvec); shape = hullShape; } else if (physicsShape == PhysicsWorld.PhysicsShape.Box) { /* Mesh boxMesh = MeshFactory.CreateCube(boundingBox.Min, boundingBox.Max); List<JVector> jvec = new List<JVector>(); List<TriangleVertexIndices> tv = new List<TriangleVertexIndices>(); for (int v = 0; v < boxMesh.vertices.Count; v++) { Vector3f myvec = boxMesh.vertices[v].GetPosition(); jvec.Add(new JVector(myvec.x, myvec.y, myvec.z)); } for (int i = 0; i < boxMesh.indices.Count; i += 3) { tv.Add(new TriangleVertexIndices(boxMesh.indices[i], boxMesh.indices[i + 1], boxMesh.indices[i + 2])); } Octree oct = new Octree(jvec, tv); TriangleMeshShape trimesh = new TriangleMeshShape(oct);*/ shape = new BoxShape(boundingBox.Extent.x, boundingBox.Extent.y, boundingBox.Extent.z); // shape = trimesh; // tv.Clear(); // jvec.Clear(); // oct = null; // boxMesh = null; } meshes.Clear(); matrices.Clear(); geom.Clear(); return shape; }
public void setPhysMesh(string pMeshName, JMatrix mOrientation) { pBoxList.Add(pMeshName); Mesh pboxMesh = gameWindow.meshLoader.getMesh(pMeshName); ConvexHullShape objShape = new ConvexHullShape(GenericMethods.FromOpenTKVecArToJVecList(pboxMesh.positionVboData)); setPhysMesh(objShape); }
protected void RecalculateObject(IGameContext gameContext, IRenderContext renderContext) { // Recalculate vertex caches. this.m_CachedHitTestVertexPositionNormalTextures = this.CalculateVertexPositionNormalTextures(true).Select(x => x.Position).ToArray(); this.m_CachedNoHitTestVertexPositionNormalTextures = this.CalculateVertexPositionNormalTextures(false); // Recalculate vertex and index buffers. if (this.m_IndexBuffer != null) { this.m_IndexBuffer.Dispose(); } if (this.m_VertexBuffer != null) { this.m_VertexBuffer.Dispose(); } this.m_IndexBuffer = new IndexBuffer( renderContext.GraphicsDevice, typeof(short), this.MeshIndicies.Length, BufferUsage.None); this.m_IndexBuffer.SetData(this.MeshIndicies); var vertexes = this.m_CachedNoHitTestVertexPositionNormalTextures; this.m_VertexBuffer = new VertexBuffer( renderContext.GraphicsDevice, typeof(VertexPositionNormalTexture), vertexes.Length, BufferUsage.None); this.m_VertexBuffer.SetData(vertexes); // Recalculate physics object. var world = gameContext.World as RoomEditorWorld; if (world != null) { if (this.m_RigidBody != null) { this.m_JitterWorld.RemoveBody(this.m_RigidBody); } var shape = new ConvexHullShape(this.GetVertexes().Select(x => x.ToJitterVector()).ToList()); this.m_JitterWorld = world.JitterWorld; this.m_RigidBody = new RigidBody(shape) { IsStatic = true, Position = new JVector(0, 0, 0) - shape.Shift }; // The shape vertexes include X, Y, Z position offset, so // the shift moves the object completely into the correct // position. this.m_JitterWorld.AddBody(this.m_RigidBody); this.m_PendRecalculation = false; } else { // We can't update the physics entity, so pend until we can. this.m_PendRecalculation = true; } }
/// <summary> /// A really stupid parser for convex decomposed files made by testhacd.exe (see Other\hacdtest) /// </summary> public List<ConvexHullShape> BuildFromHACDTestObjFile(string path) { List<ConvexHullShape> shapes = new List<ConvexHullShape>(); string[] lines = File.ReadAllLines(path); Char[] splitter = new Char [] {' '}; List<JVector> convexPoints = new List<JVector>(); for (int i = 0; i < lines.Length; i++) { string line = lines[i]; if (line.StartsWith("v")) { string[] values = line.Split(splitter); JVector vertex = new JVector(float.Parse(values[1], NumberFormatInfo.InvariantInfo), float.Parse(values[2], NumberFormatInfo.InvariantInfo), float.Parse(values[3], NumberFormatInfo.InvariantInfo)); convexPoints.Add(vertex * 5f); } if(line.StartsWith("#")) { if(convexPoints.Count > 0) { List<JVector> copyVertex = new List<JVector>(convexPoints); convexPoints.Clear(); ConvexHullShape cvhs = new ConvexHullShape(copyVertex); if(cvhs.Mass > 0.001f) shapes.Add(cvhs); } } } return shapes; }
public static ConvexHullShape BuildConvexHullShape(Model model) { var vertices = ExtractVertices(model); var hull = new ConvexHullShape(vertices); return hull; }
void IConstructable.Construct(IDictionary<string, string> param) { var type = param["type"]; switch (type) { case "trimesh": var physData = ResourceFactory.LoadAsset<PhysicsData>(param["physData"]); Shape shape = new TriangleMeshShape(physData.Octree); Body = new RigidBody(shape) { Material = { Restitution = 0f, KineticFriction = 0f } }; break; case "hull": physData = ResourceFactory.LoadAsset<PhysicsData>(param["physData"]); shape = new ConvexHullShape(physData.Vertices); Body = new RigidBody(shape); break; case "sphere": shape = new SphereShape(float.Parse(param["radius"], CultureInfo.InvariantCulture)); Body = new RigidBody(shape); break; case "box": var d = param["size"].ConvertToVector(); var offset = param.Get("offset", "0;0;0").ConvertToVector(); shape = new BoxShape(2.0f * d.ToJVector()); Body = new RigidBody(shape) { Position = offset.ToJVector() }; break; case "capsule": var height = float.Parse(param["height"], CultureInfo.InvariantCulture); var radius = float.Parse(param["radius"], CultureInfo.InvariantCulture); shape = new CapsuleShape(height, radius); Body = new RigidBody(shape) { Position = JVector.Backward * (0.5f * height + radius), Orientation = JMatrix.CreateRotationX(MathHelper.PiOver2) }; break; default: throw new Exception("Unknown shape: " + type); } Body.IsStatic = Convert.ToBoolean(param.Get("static", "false")); Body.Material.KineticFriction = 0.5f; Body.Material.StaticFriction = 0.5f; Body.Material.Restitution = 0.5f; }