Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

XNA 3d how get models primitives

  1. Jun 21, 2014 #1
    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 (Text):

     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: Jun 21, 2014
  2. jcsd
  3. Jul 17, 2014 #2
    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?
     
  4. Jul 19, 2014 #3
    it's fien found my issue. i needed forgot to UV map the model. apologies. stupid non-code related error.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook