XNA 3d how get models primitives

  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    3d Models
Click For Summary
SUMMARY

This discussion focuses on implementing triangle-by-triangle collision detection in XNA using a custom method to extract model primitives. The user shared a code snippet that defines a TriangleVertex struct and a GetModelsPrimitives method to retrieve UV-mapped triangles from a model. The error encountered, "The array is not the correct size for the amount of data requested," was resolved by ensuring the model was UV mapped correctly before processing. This highlights the importance of proper model preparation in 3D game development.

PREREQUISITES
  • Understanding of XNA Framework and its 3D capabilities
  • Familiarity with UV mapping techniques in 3D modeling
  • Knowledge of C# programming, particularly with structs and lists
  • Experience with collision detection algorithms in game development
NEXT STEPS
  • Research advanced collision detection techniques in XNA
  • Learn about optimizing UV mapping for 3D models
  • Explore the use of Matrix.Transform for vertex manipulation
  • Investigate error handling in C# for game development
USEFUL FOR

Game developers working with XNA, particularly those focused on 3D graphics and collision detection, will benefit from this discussion.

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.