2D Physics ala GTA 2

Hi Com,

i want to write a little 2d top-down racer. but i don’t get a starting point for the “physics” behind. i’ve read some articles like Adanced Character Physics but it don’t want to go into my head. is there something like dummy-physics-step-by-step-tutorial? the physics don’t need to be realistic, the gameplay is more important for me. i know some self-teached vector math but i’ve no experience with matrix math.

thx in advance

and btw: yes my first official question here, and sorry for any grammar / word mistakes, english isn’t my natural language.

“Advanced character physics” is a nice approach, but IMHO for GTA style physics you might be better off with a more traditional rigid body simulation.

The original 2.5d GTA games actually had quite a complicated engine simulation - IIRC it tracked things like torque, horsepower and revs, and for the actual cars the wheel position and orientation affected the forces that would be applied (rear-wheel-drive car handled differently from a front-wheel drive one).

If you don’t want to delve into the icky physics details yourself it might be easier to use a physics library (either a 2d one with gravity turned off or a full 3d one where you restrict movement in the z axis).

If you want to do it yourself I’d start begin simply and build up. Start with the basic equations of motion and you can have a sprite with basic speed and acceleration applied based on key input. Basic circle vs. circle collision detection is easy, and so detecting when two objects intersect and altering their speed will get you something akin to snooker balls. Add in orientation, upgrade to rotated rectangles rather than circles, add friction from wheels, etc. etc.

Also more reading here: http://www.java-gaming.org/index.php/topic,19653.0.html

Check out JBox2D and Phys2D for physics simulations, they’re both very good.

Also, Kev Glass (author of phys2d) made an awesome physics car game (2d side view with gravity, not top down) here:

It uses the vertlet integration technique in that paper you referenced. You should ask kev about it, this is his web page: http://slick.cokeandcode.com/

Thank you very much Orangy Tang and CommanderKeith. I’ll have a look at the basic equations of motion to learn the basics first, because i want to understand how it works. If i don’t get the point i’ll have a look at Phys2D and JBox2D.

thx

You can also check out Daid’s Ludum Dare 13 entry, which was an interesting vector-based top-down game where you drive cars in a city, and it used Box2D for the physics. Windows only, unless you want to compile it. Also, this isn’t in Java, I was more posting this so you could see how this concept can work with Box2D. When you mentioned your question this game is immediately what I thought of, because I think he had very satisfying car physics and it was fun to drive around his city.

http://daid.mine.nu/pub/LD13_DigitalRoadJam_final.zip

Thanks a bunch Demonpants. It’s really interesting.

Can somebody explain this:


void calcWheel(float angle, b2Body* wheelBody, b2Vec2 pos, float drive) {
        b2Mat22 wheelMat(angle);
        b2Vec2 wheelVelo = wheelBody->GetLocalVector(wheelBody->GetLinearVelocityFromLocalPoint(pos));
        wheelVelo = wheelMat.Solve(wheelVelo);
        wheelVelo.y = drive;
        wheelVelo.x *= -50;
        //TODO: limit wheelVelo.x for slipping
        wheelVelo = wheelMat.GetInverse().Solve(wheelVelo);
        wheelBody->ApplyForce(wheelBody->GetWorldVector(wheelVelo), wheelBody->GetWorldPoint(pos));
}

What is happening here?

My understanding:

  • angle: relative wheel rotation in radians
  • wheelBody: car body
  • pos: relative wheel position from car
  • drive: throttle (1f = max)

Looks like it does some sort of individual physics calculation for the wheels. I’d recommend asking the author, because LD entries are unlikely to have good comments. :slight_smile:

http://www.ludumdare.com/compo/author/daid/

For a sec I thought Java had ‘->’. I was like wtf, when was that added. Then I noticed ‘*’, damn c!

Heh, I’ve seen the following which is valid Java code:


// While x appraoches zero
while (x-->0) { ... }