PageRenderTime 222ms CodeModel.GetById 67ms app.highlight 12ms RepoModel.GetById 68ms app.codeStats 1ms

/PhysicsEngines/Jitter/Helpers.cs

#
C# | 76 lines | 54 code | 6 blank | 16 comment | 4 complexity | 1d48e0c1997879b23e176e09954df8a5 MD5 | raw file
 1using System.Collections.Generic;
 2using Delta.Rendering.Models;
 3using Delta.Utilities.Datatypes;
 4using Jitter.Collision;
 5using Jitter.Collision.Shapes;
 6using Jitter.LinearMath;
 7
 8namespace Delta.PhysicsEngines.Jitter
 9{
10	public static class Helpers
11	{
12		#region CreateFrom (Static)
13		/// <summary>
14		/// Helper method used for creation of Jitter TriangleMeshShape by 
15		/// populating data with vertices and indices gathered from out Mesh.
16		/// </summary>
17		/// <param name="mesh">The mesh.</param>
18		/// <param name="localSpaceMatrix">The local space matrix.</param>
19		/// <param name="invertTriangles">
20		/// if set to <c>true</c> we invert winding order of triangles.
21		/// </param>
22		/// <returns>
23		/// New created jitter TriangleMeshShape created from Delta engine mesh.
24		/// </returns>
25		public static TriangleMeshShape CreateFrom(Mesh mesh,
26			Matrix localSpaceMatrix, bool invertTriangles)
27		{
28			// extract the data from the physics mesh
29			List<JVector> triangleVertex = new List<JVector>();
30			List<TriangleVertexIndices> triangleVertexIndices =
31				new List<TriangleVertexIndices>();
32
33			// apply the local transform of the mesh so we get correct coordinates
34			Matrix TransMat = localSpaceMatrix;
35
36			JVector tempVector;
37			for (int num = 0; num < mesh.Geometry.Data.NumberOfUsedVertices; num++)
38			{
39				Vector transformed = Vector.Transform(
40					mesh.Geometry.Data.GetPosition(num), TransMat);
41				JitterDatatypesMapping.Convert(ref transformed, out tempVector);
42				triangleVertex.Add(tempVector);
43			}
44
45			// set inverted tris if needed
46			ushort[] Indices = mesh.Geometry.Data.Indices;
47			if (invertTriangles)
48			{
49				for (int i = 0; i < Indices.Length; i = i + 3)
50				{
51					triangleVertexIndices.Add(new TriangleVertexIndices(
52						Indices[i + 2],
53						Indices[i + 1],
54						Indices[i]));
55				}
56			}
57			else
58			{
59				for (int i = 0; i < Indices.Length; i = i + 3)
60				{
61					triangleVertexIndices.Add(new TriangleVertexIndices(
62						Indices[i],
63						Indices[i + 1],
64						Indices[i + 2]));
65				}
66			}
67
68			// create the triangle mesh of the level and add it to the physics world
69			Octree ocTree = new Octree(triangleVertex, triangleVertexIndices);
70			TriangleMeshShape triangleMesh = new TriangleMeshShape(ocTree);
71
72			return triangleMesh;
73		}
74		#endregion
75	}
76}