Follow Slashdot stories on Twitter

 



Forgot your password?
typodupeerror
×
Games Books Media Book Reviews Entertainment

Physics For Game Developers 328

Richard Jones writes: "In my opinion, the most difficult aspect of writing a good 3D game is coding complex physics. If you can take away all the flashy graphics, texture maps, light and shadows from a game, and it's still at least 75% as playable and addictive, then you have an excellent game. But too many programmers seem to be ready to concentrate on the graphics, neglecting the underlying physics which make the game playable. If you compare, say, Re-volt with its fabulously detailed models of remote-controlled cars, and Carmageddon which on the N64 at least has sucky physics, well I know which one I'm still playing." He's contributed his review (below) of a book intended to help game programmers make games that aren't sucky.
Physics For Game Developers
author David M. Bourg
pages 326
publisher O'Reilly
rating 8
reviewer Richard Jones
ISBN 0-596-00006-5
summary A good introduction to the difficult subject of writing 3D games and simulations with accurate physics, let down by a few minor snags.

Programmers who want to get serious about game physics will love David M. Bourg's Physics for Game Developers. As I've said, the subject is inherently very difficult, and the book assumes that you are already familiar with vector and matrix arithmetic up to college level, integration and differentiation, and at least you hazily recall your mechanics/physics lessons from school. You also won't be afraid to wade through Bourg's carefully documented derivations of formulae for various physical effects, and his well-commented source code.

The book starts off by recapping the basic concepts of mass, centre of gravity, moment of inertia and inertia tensors. Bourg assumes that you have a working grasp of these subjects, and I admit that I had to go back to some of my A-level mechanics text books. He then goes straight into kinematics, where he uses standard (but forgotten!) integration techniques to calculate velocities from accelerations and positions from velocities. His examples are excellent, although a few exercises wouldn't have gone amiss. The chapter on forces covers a great many different types of forces such as springs and buoyancy, but curiously omits the important subject of contact forces (the normal force that a table, for instance, exerts on your computer monitor to stop it falling through the floor). In fact contact forces don't appear until much later in the book. Particles, rigid bodies and impulses (forces from collisions) are introduced in chapters 4 and 5.

At this point I have to say I was a little bit confused. What did this have to do with game programming? Everyone knows that games spend most of their time running round a single big "main loop," working out the forces on each object, looking for collisions and working out which keys the user is pressing. It doesn't seem imaginable that a game programmer could completely solve all the equations of motion by pure integration at the beginning of the game, and then just run the positions of the players through the graphics engine like a movie!

I already knew a little about this from what I'd found from the web, but what most games actually do is calculate all the forces on all the objects (players, scenery, etc.) in the game, and then integrate them at each step. Some of these forces will be generated by human players pressing keys on the keyboard or wiggling the joystick, and that's how the objects end up moving. Pure integration isn't usually possible, so the physics engine performs numerical integration - a kind of fast approximation to the pure "closed form" solution. Numerical integration is itself a tricky subject, but it's the meat-and-veg of good game programming. Surprisingly, numerical integration and a realistic main loop doesn't appear until chapter 11 (172 pages into the book). I skipped straight to this section, and I suggest you do so too...

The chapter on numerical integration is excellent and contains the first realistic gaming (or at least simulation) code. Many games I've examined use simple numerical integration, like this:

// At each step ... A = acceleration, dt = time step
Vx += Ax * dt;
Vy += Ay * dt;
Sx += Vx * dt;
Sy += Vy * dt;

Unfortunately this method (Euler's method) is very inaccurate and unstable: if you tried to simulate planets orbiting around a sun using this method, they'd soon fly off into outer space unrealistically. Bourg gives an excellent introduction to better methods such as the "improved Euler" method and the popular Runge-Kutta method, and he covers them in a context which will make it clear how to use these methods in your own programs.

The book reaches a crescendo with three fully developed simulations: two hovercraft which you can drive around and jolt into each other like bumper cars -- they spin around realistically; a flight simulator; and a 3D car which can be crashed into blocks that bounce around. Again the source code is meticulously commented and generally well written. My only two reservations about the code are: It would be nice if Bourg had chosen to use OpenGL instead of Direct3D so that those of us without regular access to Windows could actually compile and run the examples. The book would make an ideal companion to the OpenGL Red Book. And coming firmly from the Windows camp, Bourg's examples are full of all the horrors of Win32 APIs and Hungarian Notation. But maybe that's just my personal preference :-)

