Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

top-down racer handbrake implementation

Posted on 2000-05-13
9
Medium Priority
?
356 Views
Last Modified: 2010-07-27
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
Comment
Question by:GabeSmed
  • 4
  • 3
  • 2
9 Comments
 
LVL 3

Expert Comment

by:matth012098
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)
{
   // 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, 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

by:matth012098
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

by:GabeSmed
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:GabeSmed
ID: 2831737
the CAuto class is already in directX
0
 
LVL 3

Expert Comment

by:matth012098
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

by:
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
 \  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
0
 

Author Comment

by:GabeSmed
ID: 2940225
Comment accepted as answer
0
 

Author Comment

by:GabeSmed
ID: 2940226
wow.  Thanks!
0
 

Expert Comment

by:woottond
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question