Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# top-down racer handbrake implementation

Posted on 2000-05-13
Medium Priority
355 Views
I'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.
0
Question by:GabeSmed
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 2

LVL 3

Expert Comment

ID: 2831639
Hi

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)
{
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, handbrake_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
0

LVL 3

Expert Comment

ID: 2831641
Sorry about the format of the source, but this form seems to remove all the tabs for some reason.

Matth
0

Author Comment

ID: 2831734
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.
0

Author Comment

ID: 2831737
the CAuto class is already in directX
0

LVL 3

Expert Comment

ID: 2831792
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

0

Accepted Solution

woottond earned 150 total points
ID: 2939938
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

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
0

Author Comment

ID: 2940225
0

Author Comment

ID: 2940226
wow.  Thanks!
0

Expert Comment

ID: 2940513
The other thing to note is the reverse of the last paragraph - when you hit the gas the front tires have less weight, and therefore tend to push more
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  Iâ€™ll start with the algorâ€¦
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I wasâ€¦
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonstâ€¦
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (httpsâ€¦
###### Suggested Courses
Course of the Month6 days, 18 hours left to enroll