So in summary: The Bad Points:

  • Measurement systems: Bourg moves uneasily between the English/US system and the European SI units. So we get examples which combine ft/s, meters, slugs and kilograms, uneasily converting between the two. He should have chosen one system and stuck with it.
  • A common complaint about computer books: I've just spent 25 quid on a book which will sit open on my desk for months. Is it too much to ask that it be ring bound?
  • Some subjects are not explained in enough depth. Particularly: moments, contact forces, impulse methods. Bourg should probably have written a chapter or three on collision detection.
  • The chapters are presented in a very strange order. Move chapters 6-10 until later, or introduce numerical integration earlier.
  • A few of the illustrations are inaccurate.

and The Good Points:

  • Considerably better than the usual round of maths/physics text books which make up this field. In fact, this is really one of only about 2 or 3 significant books in this area which are pitched for game developers as opposed to mathematicians, and it's certainly the best.
  • The areas which are covered are done well, in significant depth, with a good bibliography where you can find out more.
  • The commentary on the difficult equations is good, and Bourg resists the temptation to derive many of the formulae he presents, instead referring interested readers to other references.
  • Code is well documented and explained.

And now I suppose I have no excuse not to resurrect XRacer :-)


You can purchase Physics for Game Developers at Fatbrain

This discussion has been archived. No new comments can be posted.

Physics For Game Developers

