#### /PhysicsEngines/Jitter/Helpers.cs

#
C# | 76 lines | 54 code | 6 blank | 16 comment | 4 complexity | 1d48e0c1997879b23e176e09954df8a5 MD5 | raw file
Possible License(s): Apache-2.0
``````
using System.Collections.Generic;

using Delta.Rendering.Models;

using Delta.Utilities.Datatypes;

using Jitter.Collision;

using Jitter.Collision.Shapes;

using Jitter.LinearMath;

namespace Delta.PhysicsEngines.Jitter

{

public static class Helpers

{

#region CreateFrom (Static)

/// <summary>

/// Helper method used for creation of Jitter TriangleMeshShape by

/// populating data with vertices and indices gathered from out Mesh.

/// </summary>

/// <param name="mesh">The mesh.</param>

/// <param name="localSpaceMatrix">The local space matrix.</param>

/// <param name="invertTriangles">

/// if set to <c>true</c> we invert winding order of triangles.

/// </param>

/// <returns>

/// New created jitter TriangleMeshShape created from Delta engine mesh.

/// </returns>

public static TriangleMeshShape CreateFrom(Mesh mesh,

Matrix localSpaceMatrix, bool invertTriangles)

{

// extract the data from the physics mesh

List<JVector> triangleVertex = new List<JVector>();

List<TriangleVertexIndices> triangleVertexIndices =

new List<TriangleVertexIndices>();

// apply the local transform of the mesh so we get correct coordinates

Matrix TransMat = localSpaceMatrix;

JVector tempVector;

for (int num = 0; num < mesh.Geometry.Data.NumberOfUsedVertices; num++)

{

Vector transformed = Vector.Transform(

mesh.Geometry.Data.GetPosition(num), TransMat);

JitterDatatypesMapping.Convert(ref transformed, out tempVector);

triangleVertex.Add(tempVector);

}

// set inverted tris if needed

ushort[] Indices = mesh.Geometry.Data.Indices;

if (invertTriangles)

{

for (int i = 0; i < Indices.Length; i = i + 3)

{

triangleVertexIndices.Add(new TriangleVertexIndices(

Indices[i + 2],

Indices[i + 1],

Indices[i]));

}

}

else

{

for (int i = 0; i < Indices.Length; i = i + 3)

{

triangleVertexIndices.Add(new TriangleVertexIndices(

Indices[i],

Indices[i + 1],

Indices[i + 2]));

}

}

// create the triangle mesh of the level and add it to the physics world

Octree ocTree = new Octree(triangleVertex, triangleVertexIndices);

TriangleMeshShape triangleMesh = new TriangleMeshShape(ocTree);

return triangleMesh;

}

#endregion

}

}

``````