# How to rotate an object towards the walking direction?

I'm working on a game where you move a object on a game surface of cells, every time you move this object it steps to a cell, up, down, left or to the right. I have been trying to find out a way to rotate the object soo it is facing the moving direction. The rotation should be 90 degrees. It's one thing if the object is going from Up to Left and then rotate it 90 degrees, but what if object is going from Up to Down, then it need to rotate 180 degrees. I need some help with this issue to solve this in a simple and good way? Thanks!

This is the code for moving upwards and the other moving directions are similar. I'm using a short delay, so that the player can't move to fast on the game surface.

    // Upp
public void Up()
{
if (milliSeconds1 >= walkingTimer)
{
Position = new Vector2(Position.X, Position.Y - walkingSpeed);
milliSeconds1 = 0;
}
}


And in the Update:

Position = Position + Direction * 1;


EDIT: I solved this by myself! I just used radian values for each directions! :)

• possible duplicate of Rotate entity to match current velocity – bummzack Aug 13 '12 at 7:22
• @3D-kreativ When you solve your own questions, please post your solution as an answer and mark it as correct. – doppelgreener Aug 23 '12 at 0:30

It seems you already solved this, but I figured I could try to provide some info in the case it breaks, or potentially another way to do it.

Anyhow, your cell/grid approach sounds to me like what most RPGS would use (generally with the 3/4 view). In such games, your character can face four directions, where you face in the direction you moved last. If this IS the case, skip to the next paragraph. Otherwise, if you are making some sort of smooth rotation (i.e instead of a solid facing, the character/token/whatever_moves smoothly turns ver several render frames), then what you can ideally do is, find the difference between the DESIRED angle (should be one of four for the cardinal directions) in terms of what you would need to incriment to go closer to said angle, and incriment a small portion of it. I still don't get how the formula works, but I beleive:

return ((((argument0 - argument1) mod 360) + 540) mod 360) - 180;

• will find the difference needed in terms of your current direction (arg0) to the desired (arg1). I have never done such smooth turning myself, though, so sorry if that is incorrect.

On the other hand, if you simply want to face the direction you move in, this is actually quite simple. My first recomendation would be to, if you don't already have something of this sort, have a function like move(direction), if your language supports enums, a direction enum would be useful here. (is this C#?) Inside that, you do a case statement (easy with only four cases). For reference, angles generally have RIGHT = 0, UP = 90, LEFT = 180, DOWN = 270 -hence, 360 = 0; Thus, you can, when moving said direction, rotate your (sprite?) character by the constant angle defined by that direction (as in, an absolute setting: calculating relative is a bit more tricky, but still pretty simple). Here is how I picture it:

void move(Direction direction)
{
switch (direction)
{
case RIGHT:
(set rotation/direction of character/sprite to 0)
(other stuffs, like updating grid array to movement)
break;
case DOWN:
(same as above but now it is 270)
break;
...
}
}
//UP = 90, LEFT = 180


Note that in general, 0 degrees is right, and it goes 360 degrees COUNTERclockwise. However, these may depend on what you are using (I know an external library which seems to rotate clockwise, which in theory is more natural but conflicts with the more general view, so you will have to figure this out yourself). Also, in the case you just want a different sprite for each facing direction, then you can simply set the sprite by case, for example, in RIGHT case you can do sprite = RIGHT_SPRITE, etcetra.

Hopefully this will be useful, sorry if it isn't.