- #1
Superposed_Cat
- 388
- 5
Hi ll, I am currently working on 3d in xna. i have ismple collision detcetion working but I want to do triangle by triangle collision detection.How would I get a models base triangles?
I have this adapted from online to fit in with my game:
but i get an "The array is not the correct size for the amount of data requested" error. any help apreciated.
I have this adapted from online to fit in with my game:
Code:
public struct TriangleVertex
{
public VertexPositionNormalTexture I0;
public VertexPositionNormalTexture I1;
public VertexPositionNormalTexture I2;
}
public static List<TriangleVertex> GetModelsPrimitives(Entity E)
{//all models passed though here MUST be UV mapped
Matrix theMatrix = E.GetWorldModel();
List<TriangleVertex> h = new List<TriangleVertex>();
foreach (ModelMesh mesh in Game1.Models[E.ModelKey].Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[part.VertexBuffer.VertexCount];
part.VertexBuffer.GetData<VertexPositionNormalTexture>(vertices);
ushort[] drawOrder = new ushort[part.IndexBuffer.IndexCount];
part.IndexBuffer.GetData<ushort>(drawOrder);
VertexPositionNormalTexture[] vertices2 = new VertexPositionNormalTexture[drawOrder.Length];
for (int i = 0; i < drawOrder.Length; i++)
{//sort vertices2 according to draw order
vertices2[i] = vertices[drawOrder[i]];
}
for (int i = 0; i < vertices2.Length; i += 3)
{//group it into triangles
TriangleVertex tv = new TriangleVertex();
tv.I0.Position = Vector3.Transform(vertices2[i].Position, theMatrix);
tv.I1.Position = Vector3.Transform(vertices2[i + 1].Position, theMatrix);
tv.I2.Position = Vector3.Transform(vertices2[i + 2].Position, theMatrix);
h.Add(tv);
}
}
}
return h;
}
Last edited by a moderator: