# My logic in collision detection in diagonal code correct? [closed]

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))
{
return;
}

dx = -2;
dy = -2;
move(dx, dy);
}
``````
-

## closed as too localized by msell, Byte56♦, Josh Petrie♦, bummzack, AnkoMay 8 '13 at 10:57

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

sorry for the horrible code block edit. I looked for some directions for this. – cyberspace009 May 7 '13 at 19:53
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. – aaaaaaaaaaaa May 7 '13 at 20:17
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. – Byte56 May 7 '13 at 20:39

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.

-