Sorry about the format of the source, but this form seems to remove all the tabs for some reason.
Matth
Main Topics
Browse All TopicsI'd like some input as to how I can make a simulation of a car hand brake in a top down car game using C++ and DirectX. It doesn't need to be realistic but it should feel somewhat real.
I'd be thankful for even some thoughtful ideas, not code or anything.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Wow, that's great. I can see you spent a long time typing all that out :). Unfortunately, that's not really what I want.
I already have a CAuto class with movement, acceleration, turning, turboing (is that a word?), and braking built in. However, I would like to know if anyone can help me build an *accurate* hand-braking model.
I.E. you know in computer games when you turn and handbrake your car spins out of control? I want something like that.
Thanks anyway! If nobody else answers this question I'll just give you the points.
Hi
To simulate this in physics is very difficult since these affects such as spin, and sway come about from the fact that the physical characteristics of the car and the road are not perfectly symmetrical, e.g Front tyres maybe more worn than the back tyres, or the road under the front left wheel is slippier than the road under the back right wheel.
I suppose without delving into a major area of physics it would be possible to simulate by setting thresholds for the handbrake variable and causing a particular action to occur if the handbrake exceeeds one of these thresholds e.g
Drag
----
Drag would occur under any handbrake condition, since it will always slow the car down
Sway
----
If the handbrake is applied too quickly then the back end would sway in a predetermined manner.
Spin
----
If sway occurs and the user continues to accerate the handbraking then the car would be set off into a spin. The speed of the spin could be determined by the rate of increase in handbrake from sway to spin. The direction of spin could be decided from the difference between the front wheels and body angle.
Speed would also have to be factored in. I suppose all of the above could be scaled by speed, with exception to drag.
Matth
I would involve a variable called Max_Traction and Traction.
The hardness of braking, or handbrake (Locking tires in almost all cases) would increase the rate the car moves as a result of turning, and/or surface...
Say the surface is 15 degrees
0 CAR
\ Road
Then using the mass of the car, multiplied, by the friction coefficant (a decimal number) multiplied by gravity. Now bring in the Lateral speed of the back (and the front should be done too = but keep the front and back lateral speeds seperate, to allow fishtale and push)
The first number is an accelerating number, the speed is the current lateral movement...
This will now give you the tail of the car to move out. To get it to move back in get the angle of the car relative to its forward motion, then using the triangle theorem again, get the length of the cars rear to the line of travel, in meters. This is the inward pull. Multiply this also by the traction.
Take these two opposite movements and add them together (or subtract absolute values) This will do roughly for the rear - the same follows for the front end, except the outward pull is found by the angle of turn, compared to current travel, get the force of seperation from the triangle, multiply by front tire traction... This should by higher when braking!!!
When braking on a turn the front holds the line harder cause you are braking, and more traction on the front. The rear however has less traction and breaks faster from the line...
Cheers!
Remember to gi
Business Accounts
Answer for Membership
by: matthPosted on 2000-05-21 at 18:38:56ID: 2831639
Hi
, handbrake_pressed);
I guess you could do the following:
#define MAX_HANDBRAKE = 256
int handbrakeinc = 0;
int handbrake = 0;
void check_handbrake()
{
if (space_bar_pressed)
{
handbrakeinc++;
if (hanbrake != MAX_HANDBRAKE)
handbrake += handbrakeinc;
}
else
{
handbrakeinc = 0;
handbrake = 0;
}
}
The above is only to handle the very basic mechanics of an handbrake. As the user holds down the key more and more handbrake is applied at an accelerated rate. You could then input handbrake into your physics routine and reduce velocity accordingly, throttle could be implemented in a similar manner.
void simple_physics(int ax, int ay, int handbrake)
{
// Add acceleration onto velocity
vx += ax;
vy += ay;
// Apply handbrake
if (handbrake != 0)
{
vx /= handbrake;
vy /= handbrake;
}
// Add velocity onto position to move car
x += vx;
y += vy;
}
vx and vy should be part of your object structure, see later in this message
Ive not tried not of this code so I dont know how well it performs. Dividing the x and y velocities by the amount of handbrake will slow down the car.
Maybe your car structure could look like this
typedef struct {
int x, y;
int vx, vy;
int throttle;
int brake;
int handbrake;
} CAR;
In which case the above routines will change to:
void check_handbrake(CAR *car)
{
if (space_bar_pressed)
{
car->handbrakeinc++;
if (car->handbrake != MAX_HANDBRAKE)
car->handbrake += handbrakeinc;
}
else
{
car->handbrakeinc = 0;
car->handbrake = 0;
}
}
void simple_physics(CAR *car)
{
// Apply handbrake
if (car->handbrake != 0)
{
car->vx /= car->handbrake;
car->vy /= car->handbrake;
}
// Add velocity onto position to move car
car->x += car->vx;
car->y += car->vy;
}
However since you are using C++ you would be better off implementing all this as a class like so
#define MAX_HANDBRAKE 256
#define MAX_THROTTLE 256
// Fill these with sin and cosine waves, amplitude between -4095 and +4095
int sine[360];
int cose[360];
bool throttle_pressed = false;
bool handbrake_pressed = false;
typedef struct {
int x, y;
int vx, vy;
int angle;
int brake;
int throttleinc;
int throttle;
int handbrakeinc;
int handbrake;
} CAR_TYPE;
class CAR
{
private:
CAR_TYPE car;
int handbrakeinc;
int handbrake;
public:
CAR(int x, int y);
private:
void apply_handbrake();
void apply_throttle();
public:
void run(bool apply_throttle, bool apply_handbrake);
void set_angle(int ang);
};
CAR::CAR(int x, int y)
{
car.handbrake = 0;
car.handbrakeinc = 0;
car.throttle = 0;
car.throttleinc = 0;
car.x = x;
car.y = y;
car.vx = 0;
car.vy = 0;
car.angle = 0;
}
void CAR::apply_handbrake()
{
if (handbrake < MAX_HANDBRAKE)
{
car.handbrakeinc++;
car.handbrake += car.handbrakeinc;
}
else
car.handbrake = MAX_HANDBRAKE;
}
void CAR::apply_throttle()
{
if (handbrake < MAX_THROTTLE)
{
car.throttleinc++;
car.throttle += car.throttleinc;
}
else
car.throttle = MAX_THROTTLE;
}
void CAR::run(bool apply_throt, bool apply_hb)
{
if (apply_throt) // Player has foot on throttle
apply_throttle();
else // Player has taken foot off throttle
{
// Decay throttle
car.throttleinc = 0;
if (car.throttle > 0)
car.throttle--;
else
car.throttle = 0;
}
if (apply_hb) // Player has foot on handbrake
apply_handbrake();
else // Player has taken foot off handbrake
{
// Decay handbrake
car.handbrakeinc = 0;
car.handbrake = 0;
}
// Acceleration is applied in direction car is travelling
car.vx = (sine[car.angle] * car.throttle) >> 12; // >>12 = div by 4096 which is amplitude of sin wave
car.vy = (cose[car.angle] * car.throttle) >> 12; // >>12 = div by 4096 which is amplitude of cosine wave
// Handle brake physics
if (car.handbrake != 0)
{
car.vx /= car.handbrake;
car.vy /= car.handbrake;
}
// Move car
car.x += car.vx;
car.y += car.vy;
// Collision detection
// .....
// .....
// .....
}
void CAR::set_angle(int ang)
{
car.angle = ang;
}
void main(void)
{
// Create a car
CAR mycar(0, 0);
while(1)
{
// Monitor keyboard
// Run car handler
mycar.run(throttle_pressed
}
}
The above code compiles but I havent tested it and may require messing around with, but it shows the basics of whats happening. Personaly I would implement the physics system using proper physics so later on applying physical effects to the car would be easier to implement. The above method also uses all integer maths, you may want to do it in float, its as fast on a PC if not faster, I would still tabulate sin and cosine though if your gonna have a lot of cars.
DirectX is a whole different ball game and is way out of the realms of this forum, but basically you will use:
DirectInput for monitoring keyboard input
DirectDraw for drawing the cars and scenery
DirectSound for sound effects
DirectMusic for music or just play CD music its easier
Direct3D if you feel adventurous and want the game to run in 3D.
Hope this helps
Matth