Comments Filter:
  • by sam@caveman.org ( 13833 ) on Tuesday December 18, 2001 @11:35AM (#2719806) Homepage
    i've played a lot of games, and gone 'wow' at some of the eye candy, but if you've played baldur's gate for PS2 you know that the coolest thing that has ever been coded for a game is the WATER for this game. you leave a wake behind you when you walk through it, you splash when you jump in, and ripples interact and reflect from the shore. maybe the physics aren't 'perfect' or whatever, but the realism of the water was enough to really 'immerse' myself into the game.

    -sam
  • by mrroot ( 543673 ) on Tuesday December 18, 2001 @11:37AM (#2719821)
    Somestimes games are actually more fun if they defy the laws of physics. Such as being able to jump 20ft in the air (I can only jump about 2ft myself). Don't get me wrong, it has to be somewhat realistic, but any good game will also be a little unrealistic too.
  • Grand Prix Legends (Score:2, Interesting)

    by Anonymous Coward on Tuesday December 18, 2001 @11:46AM (#2719876)
    The game may be three years old, with (originally) dodgy graphics, but it had the best car physics engine of any PC driving game. You could actually get a feeling for what the car was doing, how it was sliding etc, and in a '67 F1 car, which slides a lot, that was a big help.
    It's still one of the most popular games in the driving sim community, with new mods and graphics appearing almost daily, but in the end it's the physics that draws people in. Yah it's hard to drive, but it should be hard to drive, and once you learn it, you'll never look back.
  • by arkanes ( 521690 ) <arkanes@NoSPam.gmail.com> on Tuesday December 18, 2001 @11:47AM (#2719886) Homepage
    The space sim Terminus is great in this regard - when you start the game, you can select the level of reality in your physics - anywhere from (almost) 0, to full Newtonian. So if you just wanna blow stuff up, go for 0, and if you want to have to worry about carefully calculating your path so you take maximum advantage of gravity and don't waste fuel on course adjustments, you take full. Great stuff. I reccomend the game, BTW, fantastic gameplay even if the graphics lack by todays standards.
  • Decision Making (Score:2, Interesting)

    by 3141 ( 468289 ) on Tuesday December 18, 2001 @11:48AM (#2719893) Homepage
    I agree, in some games, realistic physics can make a big difference. I remember when I first played Super Mario Bros. 1 on the NES, seeing how the fireball bounces along in a straight line until it gets to a hole, and actually falls down, I was rather impressed. (Small things, small minds, whatever. I liked it.)

    However, in modern 3D-type games, it's important to pick and choose where you spend your time coding in the physics. In a racing game, it's a lot more important to spend time making sure the cars handle correctly than how realistically the trees sway in the background.

    Unfortunately, there just isn't the time to make everything about the game perfect, and it's sad to see when a program has missed it's spot because of delays in implementing useless features.
  • by thex23 ( 206256 ) on Tuesday December 18, 2001 @11:53AM (#2719914) Homepage
    Realistic physics? Yes, please!

    I want a FPS that lets you blow holes in walls, accurately representing physical damage from weapons. Imagine Counter-Strike with realistic bullet physics: ricochets, windage, and weapon recoil that isn't predefined.

    Hacking physics though, now that's a job and a half: figuring out a quick and dirty method of approximating the complexities of the real-world, and still have it look natural, making it look like a real environment filled with objects that have familiar properties and behaviours. And then blowing them up, REAL GOOD.

  • Carmageddon (Score:3, Interesting)

    by CaseyB ( 1105 ) on Tuesday December 18, 2001 @12:06PM (#2719989)
    Carmageddon which on the N64 at least has sucky physics,

    Excepting the possibility that the N64 version is completely different from the PC version, you are completely clueless. Carmageddon, of ALL the games you could have chosen, was a pioneer in game physics. It was the very first game that I played that had a real rigid-body simulator built into the engine.

    Say what you will about the gameplay, or the physical settings (gravity was too low), but you can't say it had sucky physics. The cars and environmental objects interacted in an incredibly realistic manner. It was miles ahead of Re-Volt in that department.

  • by dreadpiratemark ( 450962 ) on Tuesday December 18, 2001 @12:06PM (#2719990)
    I don't know that I really agree with the idea that physics need to be wildly realistic in the 'games' that I play. Honestly, I don't want to know what kind of recoil the rocket launcher in Half-Life would inflict on my character. I'm happier just watching the rocket track nice and straight towards my target instead of attempting to pick myself up off of the ground where I slid to a stop from the highly realistic recoil.

    On the other hand, when I play a 'simulation' the physics are quite important. The best physics I've found in a sim are in a 3-year-old game: Grand Prix Legends. You're racing 1967-era Formula One cars, which means skinny tires, no downforce, high powered...which is a combination for difficult driving. But the physics engine is spectacular: you touch the gas when in neutral, you can feel the torque twist the body of the car; each wheel has its own model; touch the gas a bit to hard in a corner and be prepared to swap ends of the car, etc., etc.

    The graphics are what you'd expect for a 3 year old game, but a dedicated community has sprung up to support the sim with everything from replay analysers [bip.net], new tracks and graphics [sportplanet.com], and even a movie maker! [racesim.net]

    It seems that if you make a sim with good enough physics behind it, the fans of it can create new 'eye candy' to keep the sim looking good. But if you have crappy gameplay (see: Andretti Racing) and good graphics, the game will quickly be relegated to the bargin bin when something prettier comes along.

    -Mark
  • by Anonymous Canuck ( 249873 ) on Tuesday December 18, 2001 @12:17PM (#2720048)
    "Everyone knows that games spend most of their time running round a single big "main loop," working out the forces on each object, looking for collisions and working out which keys the user is pressing."

    Actually, the game is not working out the forces of each object. This is why most objects in 3d fps games are static. Beyond basic collision detection, there is no interaction with the objects. If there are interactive objects in your game, the math involved in calculating the physics of a 3d pack of cigarettes or a 3d can of cola would not only take an enormous ammount of coding to bring to light, but also put undo load on the CPU.
    When we worked on Ghost Recon, one of the big problems with physics involved calculating the way the trees swayed in the wind(believe it or not). Part of the solution involved going from multiple wind directions to a single one. The users wouldn't notice it. Heck, we could have made the trees static, thus relieving the system of about 40% of it's performance hit, but we wanted some level of realism in the background and atmosphere.

  • Um (Score:1, Interesting)

    by autopr0n ( 534291 ) on Tuesday December 18, 2001 @12:19PM (#2720061) Homepage Journal
    The ability to jump 20 feet into the air has nothing to do with whether or not you're using a realistic physics model, just how much acceleration from gravity is present and how hard you jump.

    Things don't need to be exactly as they are here on earth, but having 'no physics' I don't think would be very much fun.
  • by Dimensio ( 311070 ) <darkstar@LISPiglou.com minus language> on Tuesday December 18, 2001 @12:44PM (#2720293)
    I think that the most glaring physics error commonly seen in many games -- especially platformers and FPSers -- is violations of inertia. Take almost any game where 1) your character can jump and 2) there are horizontally moving platforms of some sort. Stand on a moving platform and jump as it moves. In almost every single game you play your character will jump straight up in the air and the platform will move out from under the character, even though real physics should have the character continually moving in the direction of the vehicle (air resistance isn't that strong or your characters wouldn't be able to move at all).

    I think that I've seen one game (whose name escapes me atm) that actually got it more or less correct -- and I think that was just a game engine quirk that caused it.
  • by drenehtsral ( 29789 ) on Tuesday December 18, 2001 @01:13PM (#2720491) Homepage
    I've found in my limited (hobby only) game programming experience that real physics make for boring run 'n' jump games, but they make for wonderful projectile games. Think ballistics (Scorched Earth, Quake, etc...).

    Another pet peeve i have is that a lot of these sort of things never really mention one killer point. When computing whatever delta t for updating a scene, it is best to use a moving average of the last n frames (i use 32, kept in a rotating ring buffer), because that keeps an unusually long or short frame from fucking up any calcultions.

Suggest you just sit there and wait till life gets easier.

Working...