Logic help? (collisions)

Could you please help me with this logic, this code is going to be the 4 sided collision for every 4 sided object in my 2drpg. this is what ive got soo far,

void collision() {
  //LEFT WALL**
  if (dir == 3 && locx+50 > house1x && locx < house1x+house1w && locy+64 < house1y && locy > house1y+house1h) {
    locx -= width/512;
  }

  //RIGHT WALL**
  if (dir == 2 && locx < house1x+house1w && locx+50 > house1x && locy+64 < house1y && locy > house1y+house1h) {
    locx += width/512;
  }

  //TOP WALL
  if (dir == 1 && locy+64 > house1y && locy < house1y+house1h && locx+50 > house1x && locx < house1x+house1w) {
    locy -= width/512;
  }

  //BOTTOM WALL
  if (dir == 4 && locy+64 > house1y && locy < house1y+house1h && locx+50 > house1x && locx < house1x+house1w) {
    locy += width/512;
  }
}

the top and bottom walls work but the left and right do not?

Tagged:

Answers

  • I think you will need to post the whole code. You have a lot of constants in there and I have no idea what they mean.

  • Let me ask a different question... How would you do it if you were me?

  • Is your game grid based or free movement? I'm guessing it is an aerial view 2D game

  • you're using width for both your up/down and left/right code. should two of them be using height?

  • Left / right wall collision doesn't need to check for y coordinates, I think. Idem for vertical vs. x.

  • Ugghh width/512 is the movement speed

Sign In or Register to comment.