0
\$\begingroup\$

I'm working on integrating some the 3D models I import into my physics system by trying to take their "size" and use that as the side for objects within my physics system. When I say size, I'm referring essentially to a bounding box size around my model. Here's an example of a test level layout I'm designing:

Level Layout

All of the cubes below are 1x1 units in Blender (measuring from the center), so I've been able to just code in those assumptions. However, it starts to get more interesting when you consider the slope and smaller platform I have.

For the slopes, I've found an easy hack for the time being (as long as their size remains 1x1). However, the platforms are throwing me off. A few things to call out with the platform model:

  • The model itself is centered at (0,0,0) and no scaling was done on the model itself in Object Mode
  • I scaled down the Z of the model in Edit Mode, while the model itself remains at (1,1,1) scaling

I'm now trying to figure out how to get some form of size for the model. For what I'm looking for, if we assume the cubes I have are bounds box size of (1,1,1), then this platform would be a bounding box size of (1, 1, .25). I would assume that I would need to look at the model's meshes to determine this, but I'm not entirely sure

Any help on this would be appreciated. Thanks!

\$\endgroup\$
1
\$\begingroup\$

After doing some more digging for a few hours, I found an old solution from XNA that still works in MonoGame: "How do I create a bounding boxes with XNA 4.0?

I've tweaked the solution above to simply get me the min and max points WITHOUT using the world transform (as I don't care where it's positioned, just the default size). This is set up as a class function for my Model wrapper class, but can obviously be tweaked to take a Model as a parameter:

    public BoundingBox GetBounds()
    {
        Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
        Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);

        foreach (ModelMesh mesh in this.Model.Meshes)
        {
            foreach (ModelMeshPart meshPart in mesh.MeshParts)
            {
                int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride;
                int vertexBufferSize = meshPart.NumVertices * vertexStride;

                int vertexDataSize = vertexBufferSize / sizeof(float);
                float[] vertexData = new float[vertexDataSize];
                meshPart.VertexBuffer.GetData<float>(vertexData);

                for (int i = 0; i < vertexDataSize; i += vertexStride / sizeof(float))
                {
                    Vector3 vertex = new Vector3(vertexData[i], vertexData[i + 1], vertexData[i + 2]);
                    min = Vector3.Min(min, vertex);
                    max = Vector3.Max(max, vertex);
                }
            }
        }

        return new BoundingBox(min, max);
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.