Simulated steering when coasting.

sparkaniasparkania Member, PRO Posts: 300
This took me 4 hours to figure out.. I figured I'd save someone else the trouble.. Plus I have been helped every time I've asked a question, just figured I'd contribute.

From an overhead standpoint, Simulating wheels turned even when decellerating by creating a continued yet decellerating rotation of a car once the throttle has been let off.

This was to solve a problem I was having when decellerating into a turn..

I have set the controls for my car to my num pad.

C/Key
8 Down, Do, accellerate in direction: 0, relative to: Actor, with accelleration: game.hotrodspeed
C/Key
2 Down, Do, accellerate in direction: 180, relative to: Actor, with accelleration: game.hotrodspeed
C/Key
4 Down, Do, Rotate in a: CounterClockwise direction, at a speed of: game.hotrodturn
C/Key
6 Down, Do, Rotate in a: Clockwise, direction, at a speed of: game.hotrodturn

Originally I had additional key combinations for left and right with their respective forward and reverse keys included. This to control the counter rotation of the actor when reversing.. The problem with that was, if say I was driving Forward and Left, then 8 and 4 were required. But as soon as I let go of the throttle (8) then the car also stopped turning as both keys were no longer being pressed and the car slides to a stop. I couldn't have the Left (4) key run independantly because that would mean the car would just rotate in place even after it stopped moving. Additionally, it would not have the reverse rotating effect when driving in reverse..

Here is what I came up with.

Game Attributes
(real)hotrodSpeed 150
(real)hotrodTurn 120
(real)totalX 0
(real)totalY 0
(real)transmission 0

Actor/Physics
Density 1
Friction 100
Bounce 1
Fixed Rotation (check)
Moveable (check)
Drag 100
Angular Drag 0

and because I have a few vehicles in my game, I have their Max Speeds constrained out to Game attributes for a better overview.

P/Constrain Attribute
constrain: self.motion.max Speed to: game.hotrodSpeed

The first problem I ran into was Velocity. I wanted the car's rotation speed to relate to the car's velocity. Thereby slowing the turning rotation as the car slows it's motion. The problem with that was.. First, the Actor's Velocity is not measure in 1 value, rather 2 X and Y. Angular Velocity did not apply. (even though now that I type this, it probobly could have been used instead of the game.hotrodTurn

Anyway.. The X and Y velocities are not always positive numbers, (up=Positive Y, down=Negative Y, Right=Positive X, Left=Negative X values) so simply adding or averaging the X and Y values and matching them to the turn speed produced quite crazy results depending on in what direction on the screen you were moving. Fun to mess with but counter-productive. So I had to make all the values into consistant values in order to have predictable numbers to work with.

Had to do this by X and Y respectively.

For the X

C/Attribute
if: self.motion.linearVelocityX < 0
do,
P/Constrain Attribute
constrain: game.totalX to: (self.motion.linearVelocityX - self.motion.linearVelocityX)-self.motion.linearVelocityX
else,
P/Constrain Attribute
constrain: game.totalX to: self.motion.linearVelocityX

then again for Y

C/Attribute
if: self.motion.linearVelocityY < 0
do,
P/Constrain Attribute
constrain: game.totalY to: (self.motion.linearVelocityY - self.motion.linearVelocityY)-self.motion.linearVelocityY
else,
P/Constrain Attribute
constrain: game.totalY to: self.motion.linearVelocityY

game.totalX and game.totalY are now positive numbers regardless of which direction on the screen I am going.

the next part was what took the longest to figure out.. I tried all different rule combinations and locations but this one finally worked. I created a toggle value I call the Transmission. A value which would be changed when I hit either the Forward (8) or Reverse (2) buttons.. and stay that way until the opposite was pushed. Before creating this toggle I was having the murder problem of reversing the turn when driving in reverse. I tried adding special conditions and timers and flipping the rotation directions, but the game.totalX and Y were overriding the changes.. and putting timers on full blocks of other commands was very limiting to how you drive..

The Transmission tells what to do with the game.totalX and game.totalY to achieve a positive or negative number which will ultimately determine the speed and direction of your rotation as it relates to you velocity..

C/Attribute
if: game.transmission = 1
do,
P/Constrain Attribute
constrain: game.hotrodturn to: (game.totalX + game.totalY)-(game.totalX + game.totalY)-(game.totalX + game.totalY)
else,
P/Constrain Attribute
constrain: game.hotrodturn to: game.totalX + game.totalY

now I add to the 'Do' list of my Forward and Reverse buttons

Forward (8)
A/Change Attribute
set: game.transmission to: 0

and then

Reverse (2)
A/Change Attribute
set: game.transmission to: 1


Thats It!!! Now, when you trive your car, it will offer the simulated movement as if you let off the gas but were still manipulating the wheel (for all you Drifters)

I am sure that the first response to this post will be one of the Guru's who have helped me before saying 'no.. you just click this and that and it does the same thing..'

Anyway.. Enjoy it while it lasts..

Comments

  • jay2dxjay2dx Member Posts: 611

    just reading though this as im attempting to make a racing game, and I need the car to drift around corners and slow down a little! hopefully I can get this into gamesalad without too much hassle :)

    thanks in advance :)

Sign In or Register to comment.