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.
Player movement and changing rotation depending on the way they are facing
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.
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.
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.
Third, add or subtract that amount to the current car rotation.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement