Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
    }
share|improve this question

closed as too localized by msell, Byte56, Josh Petrie, bummzack, Anko May 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
2  
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. –  eBusiness May 7 '13 at 20:17
1  
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

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer

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