[FIXED] Boundingbox 3D not reading any collision

Hello everyone,

I have created a boundinbox for my model. I want to check if this intersects with the camera I have.
But I cannot get it to work. If i put a breakpoint on the if statement for containment it breaks at launch but if i put it at the action done when in collides it never breaks.

Thanks in advance,
THis is the code for creating the boundingbox

public List<BoundingBox> MeshModel(Model impType)
{
    List<BoundingBox> boundingBoxes= new List<BoundingBox>();
   
    Matrix[] transforms = new Matrix[impType.Bones.Count];
    impType.CopyAbsoluteBoneTransformsTo(transforms);

    foreach (ModelMesh mesh in impType.Meshes)
    {
        Matrix meshTransform = transforms[mesh.ParentBone.Index];
        boundingBoxes.Add(BuildBoundingBox(mesh, meshTransform));
    }
    return boundingBoxes;
}


private BoundingBox BuildBoundingBox(ModelMesh mesh, Matrix meshTransform)
{
  
        // Create initial variables to hold min and max xyz values for the mesh
        Vector3 meshMax = new Vector3(float.MinValue);
        Vector3 meshMin = new Vector3(float.MaxValue);

        foreach (ModelMeshPart part in mesh.MeshParts)
        {
            // The stride is how big, in bytes, one vertex is in the vertex buffer
            // We have to use this as we do not know the make up of the vertex
            int stride = part.VertexBuffer.VertexDeclaration.VertexStride;

            VertexPositionNormalTexture[] vertexData = new VertexPositionNormalTexture[part.NumVertices];
            part.VertexBuffer.GetData(part.VertexOffset * stride, vertexData, 0, part.NumVertices, stride);

            // Find minimum and maximum xyz values for this mesh part
            Vector3 vertPosition = new Vector3();

            for (int i = 0; i < vertexData.Length; i++)
            {
                vertPosition = vertexData[i].Position;

                // update our values from this vertex
                meshMin = Vector3.Min(meshMin, vertPosition);
                meshMax = Vector3.Max(meshMax, vertPosition);
            }
        }

        // transform by mesh bone matrix
        meshMin = Vector3.Transform(meshMin, meshTransform);
        meshMax = Vector3.Transform(meshMax, meshTransform);

        // Create the bounding box
        BoundingBox box = new BoundingBox(meshMin, meshMax);
        return box;
}`

And this is the code to check if it collides
foreach (BoundingBox boxes in MeshModel(impModel)) { if (boxes.Contains(camera.Position)== ContainmentType.Contains) { hud.Health -= 10; } }

I do not understand why this isn’t working.

Thanks in advance,

Jeromer

is your model at 0,0,0?

Because your Bounding Box is only transformed by the parent’s bone transformation, but not by the parent’s position. You would have to add that.

Apart from that, I think it looks correct, I am not sure about the creation with Vector3.Min, but I think it should work.

Note: That the Model.Meshes have a boundingSphere created for them during compile time already. This one I use for frustum culling etc. so you can check against that to see what works best for you.

Thanks for the reply,

No the model is not at 0,0,0. But i have done it differently. Far less elegant. But ill have a look at eh parents position as well, thanks.

Currently i have just created my own boundingbox for the camera with my own parameters and used that. But if i get this to work it is far better. Thanks i will have a go at it.

Just need to figure out how to use the parent’s position. But i should be able to do that.

Yup, that’s good the way it is. I also think the problem is that the code is not using the parent transform.

Oke,

I am still stuck. Sorry for my incompetence on the subject. 3D boundingboxes in this way are completely new for me. I understand how they work. But not entirely how to create them via this method. This is something I saw on the internet as being the “correct” method. But as you guys said i mussed the parent transform, i have been trying to add that. But to my But the parentbone for position. I thought i could just call on the parent transform and get the position. But apparently i am doing something wrong. I just can’t get it to work. So any help or advice is greatly appreciated.

Jeromer

You should draw the bounding box so you can see it.

Thanks everyone, after drawing the boundingboxes it indeed showed me that the position of the model wasn’t being updated. I wrote a somewhat different method but i was taking the position of the model in account now.

Once again thanks for all the help

Jeromer