Reputation: 409
I am building an auto-run platforming game. I have code so that if the character collides with a wall, they are pushed back until they are able to get past the wall. Unfortunately, this means that when the character tries to jump on an object, he just gets pushed backward. How do I discern when a character is on top of an object, from when it is on the side?
My Current Algorithm (c-ish because i like c-ish syntax):
if (wall.Top < (character.Top + character.Height) &&
wall.Left < (character.Left + character.Width) &&
(wall.Top + wall.Height) > character.Top &&
(wall.Left + wall.Width) > character.Left) { #code here# }
Upvotes: 0
Views: 28
Reputation: 8215
Four conditions are too much here. Depending on the heights of the character and that of the wall, the 1st and the 2nd condition eliminates cases where actually your character is on the wall.
Assuming that you consider the character to be on top of the wall if its center bottom is on top of the wall, you could do the following:
character.Center = character.Left + character.Width / 2;
if (character.Center > wall.Left &&
character.Center < wall.Left + wall.Width &&
character.Top - character.Height > wall.Top) { ... }
Upvotes: 1