0
\$\begingroup\$

I have the following problem: I have two GameObjects (one is static, the other is the player). Both of them have one BoundingBox that surrounds them, and a List that contains more BoundingBoxes for a more detailed collision detection. I use the follwing code when the player moves:

/// <summary>
    /// Checks if this Game Object blocks the specific boundingbox.
    /// </summary>
    /// <param name="box">Box.</param>
    public virtual bool Blocks(BoundingBox box)
    {
        if (BoundingBoxes.Count.Equals(0))
        {
            return !(this.SurroundBox.Contains(box) == ContainmentType.Disjoint);
        }

        bool blocked = false;
        BoundingBoxes.ForEach(b =>
        {
            if (b.Contains(box) != ContainmentType.Disjoint)
                blocked = true;
        });

        return blocked;
    }

    /// <summary>
    /// Checks if this Game Object blocks any of boxes in the specific list of boundingboxes.
    /// </summary>
    /// <param name="boxes">Boxes.</param>
    public virtual bool Blocks(List<BoundingBox> boxes)
    {
        bool blocked = false;
        boxes.ForEach(b =>
        {
            if (this.Blocks(b))
                blocked = true;
        });

        return blocked;
}

If I just use the Surroundboxes, everything works fine. If I start using the Lists, blocked never gets true. I'm pretty shure my BoundingBoxes aren't the problem (visually they clearly overlap!). Here's a screenshot, where b is the red boundingbox from the tree and the lightblue is the one from the player. Blue boundingboxes are the surroundingboxes. The right image is just there to demonstrate that the player's boundingbox is also 3D. (In this case each GameObjects List of BoundingBoxes only contains one BoundingBox that is slightly smaller than their SurroundingBox) enter image description here

Shouldn't if (b.Contains(box) != ContainmentType.Disjoint) now return true? If not, wheres my mistake? Any comment/help is appreciated!

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

Nevermind, I found out how to solve it: I should use BoundingBox.CreateFromPointsinstead of the standard constructor. The standard constructor gets confused if Minand Max aren't actually Min and Max. BoundingBox.CreateFromPoints takes care of this.

\$\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.