Hitbox/collision issue


(Nate ) #1

Hey guys! So I have a player and when he does not have a power up, he is one size, and when he has a power up he is larger. Also just the way the sprite is when the player is walking left or right slightly shifts, so when my player is facing the right the hit box is slightly different from when the player is facing the left.

In my update method this is what I do to handle the minor shift in hitbox:

		if (!hasPowerup && playerisfacing == 1)
		{
			setHitbox(40, 60, -3, -20);
		}
		
		if (!hasPowerup && playerisfacing == 2)
		{
			setHitbox(40, 60, -16, -20);
		}

The problem I have is, say I jump towards a wall, in this game the walls do not allow the player to pass through them, but if I jump towards a wall, and change my player direction the hitbox changes like it should, but if I am touching the wall while the hit box is changing, my player clips into the wall and gets stuck… I am assuming it has to do with me changing the values of the hitbox?

What do you guys think? Is there a simpler way to do this?

Thank you!


(Bora Kasap) #2

actually i don’t know exactly what it does but have you tried setting “true” moveBy’s “sweep” boolean?


(JP Mortiboys) #3

Firstly make sure that you’re only changing the hitbox when the turn event happens - not on every frame.

That done, it’s can be as simple as shunting the player in the right direction when the turn happens:

if (playerIsTurning && !hasPowerup) {
  if (playerisfacing == 1) {
    setHitbox(40, 60, -3, -20);
    while(collide("wall", x, y)) { x++; }
  }
  else if (playerisfacing == 2) {
    setHitbox(40, 60, -16, -20);
    while(collide("wall", x, y)) { x--; }
  }
}

(Note that I might have got the “++” and the “–” in the wrong slots - swap them if necessary)


(Nate ) #4

Worked beautifully! Thank you so much Raptor! :smiley: