XNA 3d how get models primitives

  • Thread starter Superposed_Cat
  • Start date
  • Tags
    3d Models
In summary, the conversation is about implementing triangle by triangle collision detection in a 3D game using XNA. The person has adapted some code from online and is trying to implement it, but is getting an error related to UV mapping the model. They later realize their mistake and the issue is resolved.
  • #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:
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
  • #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?
 
  • #3
it's fien found my issue. i needed forgot to UV map the model. apologies. stupid non-code related error.
 

Related to XNA 3d how get models primitives

1. How can I import 3D models into XNA?

To import 3D models into XNA, you can use the built-in content pipeline. This pipeline allows you to import various types of 3D files, such as .fbx, .obj, and .x, and convert them into a format that can be used in XNA. You can access the content pipeline by right-clicking on your project in the Solution Explorer and selecting "Add" > "Existing Item". From there, you can select your 3D model file and it will be imported into your project.

2. How do I create primitives in XNA?

XNA provides several built-in classes for creating 3D primitives, such as spheres, cubes, and cylinders. These classes are located in the Microsoft.Xna.Framework.Graphics namespace and can be accessed by creating an instance of the desired primitive and passing in the necessary parameters, such as size and position. You can then draw the primitive using the Draw method in your game's Draw method.

3. How can I manipulate and transform 3D models in XNA?

XNA provides a variety of methods for manipulating and transforming 3D models. You can use the various translation, rotation, and scaling methods in the Microsoft.Xna.Framework.Matrix class to move and resize your models. Additionally, XNA also provides a ModelBone class, which allows you to manipulate individual bones within a model to create more complex animations.

4. Can I use custom 3D models in XNA?

Yes, XNA allows you to use custom 3D models in your game. As mentioned before, you can import these models using the content pipeline. However, it's important to note that not all 3D models may be compatible with XNA. It's recommended to use models with a maximum of 65,000 triangles and no more than 255 different materials in order to ensure optimal performance in your game.

5. Are there any resources or tutorials available for learning how to use 3D models in XNA?

Yes, there are many resources and tutorials available for learning how to use 3D models in XNA. The official XNA website provides a variety of documentation and tutorials, and there are also numerous online communities and forums where you can find help and support from other XNA users. Additionally, there are also many online tutorials and videos available on websites such as YouTube and Udemy that can help you learn how to use 3D models in XNA.

Back
Top