Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Boundingbox 3D not reading any collision

$
0
0

@JeroenT wrote:

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

Posts: 4

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles