0
\$\begingroup\$

This is my collision detection function in the maze game im working at. I never made a game before and I dont have that much programming experience.

The question I keep asking me continously is why this isn't working. The Solids are instances of the class Solids with rectangular shape(X(Top-left-corner),Y(Top-left-corner),width,height) which are stored in a List< T >. The function below iterates through the List and calls the second function below. checkForCollision is called every FrameUpdate.

Can somebody please help me. I'm becoming desperate :)

private void checkForCollision()
        {
            foreach (Solids wall in level1.WallObjects)
            {
                if (IsCollidedSolids(ref Pl1, wall))
                {
                    Pl1.OnCollision();
                    break;
                }
            }
        }


public bool IsCollidedSolids(ref Player Pl ,Solids act)
        {
            if (Pl.X+Pl.width >= act.X && Pl.Y >= act.Y - act.height)
                return true;
            if (Pl.X + Pl.width >= act.X && Pl.Y -Pl.height <= act.Y)
                return true;
            if (Pl.X <= act.X + act.width && Pl.Y - Pl.height <= act.Y)
                return true;
            if (Pl.X <= act.X + act.width && Pl.Y >= act.Y - act.height)
                return true;
            else
                return false;
        }
\$\endgroup\$
1
  • \$\begingroup\$ What happens that means it doesn't work? What do you expect to happen? What tests do you run to determine if it is working? \$\endgroup\$ May 15 '14 at 2:54
0
\$\begingroup\$

First, seems like you are confusing your coordinate system. If its origin is top-left corner of screen, then its axes pointing towards bottom and right respectively. Thus to find bottom of a axis aligned box add height to top, instead of substractng it.

Second, logic is just wrong. First condition states: "if top-right point of first object is farther from coordinate center than bottom-left point of second object". There still can be gap between objects.

It may be easier to check for conditions that guarantee objects are not interecting, f.e.:

if(a.top > b.bottom) return false;
if(b.top > a.bottom) return false;
if(a.left > b.right) return false;
if(b.left > a.right) return false;
return true;

Or we may think more carefully about uderlying structure of axis aligned box. It may be represented as two ranges, one per each axis. To check if two boxes are intersecting is to check that two pairs of ranges are intersecting. Now this is trivial to google or remember from math class.

bool RangesIntersecting(start1, end1, start2, end2)
{
    return max(start1, start2) < min(end1, end2);
}

bool AABBsIntersecting(a, b)
{
    return RangesIntersecting(a.x, a.x + a.width, b.x, b.x + b.width)
        && RangesIntersecting(a.y, a.y + a.height, b.y, b.y + b.height);
{
\$\endgroup\$
1
  • \$\begingroup\$ thx for the help:) you brought me in the right direction. I solved my problem with a tiled Map now. And I am using OpenGL so the Y axis goes upwards, forgot to say that. \$\endgroup\$
    – bushuo
    May 15 '14 at 20:02

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.