XNA 3d how get models primitives

  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    3d Models
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 4K views
Superposed_Cat
Messages
388
Reaction score
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:
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;
        }
but i get an "The array is not the correct size for the amount of data requested" error. any help apreciated.
 
Last edited by a moderator:
Physics news on Phys.org
I'm sorry you are not generating any responses at the moment. Is there any additional information you can share with us? Any new findings?
 
it's fien found my issue. i needed forgot to UV map the model. apologies. stupid non-code related error.