5

Okay so this problem has been bothering me for the longest time. Can anyone show me or point me to an algorithm that can control a car like that of GTA2? After 3 days of research all I could come up with was all of these algorithms for using pivot and joints on the wheels and separate wheels and such. Is that the only way to achieve simple car movement like that of GTA2?

I want to be able to use the algorithm on a rectangle without wheels but still be able to have the car drift. Is that possible? By the way, I am usign Box2D for the 2D game.

I know this is more suitable for gamedev but for some reason I can't post questions .

Free Lancer
  • 1,000
  • 2
  • 16
  • 33
  • This is really broad... sure it's possible, what part are you having problems with? – iforce2d Mar 02 '12 at 12:31
  • I guess the problem I am having is implementing a simple algorithm in Java that's called on every frame to make a car drive like that of GTA2 the game. – Free Lancer Mar 03 '12 at 01:19
  • 2
    Ok so the part you're having problems with is... the whole thing :) I was hoping for some specific behavior you need, for example I think canceling out the sideways velocity of the car would be a typical requirement. I was wondering if you had seen this one: http://www.iforce2d.net/b2dtut/top-down-car The first part looks at some typical top-down car physics stuff applied to a single body. Later it uses four of those bodies as wheels but you could treat the single body as a car if you wanted to. – iforce2d Mar 03 '12 at 11:10
  • OH! I saw that link before but I thought he was modeling real world car physics with 4 wheels which I thought was too much for this simple game. I didn't see that there was a part on using a single body!!! THANK YOU! Can you put it as an answer so I could mark check for other people's reference? – Free Lancer Mar 03 '12 at 19:19
  • Wait I just noticed that the single body is a tire. Are you saying I could use that as a car instead for my situation? – Free Lancer Mar 03 '12 at 19:55
  • Sorry I missed your comment somehow. Yep, that's what I'm saying. You could pretend the single 'tire' was in fact the car itself. To turn it you could apply a torque or angular impulse directly, though you might want to scale down the allowable torque as the forward speed nears zero to prevent the car from being able to turn on the spot. – iforce2d Apr 20 '12 at 19:08

2 Answers2

8

A simple answer that can turn into something quite big so I will try to explain by presenting different points in an increasing order of sophistication. I will be assuming a basic knowledge of physics.

  1. Assume a fixed turning radius (not too bad if you are using a keyboard, quite annoying if you have an analog controller). Nothing like trying out different positions to find out what radius feels good.

  2. Assume that you have wheels that are initially facing forward and as you press the turn key they progressively turn to the maximum possible. This basically means decreasing the radius from infinity to your smallest possible radius (you can figure out the relationship between the angle of the wheels and the radius easily). If you have an analog controller then the radius should be controlled by the continuous values of the analog input.

  3. Let the forces enter! When you are turning in a car, you only turn due to a centripetal acceleration. That centripetal acceleration is caused by a force which is actually the friction of the car with the road. You can consider the friction a constant and the mass of your vehicle constant without major problems then you have a relatioship between the velocity of the car and the critical radius (the minimum radius you can turn given the velocity). The centripetal acceleration is a=v^2/r = Friction/mass so the critical radius r = v^2*mass/Friction. You can consider that no matter how much you turn you vehicle will drift and, at maximum, describe this curve. This should give you a nice simulation but still not the "losing control" feeling. For this see the next point! circular motion

  4. The theory is exactly the same as in the point before but the main thing is that Friction in reality is not constant. In fact, the static friction will always be higher that the kinetic friction. In practice, you should have a static friction and a (smaller) kinetic friction. You calculate r according to the static friction and when your velocity is too big to achieve that r (this is when you would drift) you start calculating the new r using the kinetic friction. This will give you the losing control feeling but the vehicle will still not spin. Friction

  5. In order to see the spin, you would have to consider the forces applied in every wheel (it is the fact that the different wheels are under different forces that makes the car spin) and consider some more advanced physics such as which wheels are the driving wheels and also consider the kinetic friction not a constant. However I believe this is out of your scope.

  6. Alternatively you can do something that GTA2 seemed to do. The moment you know you are going to drift or are drifting too much (you set a threshold here) just programmatically make the vehicle lose control and spin.

Hope this helps, if you have any specific doubt just ask.

PhyBandit
  • 303
  • 2
  • 7
1

I found the http://www.banditracer.eu/carexample/ demonstrated a simple example for using Box2D to show car movement. The http://www.banditracer.eu/ has an open source game that you can observe to see if it has the drifting movement your looking for. You can check out the code and see how they handled the drifting movement and do the same for your project.

GatorQue
  • 43
  • 4