Collision Detection in Java against two objects

XXXconanXXX

Cocktails anyone?
Reaction score
284
Been stuck on this and I can't figure out how to get the character to hit the block, and still be able to move after he hits it.

Here's the code for the move method:

Code:
public void move() {
            
            if (dx != -1 ) {
                if (Left + dx <= 150) {
                    Left = Left + dx;
                } else {
                    x = x + dx;
                    nx2 = nx2 + dx;
                    nx =  nx + dx;
                }
            } else if(Left + dx > 0) {
                    Left = Left + dx;
            }
}

nx and nx2 scroll the background, Left is to make sure the char can't go back to previous areas of the level.

Problem is, when I check for as long as the character's X isn't the same as the block's X, move() he'll get stuck when he hits the block and is not able to move. Very frustrating. Any ideas?
 

UndeadDragon

Super Moderator
Reaction score
447
The general way to do collision (how I've learnt anyway :p) is to save the start position of the moving object at the start of the move function, then check the collisions (no moving yet), then if it is no colliding, then you can do the movement. It might stop it from getting stuck inside the object.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top