I made this code to detect collision from the diagonal(top left). I'm trying to find the problem on what I'm doing wrong. Is it necessary to have a diagonal collision for the wall? Player class:

if(key == KeyEvent.VK_LEFT && key == KeyEvent.VK_UP)
      if(checkWallColl(World.player, World.topLeftColl))

       dx = -2;
       dy = -2;
       move(dx, dy);
  • \$\begingroup\$ sorry for the horrible code block edit. I looked for some directions for this. \$\endgroup\$ Commented May 7, 2013 at 19:53
  • 2
    \$\begingroup\$ If your code does not work as intended, in what way does it fail? Normally you do not need special code to handle orthogonal cases, but there is not much else I can say from looking just at that snip. \$\endgroup\$ Commented May 7, 2013 at 20:17
  • 1
    \$\begingroup\$ You need to tell us the problem you're having. The code is correct if it does what you want. Use the debugger, track down your problem that way. \$\endgroup\$
    – House
    Commented May 7, 2013 at 20:39

1 Answer 1


You should describe what isn't working with your code, is it not properly detecting collision? Because then you should look at your collision detection function.

But in general, for 2d you should separate the checking of horizontal and vertical collision. If you do that then there won't be "diagonal collision", just a horizontal collision and a vertical collision.

So for a very basic pseudo example:

dx = 0;

dy = 0;

If left pressed and no horizontal collision at x - 2 then dx = -2;

If up pressed and no vertical collision at y - 2 then dy = -2;

doMove(dx, dy);

So if the player is pressing up and left, and there's only a horizontal collision, the player will still move up. This will allow snug fits into a corner with a more robust movement system. This isn't the best way to go with implementing movement, but it will serve your purposes with the way you've chosen.


Not the answer you're looking for? Browse other questions tagged .