public static DebugInfo GetDebugInfo(BEPUphysics.Entities.Entity DisplayedObject, Color color) { var wrappedShape = DisplayedObject.CollisionInformation.Shape as WrappedShape; if (wrappedShape == null) throw new ArgumentException("Wrong shape type"); var points = new List<Vector3>(); Vector3 max; var direction = new Vector3(); float angleChange = MathHelper.TwoPi / NumSamples; for (int i = 1; i < NumSamples / 2 - 1; i++) { float phi = MathHelper.PiOver2 - i * angleChange; var sinPhi = (float)Math.Sin(phi); var cosPhi = (float)Math.Cos(phi); for (int j = 0; j < NumSamples; j++) { float theta = j * angleChange; direction.X = (float)Math.Cos(theta) * cosPhi; direction.Y = sinPhi; direction.Z = (float)Math.Sin(theta) * cosPhi; wrappedShape.GetLocalExtremePoint(direction, out max); points.Add(max); } } List<VertexPositionColor> vertices = new List<VertexPositionColor>(); List<short> indices = new List<short>(); wrappedShape.GetLocalExtremePoint(Toolbox.UpVector, out max); points.Add(max); wrappedShape.GetLocalExtremePoint(Toolbox.DownVector, out max); points.Add(max); var hullTriangleVertices = new List<Vector3>(); var hullTriangleIndices = new List<int>(); Toolbox.GetConvexHull(points, hullTriangleIndices, hullTriangleVertices); //The hull triangle vertices are used as a dummy to get the unnecessary hull vertices, which are cleared afterwards. hullTriangleVertices.Clear(); foreach (int i in hullTriangleIndices) { hullTriangleVertices.Add(points[i]); } for (short i = 0; i < hullTriangleVertices.Count; i += 3) { Vector3 normal = Vector3.Normalize(Vector3.Cross(hullTriangleVertices[i + 2] - hullTriangleVertices[i], hullTriangleVertices[i + 1] - hullTriangleVertices[i])); vertices.Add(new VertexPositionColor(hullTriangleVertices[i], color)); vertices.Add(new VertexPositionColor(hullTriangleVertices[i + 1], color)); vertices.Add(new VertexPositionColor(hullTriangleVertices[i + 2], color)); indices.Add(i); indices.Add((short)(i + 1)); indices.Add((short)(i + 2)); } DebugInfo DebugInfo = new DebugInfo(); DebugInfo.vertices = vertices.ToArray(); DebugInfo.indices = indices.ToArray(); return DebugInfo; }
public static DebugInfo GetDebugInfo(BEPUphysics.Entities.Entity DisplayedObject, Color color) { ConeShape coneShape = DisplayedObject.CollisionInformation.Shape as ConeShape; if (coneShape == null) throw new ArgumentException("Wrong shape type."); float verticalOffset = -coneShape.Height / 4; float angleBetweenFacets = MathHelper.TwoPi / NumSides; float radius = coneShape.Radius; //Create the vertex list var topVertexPosition = new Vector3(0, coneShape.Height + verticalOffset, 0); List<VertexPositionColor> vertices = new List<VertexPositionColor>(); List<short> indices = new List<short>(); for (int i = 0; i < NumSides; i++) { float theta = i * angleBetweenFacets; var position = new Vector3((float)Math.Cos(theta) * radius, verticalOffset, (float)Math.Sin(theta) * radius); Vector3 offset = topVertexPosition - position; Vector3 normal = Vector3.Normalize(Vector3.Cross(Vector3.Cross(offset, Vector3.Up), offset)); //Top vertex vertices.Add(new VertexPositionColor(topVertexPosition, color)); //Sloped vertices vertices.Add(new VertexPositionColor(position, color)); //Bottom vertices vertices.Add(new VertexPositionColor(position, color)); } //Create the index list for (short i = 0; i < vertices.Count; i += 3) { //Each iteration, the loop advances to the next vertex 'column.' //Four triangles per column (except for the four degenerate cap triangles). //Sloped Triangles indices.Add(i); indices.Add((short)(i + 1)); indices.Add((short)((i + 4) % vertices.Count)); //Bottom cap triangles. var nextIndex = (short)((i + 5) % vertices.Count); if (nextIndex != 2) //Don't add cap indices if it's going to be a degenerate triangle. { indices.Add((short)(i + 2)); indices.Add(2); indices.Add(nextIndex); } } DebugInfo DebugInfo = new DebugInfo(); DebugInfo.vertices = vertices.ToArray(); DebugInfo.indices = indices.ToArray(); return DebugInfo; }
public static DebugInfo GetDebugInfo(BEPUphysics.Entities.Entity DisplayedObject, Color color) { MobileMeshShape shape = DisplayedObject.CollisionInformation.Shape as MobileMeshShape; var tempVertices = new VertexPositionNormalTexture[shape.TriangleMesh.Data.Vertices.Length]; for (int i = 0; i < shape.TriangleMesh.Data.Vertices.Length; i++) { Vector3 position; shape.TriangleMesh.Data.GetVertexPosition(i, out position); tempVertices[i] = new VertexPositionNormalTexture( position, Vector3.Zero, Vector2.Zero); } List<VertexPositionColor> vertices = new List<VertexPositionColor>(); List<short> indices = new List<short>(); for (int i = 0; i < shape.TriangleMesh.Data.Indices.Length; i++) { indices.Add((short)shape.TriangleMesh.Data.Indices[i]); } for (int i = 0; i < indices.Count; i += 3) { int a = indices[i]; int b = indices[i + 1]; int c = indices[i + 2]; Vector3 normal = Vector3.Normalize(Vector3.Cross( tempVertices[c].Position - tempVertices[a].Position, tempVertices[b].Position - tempVertices[a].Position)); tempVertices[a].Normal += normal; tempVertices[b].Normal += normal; tempVertices[c].Normal += normal; } for (int i = 0; i < tempVertices.Length; i++) { tempVertices[i].Normal.Normalize(); vertices.Add(new VertexPositionColor(tempVertices[i].Position,color)); } DebugInfo DebugInfo = new DebugInfo(); DebugInfo.vertices = vertices.ToArray(); DebugInfo.indices = indices.ToArray(); return DebugInfo; }
public static DebugInfo GetDebugInfo(BEPUphysics.Entities.Entity DisplayedObject, Color color) { var convexHullShape = DisplayedObject.CollisionInformation.Shape as ConvexHullShape; if (convexHullShape == null) throw new ArgumentException("Wrong shape type."); var hullTriangleVertices = new List<Vector3>(); var hullTriangleIndices = new List<int>(); Toolbox.GetConvexHull(convexHullShape.Vertices, hullTriangleIndices, hullTriangleVertices); //The hull triangle vertices are used as a dummy to get the unnecessary hull vertices, which are cleared afterwards. hullTriangleVertices.Clear(); foreach (int i in hullTriangleIndices) { hullTriangleVertices.Add(convexHullShape.Vertices[i]); } List<VertexPositionColor> vertices = new List<VertexPositionColor>(); List<short> indices = new List<short>(); Vector3 normal; for (short i = 0; i < hullTriangleVertices.Count; i += 3) { normal = Vector3.Normalize(Vector3.Cross(hullTriangleVertices[i + 2] - hullTriangleVertices[i], hullTriangleVertices[i + 1] - hullTriangleVertices[i])); vertices.Add(new VertexPositionColor(hullTriangleVertices[i], color)); vertices.Add(new VertexPositionColor(hullTriangleVertices[i + 1], color)); vertices.Add(new VertexPositionColor(hullTriangleVertices[i + 2],color)); indices.Add(i); indices.Add((short)(i + 1)); indices.Add((short)(i + 2)); } DebugInfo DebugInfo = new DebugInfo(); DebugInfo.vertices = vertices.ToArray(); DebugInfo.indices = indices.ToArray(); return DebugInfo; }
public static DebugInfo GetDebugInfo(BEPUphysics.Entities.Entity DisplayedObject, Color color) { var cylinderShape = DisplayedObject.CollisionInformation.Shape as CylinderShape; if (cylinderShape == null) throw new ArgumentException("Wrong shape type."); float verticalOffset = cylinderShape.Height / 2; float angleBetweenFacets = MathHelper.TwoPi / NumSides; float radius = cylinderShape.Radius; List<VertexPositionColor> vertices = new List<VertexPositionColor>(); List<short> indices = new List<short>(); //Create the vertex list for (int i = 0; i < NumSides; i++) { float theta = i * angleBetweenFacets; float x = (float)Math.Cos(theta) * radius; float z = (float)Math.Sin(theta) * radius; //Top cap vertices.Add(new VertexPositionColor(new Vector3(x, verticalOffset, z), color)); //Top part of body vertices.Add(new VertexPositionColor(new Vector3(x, verticalOffset, z), color)); //Bottom part of body vertices.Add(new VertexPositionColor(new Vector3(x, -verticalOffset, z), color)); //Bottom cap vertices.Add(new VertexPositionColor(new Vector3(x, -verticalOffset, z), color)); } //Create the index list //The vertices are arranged a little nonintuitively. //0 is part of the top cap, 1 is the upper body, 2 is lower body, and 3 is bottom cap. for (short i = 0; i < vertices.Count; i += 4) { //Each iteration, the loop advances to the next vertex 'column.' //Four triangles per column (except for the four degenerate cap triangles). //Top cap triangles var nextIndex = (short)((i + 4) % vertices.Count); if (nextIndex != 0) //Don't add cap indices if it's going to be a degenerate triangle. { indices.Add(i); indices.Add(nextIndex); indices.Add(0); } //Body triangles nextIndex = (short)((i + 5) % vertices.Count); indices.Add((short)(i + 1)); indices.Add((short)(i + 2)); indices.Add(nextIndex); indices.Add(nextIndex); indices.Add((short)(i + 2)); indices.Add((short)((i + 6) % vertices.Count)); //Bottom cap triangles. nextIndex = (short)((i + 7) % vertices.Count); if (nextIndex != 3) //Don't add cap indices if it's going to be a degenerate triangle. { indices.Add((short)(i + 3)); indices.Add(3); indices.Add(nextIndex); } } DebugInfo DebugInfo = new DebugInfo(); DebugInfo.vertices = vertices.ToArray(); DebugInfo.indices = indices.ToArray(); return DebugInfo; }
public static DebugInfo GetDebugInfo(BEPUphysics.Entities.Entity DisplayedObject, Color color) { var capsuleShape = DisplayedObject.CollisionInformation.Shape as CapsuleShape; if (capsuleShape == null) throw new ArgumentException("Wrong shape type."); var n = new Vector3(); var offset = new Vector3(0, capsuleShape.Length / 2, 0); float angleBetweenFacets = MathHelper.TwoPi / NumSides; float radius = capsuleShape.Radius; //Create the vertex list //Top List<VertexPositionColor> vertices = new List<VertexPositionColor>(); List<short> indices = new List<short>(); vertices.Add(new VertexPositionColor(new Vector3(0, radius + capsuleShape.Length / 2, 0),color)); //Upper hemisphere for (int i = 1; i <= NumSides / 4; i++) { float phi = MathHelper.PiOver2 - i * angleBetweenFacets; var sinPhi = (float)Math.Sin(phi); var cosPhi = (float)Math.Cos(phi); for (int j = 0; j < NumSides; j++) { float theta = j * angleBetweenFacets; n.X = (float)Math.Cos(theta) * cosPhi; n.Y = sinPhi; n.Z = (float)Math.Sin(theta) * cosPhi; vertices.Add(new VertexPositionColor(n * radius + offset, color)); } } //Lower hemisphere for (int i = NumSides / 4; i < NumSides / 2; i++) { float phi = MathHelper.PiOver2 - i * angleBetweenFacets; var sinPhi = (float)Math.Sin(phi); var cosPhi = (float)Math.Cos(phi); for (int j = 0; j < NumSides; j++) { float theta = j * angleBetweenFacets; n.X = (float)Math.Cos(theta) * cosPhi; n.Y = sinPhi; n.Z = (float)Math.Sin(theta) * cosPhi; vertices.Add(new VertexPositionColor(n * radius - offset, color)); } } //Bottom vertices.Add(new VertexPositionColor(new Vector3(0, -radius - capsuleShape.Length / 2, 0), color)); //Create the index list for (int i = 0; i < NumSides; i++) { indices.Add((short)(vertices.Count - 1)); indices.Add((short)(vertices.Count - 2 - i)); indices.Add((short)(vertices.Count - 2 - (i + 1) % NumSides)); } for (int i = 0; i < NumSides / 2 - 1; i++) { for (int j = 0; j < NumSides; j++) { int nextColumn = (j + 1) % NumSides; indices.Add((short)(i * NumSides + nextColumn + 1)); indices.Add((short)(i * NumSides + j + 1)); indices.Add((short)((i + 1) * NumSides + j + 1)); indices.Add((short)((i + 1) * NumSides + nextColumn + 1)); indices.Add((short)(i * NumSides + nextColumn + 1)); indices.Add((short)((i + 1) * NumSides + j + 1)); } } for (int i = 0; i < NumSides; i++) { indices.Add(0); indices.Add((short)(i + 1)); indices.Add((short)((i + 1) % NumSides + 1)); } DebugInfo DebugInfo = new DebugInfo(); DebugInfo.vertices = vertices.ToArray(); DebugInfo.indices = indices.ToArray(); return DebugInfo; }
public static DebugInfo GetDebugInfo(BEPUphysics.Entities.Entity DisplayedObject, Color color) { var boxShape = DisplayedObject.CollisionInformation.Shape as BoxShape; var boundingBox = new BoundingBox( new Vector3(-boxShape.HalfWidth, -boxShape.HalfHeight, -boxShape.HalfLength), new Vector3(boxShape.HalfWidth, boxShape.HalfHeight, boxShape.HalfLength)); Vector3[] corners = boundingBox.GetCorners(); List<VertexPositionColor> vertices = new List<VertexPositionColor>(); List<short> indices = new List<short>(); vertices.Add(new VertexPositionColor(corners[0], color)); vertices.Add(new VertexPositionColor(corners[1], color)); vertices.Add(new VertexPositionColor(corners[2], color)); vertices.Add(new VertexPositionColor(corners[3], color)); indices.Add(0); indices.Add(1); indices.Add(2); indices.Add(0); indices.Add(2); indices.Add(3); vertices.Add(new VertexPositionColor(corners[1], color)); vertices.Add(new VertexPositionColor(corners[2], color)); vertices.Add(new VertexPositionColor(corners[5], color)); vertices.Add(new VertexPositionColor(corners[6], color)); indices.Add(4); indices.Add(6); indices.Add(7); indices.Add(4); indices.Add(7); indices.Add(5); vertices.Add(new VertexPositionColor(corners[4], color)); vertices.Add(new VertexPositionColor(corners[5], color)); vertices.Add(new VertexPositionColor(corners[6], color)); vertices.Add(new VertexPositionColor(corners[7], color)); indices.Add(9); indices.Add(8); indices.Add(11); indices.Add(9); indices.Add(11); indices.Add(10); vertices.Add(new VertexPositionColor(corners[0], color)); vertices.Add(new VertexPositionColor(corners[3], color)); vertices.Add(new VertexPositionColor(corners[4], color)); vertices.Add(new VertexPositionColor(corners[7], color)); indices.Add(14); indices.Add(12); indices.Add(13); indices.Add(14); indices.Add(13); indices.Add(15); vertices.Add(new VertexPositionColor(corners[0], color)); vertices.Add(new VertexPositionColor(corners[1], color)); vertices.Add(new VertexPositionColor(corners[4], color)); vertices.Add(new VertexPositionColor(corners[5], color)); indices.Add(16); indices.Add(19); indices.Add(17); indices.Add(16); indices.Add(18); indices.Add(19); vertices.Add(new VertexPositionColor(corners[2], color)); vertices.Add(new VertexPositionColor(corners[3], color)); vertices.Add(new VertexPositionColor(corners[6], color)); vertices.Add(new VertexPositionColor(corners[7], color)); indices.Add(21); indices.Add(20); indices.Add(22); indices.Add(21); indices.Add(22); indices.Add(23); DebugInfo DebugInfo = new DebugInfo(); DebugInfo.vertices = vertices.ToArray(); DebugInfo.indices = indices.ToArray(); return DebugInfo; }