2D physics- how?

Hi,

I really want to add physics to my 2D tank game. Right now it just does collision detection. The majn reason I want physics is so that when I run into a wall the tank doesn’t just stop but can keep on moving, and so that I can have cooler weapons like harpoons, actual explosions, ramming, etc. How should I implement the physics? Everything in the game world has a 2D polygon already. What fields do I need to add to each game object & how should I resolve the forces, etc without having infinite loop problems? My game uses time based movement.

thanks,
Keith

mass
xVelocity
yVelocity
angularVelocity
forceSize
forceAngle
forcePoint

A simple approach is outlined in this paper. It’s not perfectly realistic, but it’s relatively simple to implement, and looks pretty good.
It’s an emergent system, so the complex global behaviour arises automagically from simple local interactions - this means you don’t have to have any physics knowledge, or indeed much maths (vectors is about it, there isn’t even any trigonometry).

Someone around here has already done a java implementation of that ( http://www.java-gaming.org/forums/index.php?topic=399.0 ) although I’m not sure if it’s still avaliable.

On the other hand doing your own implementation isn’t as hard as it looks, the math is much simpler than most physics methods and it requires much less tinkering to get stable. Definately recommended for 2d physics.

Edit: Ha, obviously I type too slowly. :slight_smile: Bleb you should put that physics stuff back online, it was quite cool.

Err, yeah… someone… ::slight_smile:

I could dig it out again, but it’s been years since i did anything to it, it could probably do with some refactoring, etc, etc.

In any case, using my crappy code won’t be as fun as doing it yourself.

edit: Have just reread the linked thread ;D - brought back the memories of how “conversing” with VRML_Java_Animator was like bathing in pure, concentrated crazy.

Thanks very much for those links. That method sounds quite amazing, its just a pity I don’t know vector or matrix maths. I’d actually be happier if it was physics & trigonometry. The method outlined in the article opened my eyes to what can be done - like I never thought of the possibility actually allowing the vehicle go inside a wall just a little so physics can be calculated in a stable way. Interesting techniques, I’ll have to do more reading.

Dependend on what you need, for startes it is x and y velocity or velocity and direction. Id take both redundant but makes things easier. You need to compute one out of the other anyways sooner or later…

On collision you need to calculate momentum / impulse. Momentum p = m * v (mass * velocity).
The velocity of the other object then is v = p/m. Velocity gets greater when the other object has a lesser mass. The direction of the movement is
away from the center of the colliding object. So when a tank hits a crate, the crate moves away from the center of the tank. You just need to calculate the angle based on x and y positions of the tanks and crates center.

Elastic collision is a little different, you need to look this one up. The tank would then bounce back when hitting a wall.

Ballistics are easy, just handle horizontal and vertical different. Horizontal speed is constant or reduces at a constant rate if you want it. Vertical speed reduces by gravity. Precomputing the point of impact or computing a shooting angle to hit a certain spot is a little difficult.

There is a book “Beginning Math and Physics for Game Programmers” - Wendy Stahler. It covers all basics.
Any physics book does it as well.

Need something special?

-JAW