View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Player movement and changing rotation depending on the way they are facing

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

### #1Lil_Lloyd  Members

Posted 18 July 2012 - 07:14 PM

Kind of a long title, I apologise! Ok, What I want to know is an efficient simple way to handle player movement after they have turned 180 degrees. For example, let's say I'm programming an overhead racer or flying a jet fighter through canyons from an overhead perspective.

When the player is facing north, left banks left, obviously! This is achieved through some counter clockwise rotation. Right banks right, obviously! This is achieved through some clockwise rotation.

HOWEVER lets say the player character/vehicle is now facing south. The player will press right to turn right on the screen, but relative to the player character this is still in fact their left! They turn left on screen and the player starts crying.

So....how do we implement this kind of system so that player movement in a racer is congruent to a player's expectations?

### #2Inferiarum  Members

Posted 19 July 2012 - 02:22 AM

i would not change anything. The player presses right to kind of rotate the vehicle clockwise. That is what I would expect, and that is also how it works in all games with this kind of gameplay.

edit: e.g. micro machines comes to mind

Edited by Inferiarum, 19 July 2012 - 02:25 AM.

### #3Lil_Lloyd  Members

Posted 19 July 2012 - 08:55 AM

Micro machines was fun, but I remember it taking a while to get used to. If I see something moving in a downwards direction and I press left I expect it to turn left, and if I want to create some exciting and crazy turns for tracks I'd rather have more intuitive controls.

I thought of a way to do this anyway now:

a switch statement using the avatar's current facing direction:

going counter clockwise from the 3 o'clock or 0 degrees position:

if angle > 0 && angle <= 180:
left rotates left, right rotates right

if angle > 90 && angle <= 270:
up rotates right down rotates left

if angle > 180 && angle <= 360:
left rotates right and right rotates left

if angle > 270 && angle <= 360 && angle <= 90:
down rotates right and up rotates left

### #4Acotoz  Members

Posted 19 July 2012 - 09:03 AM

if angle > 0 && angle <= 180:
left rotates left, right rotates right

if angle > 90 && angle <= 270:
up rotates right down rotates left

if angle > 180 && angle <= 360:
left rotates right and right rotates left

if angle > 270 && angle <= 360 && angle <= 90:
down rotates right and up rotates left

Or you can use radians, several game engines use radians instead of degrees, that way there's consistency in the math language throughout the whole code.

### #5SimonForsman  Members

Posted 19 July 2012 - 09:07 AM

Micro machines was fun, but I remember it taking a while to get used to. If I see something moving in a downwards direction and I press left I expect it to turn left, and if I want to create some exciting and crazy turns for tracks I'd rather have more intuitive controls.

I thought of a way to do this anyway now:

a switch statement using the avatar's current facing direction:

going counter clockwise from the 3 o'clock or 0 degrees position:

if angle > 0 && angle <= 180:
left rotates left, right rotates right

if angle > 90 && angle <= 270:
up rotates right down rotates left

if angle > 180 && angle <= 360:
left rotates right and right rotates left

if angle > 270 && angle <= 360 && angle <= 90:
down rotates right and up rotates left

Such a system would become quite annoying or inconsistent in long turns,

The player will expect to be able to do a 360 degree turn by holding either left or right, if you swap the controls around at various angles it will get really wierd.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

### #6kunos  Members

Posted 19 July 2012 - 09:16 AM

HOWEVER lets say the player character/vehicle is now facing south. The player will press right to turn right on the screen, but relative to the player character this is still in fact their left! They turn left on screen and the player starts crying.

No I think they would cry if that didn't happen.
It is the correct behaviour, you are turning left or right with reference to the character, not to the screen.

### #7Mussi  GDNet+

Posted 19 July 2012 - 09:24 AM

What do you expect to happen if they player keeps his left button pressed?

You could also rotate the camera with the vehicle, that way left is always left and right is always right.

### #8Inferiarum  Members

Posted 19 July 2012 - 09:51 AM

I guess there is two ways of doing top down controls. EIther relativ to the player or absolute. I do not think a mixed approach would be a good idea.

### #9Lil_Lloyd  Members

Posted 19 July 2012 - 05:10 PM

HOWEVER lets say the player character/vehicle is now facing south. The player will press right to turn right on the screen, but relative to the player character this is still in fact their left! They turn left on screen and the player starts crying.

No I think they would cry if that didn't happen.
It is the correct behaviour, you are turning left or right with reference to the character, not to the screen.

