in discussion General / Help » TriangleMeshObject Error
cant get my triangle mesh objec to create a trianglemesh. here is my trainglemeshobject code
using System;
using System.Collections.Generic;
using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Math;
using JigLibX.Physics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace qkGames
{
// A physics object that simulates all the individual polygons in a model
public class TriangleMeshObject : PhysicsObject
{
// The generated mesh
TriangleMesh triangleMesh;
//string modelName;
public List<Vector3> vertexList;
// Constructors
public List<TriangleVertexIndices> indexList;
public TriangleMeshObject()
: base()
{
Setup(null, Vector3.Zero, Vector3.Zero);
}
public TriangleMeshObject(string ModelName, Vector3 Position, Vector3 Rotation)
: base()
{
Setup(ModelName, Position, Rotation);
}
public TriangleMeshObject(string ModelName, Vector3 Position, Vector3 Rotation,
GameScreen Parent)
: base(Parent)
{
Setup(ModelName, Position, Rotation);
}
// Sets up the object
void Setup(string ModelName, Vector3 Position, Vector3 Rotation)
{
Model Model = Engine.Content.Load<Model>(ModelName);
Body = new Body();
CollisionSkin = new CollisionSkin(null);
//Body.CollisionSkin = CollisionSkin;
//this.modelName = ModelName;
//CollisionSkin.RemoveAllPrimitives();
triangleMesh = new TriangleMesh();
vertexList = new List<Vector3>();
indexList =
new List<TriangleVertexIndices>();
ExtractData(vertexList, indexList, Model);
//triangleMesh.CreateMesh(
triangleMesh.CreateMesh(vertexList, indexList, 4, 1.0f);
CollisionSkin.AddPrimitive(triangleMesh,
new MaterialProperties(0.8f, 0.7f, 0.6f));
PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(CollisionSkin);
//Mass = Mass;
this.Position = Position;
CollisionSkin.ApplyLocalTransform(new Transform(-Position, Matrix.Identity));
// CollisionSkin.
//this.EulerRotation = Rotation;
}
// Sets the model being simulated, extracts vertices, etc.
public void SetModel(string ModelName)
{
}
// Extracts the neccesary information from a model to
// simulate physics on it
public void ExtractData(List<Vector3> vertices, List<TriangleVertexIndices> indices,Model model)
{
Matrix[] bones_ = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(bones_);
foreach (ModelMesh mm in model.Meshes)
{
Matrix xform = bones_[mm.ParentBone.Index];
foreach (ModelMeshPart mmp in mm.MeshParts)
{
int offset = vertices.Count;
Vector3[] a = new Vector3[mmp.NumVertices];
mm.VertexBuffer.GetData<Vector3>(mmp.StreamOffset + mmp.BaseVertex * mmp.VertexStride,
a, 0, mmp.NumVertices, mmp.VertexStride);
for (int i = 0; i != a.Length; ++i)
Vector3.Transform(ref a[i], ref xform, out a[i]);
vertices.AddRange(a);
if (mm.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits)
throw new Exception(
String.Format("Model uses 32-bit indices, which are not supported."));
short[] s = new short[mmp.PrimitiveCount * 3];
mm.IndexBuffer.GetData<short>(mmp.StartIndex * 2, s, 0, mmp.PrimitiveCount * 3);
JigLibX.Geometry.TriangleVertexIndices[] tvi = new JigLibX.Geometry.TriangleVertexIndices[mmp.PrimitiveCount];
for (int i = 0; i != tvi.Length; ++i)
{
tvi[i].I0 = s[i * 3 + 2] + offset;
tvi[i].I1 = s[i * 3 + 1] + offset;
tvi[i].I2 = s[i * 3 + 0] + offset;
}
indices.AddRange(tvi);
}
}
}
}
}
triangleMesh.CreateMesh(vertexList, indexList, 4, 1.0f);
is throwing a
System.ArrayTypeMismatchException was unhandled
Message="Source array type cannot be assigned to destination array type."
Source="mscorlib"
StackTrace:
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Collections.Generic.List`1.CopyTo(T[] array, Int32 arrayIndex)
at System.Collections.Generic.List`1.CopyTo(T[] array)
at JigLibX.Geometry.Octree.AddTriangles(List`1 _positions, List`1 _tris)
at JigLibX.Geometry.TriangleMesh.CreateMesh(List`1 vertices, List`1 triangleVertexIndices, Int32 maxTrianglesPerCell, Single minCellSize)







