1
\$\begingroup\$

I am making a game very similar to Tank Trouble. I cannot get t the tank to collide with the maze in the map.

Let tx and ty = the tank x and y value.
Let x and y = the walls x and y value.
Let h and w = the walls height and width.

public boolean collidesWith(double tx, double ty)
{
    if ((tx - 55 > x) && (tx + 55 < x + w) && (ty - 55 > y) && (ty + 55 < y + h))
    {
        return true;
    }

    return false;
}
\$\endgroup\$
  • \$\begingroup\$ your code checks if the tank is completely contained inside the wall. Is that what you want? Also, it's confusing that tx,ty are the center of the tank, but x,y is the top-left corner of the wall. Use center for both or topleft for both. \$\endgroup\$ – CeeJay May 31 '13 at 12:42
  • \$\begingroup\$ This is a debugging question. What do you think is the problem? Have you tried solving it? Do you know what the debugger is and how to use it? \$\endgroup\$ – Anko May 31 '13 at 22:46
0
\$\begingroup\$

One way is to just return false whenever you find a case that makes it impossible for the two to intersect.

public boolean collidesWith(double tx, double ty) {
    if ((tx + 55 < x) || (tx - 55 > x + w) ||
        (ty + 55 < y) || (ty + 55 > y + h)) {
        return false;
    }

    return true;
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ this will always return true unless the wall is contained entirely inside the tank \$\endgroup\$ – CeeJay May 31 '13 at 14:29
  • \$\begingroup\$ You're right. At first I had only noticed the &&s and assumed that was the problem. Updated answer. \$\endgroup\$ – rodrigo-silveira May 31 '13 at 14:43
  • \$\begingroup\$ I think this new version is correct :) \$\endgroup\$ – CeeJay May 31 '13 at 15:41

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