It isn't REALLY though is it? See: PAC MAN. As I'm making a quick to pick up flying simulation around a maze like track this is the system I'm going for, but as an option I can always leave in the so-called "correct" scheme, just so everyone is happy! Thanks for the input.

The player will expect to be able to do a 360 degree turn by holding either left or right, if you swap the controls around at various angles it will get really wierd.

I see what you mean, however in my game design there isn't any need for such turns, as the 'tracks' are basically canyons and are quite tight, no room or need to do a 180, but thanks for the input.

What do you expect to happen if they player keeps his left button pressed?

The plane should turn to the 9 0clock position, and stay there.

### #10Mussi  GDNet+

Posted 19 July 2012 - 05:59 PM

The plane should turn to the 9 0clock position, and stay there.

In that case, I don't think the example code you've posted will suffice(it'll wobble left and right). You'll have to store what state the player is currently in, the first time the button is pressed and use that to decide how rotation follows.

Edited by Mussi, 19 July 2012 - 06:01 PM.

### #11Postie  Members

Posted 19 July 2012 - 06:25 PM

From the sounds of it what you want to do is rotate the player to an absolute screen position specified by the cursor key they pressed, regardless of their current orientation. So for example, While Left is held down, you rotate the player towards facing left (with respect to the screen). Similar for the other 3 directions. You shouldn't need to impose range restrictions for determining which direction to turn. You will however have to calculate which of anti-clockwise or clockwise rotation is shorter, and then do that.

So you'd have a key detection phase, where you set the target rotation, then a turning phase where you adjust the current rotation by a small offset in the direction of the target rotation.

That said I don't necessarily agree with this as a control system. I seems like it would be difficult to negotiate a curved track as you'd be dancing around 4 keys to achieve turns, rather than 2 with a more traditional rotate anti-clockwise or clockwise method.
Currently working on an open world survival RPG - For info check out my Development blog:

### #12Lil_Lloyd  Members

Posted 20 July 2012 - 12:15 AM

Thank you again everyone for the input so far! Very valid comments from everyone.

### #13Inferiarum  Members

Posted 20 July 2012 - 02:46 AM

I think absolute controls are suited for action games, where you also use the mouse to shoot or something like that (e.g. Bloodline Champions, Babo Violent). I guess you could do the same with vehicles although as Postie said you need some extra logic to determine the best way to turn the vehicle in the desired direction. That does not only involve the steering of the wheels but also acceleration and breaking. So in total the player has less control over the vehicle that is why i probably would not do it that way if the focus is on racing.

### #14L. Spiro  Members

Posted 20 July 2012 - 04:56 AM

The most robust, fluid, and natural system is actually fairly trivial.

There is the direction you are actually facing, in screen coordinates, and the direction you want to face, in screen coordinates.
Every frame, rotate towards the preferred direction by a small amount. It is really just that simple.

Since my hard drive crash, I have not reinstalled Adobe Photoshop CS3 on this operating system, so I will explain with words.

Firstly imagine the player is facing south. The direction is [0, -1].
The player hits RIGHT. The preferred direction becomes [1, 0].

Frame starts.

First, find the shortest distance between the current angle and the preferred angle. If you spin the car clockwise you will have to go 270 degrees, whereas spinning counter clockwise means only 90 degrees.

You now know that you need to add 90 degrees to get to the target. Adding means rotating counter clockwise, subtracting means clockwise.
bool bNeg = false;
float fRot = 90;	// You should be working with radians; this is for mental clarity.


Second, perform a sanity check. Adding 90 degrees results in instant spinning to the desired direction. Then amount of rotation should be capped for a single frame.
fRot = min( fRot, 5.0f );   // Adjust the amount to change the turning radius.

Third, add or subtract that amount to the current car rotation.
cCar.AddRot( bNeg ? -fRot : fRot );

Repeat on next frame.

I kept fRot positive because it simplifies the min().

This handles all cases.
From any rotation, the car will smoothly rotate into the desired rotation as the player hits any combination of keys.
This also allows the player to hit any combination of keys, such as down-left, etc. And even if it is in the middle of a rotation it will just start from there and rotate into the new position smoothly.

The last detail is to not rotate the car if no keys are held.

L. Spiro

Edited by L. Spiro, 20 July 2012 - 04:58 AM.

### #15Lil_Lloyd  Members

Posted 20 July 2012 - 10:52 AM

Super awesome post! Thank you!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.