XNA 3d how get models primitives

  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    3d Models
Click For Summary
The discussion revolves around implementing triangle-by-triangle collision detection in a 3D environment using XNA. The user shares a code snippet designed to extract the base triangles from a 3D model, which involves transforming vertex positions based on the model's world matrix. Initially, the user encounters an error related to array size when attempting to retrieve vertex data. However, they later identify that the issue stemmed from forgetting to UV map the model, which is essential for the code to function correctly. This highlights the importance of ensuring all models are properly prepared before implementing collision detection algorithms.
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:
Technology 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.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...