# In a Sonic-esque platformer, how to tell if the chracter is upside-down or not?

In a Sonic-esque platform game, how do you know if the player's character is currently upside down? Let's say the character is running to the left:

At a certain point the character will begin to supposedly run on the ceiling. How do I keep track of this since the chracter is touching both the ceiling and the floor of the tunnel?know

I need some way to know where the character's feet are positioned, keep track of it and allow this to change when the character is moving fast across curved surfaces.

• It would be useful if you could include more information. For what purpose do you need to know which direction the feet are facing? How is character movement handled? – Entity Nov 1 '14 at 18:34
• @TheAdamGaskins Well, if the character is upside down, pressing the left button will move you right and vice versa. – wolfdawn Nov 1 '14 at 21:38

While the character is running, mark the surface that he's running on as the active running surface. As long as the speed does not reduce to 0 (or reduced, depending on your mechanics), you're on the same surface, no matter if the head touches another surface.

To detect where you go next, if your surface is stored as a grid, store last grid cell that you walked, as well as the current grid, and by checking the local neighbourhood (say 3x3 grid cells) around the current grid cell, you can figure out where the feet should be positioned next, and you can also derive the velocity, etc from that.

You could have two collision sensors: one just under the character's feet and one just over it's head. Then it would be a matter of checking the positions of both sensors when they are both triggered and if the foot sensor is higher than the head sensor then you know that the body is upside down.

• The player character is a ball that continuously spins. I need to deduct orientation from input and interaction with the environment. – wolfdawn Nov 1 '14 at 18:01

You can use the dot product of a world up vector with an up vector relative to the player. If both of these vectors are normalized, you're results will be between 1 and -1. With 2d vectors the dot product is calculated by taking the product of the x components and adding them to the product of the y components.

Given the vectors A = (x,y) and B = (X,Y). The dot product is x*X + y*Y. So, if the world up vector is (0, 1), we can see that:

if player up vector is (0, 1), then the dot product of the up vectors is 0*0 + 1*1 = 1, and, if the player up vector is (0, -1), then the dot product is 0*0 + -1*1 = -1. This tells us the player is upside down. If the player is standing on the side of a wall (1, 0), perpendicular to the world up vector. Then the dot product is 1*0 + 0*1 = 0.

So, to recap, if the dot product is 1, the player is up right, as the dot product approaches 0, the player becomes more and more perpendicular to the world, and is perpendicular with the result is 0. As the dot product turns negative, then player's head starts angling towards the ground, and the player is completely upside down when the dot product is -1.