Question

top-down racer handbrake implementation

Asked by: GabeSmed

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.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2000-05-13 at 22:18:13ID10349987
Tags

handbrake

,

car

Topic

Game Programming

Participating Experts
2
Points
50
Comments
9

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Car Physics
    Hi I am currently implementing a car physics engine and have bumped into a couple of problems. I have been trying to figure out how to handle engine speed in RPM. My question is what is the best way of tracking / calculating RPM of the engine. Would for instance, calculating...
  2. The best programmingprogram for C++ and DirectX
    Hi! I wonder which C++ OOP program that is the most oversightly and best to use with DirectX. And which program is mostly like Ansi C++? Which program do the experts use to program 3d games and powerful applications? Thanks!!!
  3. 2D car physics - make the cars spin!!
    I'm looking for help on a 3D racing car game that I'm doing for my final year project. I'm sticking to 2D physics by ignoring the y-component for now. Given I have the vehicles centre of mass/gravity, velocity, angular_velocity, acceleration, angualar_acceleration abd the int...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

by: matthPosted on 2000-05-21 at 18:38:56ID: 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

 

by: matthPosted on 2000-05-21 at 18:40:02ID: 2831641

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

Matth

 

by: GabeSmedPosted on 2000-05-21 at 19:45:12ID: 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.

 

by: GabeSmedPosted on 2000-05-21 at 19:48:05ID: 2831737

the CAuto class is already in directX

 

by: matthPosted on 2000-05-21 at 20:29:18ID: 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

 

by: woottondPosted on 2000-06-13 at 06:47:50ID: 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

 

by: GabeSmedPosted on 2000-06-13 at 07:27:31ID: 2940225

Comment accepted as answer

 

by: GabeSmedPosted on 2000-06-13 at 07:27:31ID: 2940226

wow.  Thanks!

 

by: woottondPosted on 2000-06-13 at 08:09:22ID: 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

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...