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:
  • Physics in games (Score:2, Insightful)

    by shd99004 ( 317968 ) on Tuesday December 18, 2001 @11:37AM (#2719823) Homepage
    I remember a good old game called "4D Stunts Driving" or something... That had a pretty interesting physic. Often it was sufficient to just bump into another car and you would go up in flames and millions of pieces. Or, if you reached high enough speed in the jumps, you could get such an angle, you would fly right into the athmosphere, and if you changed the camera POV, you could maybe see the car as a few pixels high in the sky. I think that, though, is part of the charm with this game. That, and the music :)
  • by YouAreFatMan ( 470882 ) on Tuesday December 18, 2001 @11:38AM (#2719825) Homepage
    One idea might be to pair up a design-minded developer with a science-minded programmer. Kind of the right-brain/left-brain thing. Not that there isn't overlap, but usually someone is better at one than the other. And even if someone has both good design and scientific skills, it is hard to practice them at the same time. I know I have to take time to "switch" modes when going from creative stuff to more technical stuff.

    So pair up a couple of guys. One to worry about the artistic design, and another to worry about realistic physics.

  • by alen ( 225700 ) on Tuesday December 18, 2001 @11:39AM (#2719828)
    Some people aren't into super realistic physics. Some don't want to read a 300 page manual for a flight sim and spend weeks figuring out the flight model. They would rather just grab a joystick and start playing like in Wing Commander.
  • by FortKnox ( 169099 ) on Tuesday December 18, 2001 @11:39AM (#2719834) Homepage Journal
    Its called "Physics for Scientists and Engineers".

    Lets face it, the best physics is REAL physics.

    If you want your game to have good physics, then slap a good physics engine (based on real formulae) into it!
  • by joshv ( 13017 ) on Tuesday December 18, 2001 @11:41AM (#2719842)
    I really doubt that your method of numeric integration is going to be that critical an influence on the quality of game play. In fact I think that you ability to simulate physics without really doing the math would be more important. I doubt anyone is going to be timing a car's drop off of a cliff in Carmegeddon to determine what gravitational constant is used in the game.

    I think it is more important to include as many effects as you can: gravity, linear momentum, angular momentum, elastic/inelastic collisions, friction (surface and wind), than it is to model the effects perfectly.

    In fact one could argue that it is to the game designer's benefit to use an innaccurate and exaggerated physics model. Most real world collisons with the guard rail on a race course are relatively unspectacular (by design) - but that would be oh so boring in a racing game now wouldn't it?

    -josh
  • by SuzanneA ( 526699 ) on Tuesday December 18, 2001 @11:44AM (#2719863)
    IMO, the best solution is to have the physics definitions modular, or at least programmable. This is one of the advantages of UT, its physics can be overriden fairly easily.

    Even on games where there doesn't appear to be any need for modular physics, there will always be some mod author that sees a need. Say you are writing a racer, you might think that the physics should be fixed to real-world physics, but the mod author that wants to write a 'dune buggies on the moon' mod, is going to disagree with you, so keep it modular :)

  • by whatnotever ( 116284 ) on Tuesday December 18, 2001 @11:54AM (#2719920)
    You can't use real physics. Real physics lives in the continuous time domain, with differential equations and such. Games, on the other hand, live in a strange land of discrete, variable-sized time-steps. You can't just throw your differential equation at it and say "Be realistic!"

    Sure, the formulas used are *based* on the regular "scientist" formulas, but they *have* to be approximations. The book tells you how to use do those approximations.

    If you give a coder a standard physics textbook and tell him to code a physics engine, he'll either implode or spend ages working out numerical methods for approximating real physics... exactly what is in this book.
  • by _Ash_ ( 126458 ) on Tuesday December 18, 2001 @12:01PM (#2719952) Homepage
    This is already going on between companies. For instance, think about John Carmack. He writes 3D engines and produces games with them. However, his last effort, Quake 3 is a bit mediocre. But after a few months games come out that are a lot better which use the Q3 engine.
    Actually, Half-Life is based upon the Q2 engine and is a lot better than Quake 2 itself.

    Maybe John Carmack should just focus on building 3D engines which game developers could than use to produce brilliant games.
  • by skoda ( 211470 ) on Tuesday December 18, 2001 @12:03PM (#2719967) Homepage
    There's a difference between "unrealistic" and "unstable".

    Unrealistic suggests that it doesn't behave as in the real world -- but that doesn't mean it isn't modeled on the same principles (acceleration, momentum, etc.) And you still want to simulate it consistently; it should "feel" right.

    But an unstable computation method can "blow up", regardless of realistic or unrealistic "physics."

    Consider:
    Jump pads in games like Quake or Unreal are "unrealistic". But they are modeled at least partially on physics. But to make them work right, in all their unrealistic glory, the computation method must be stable.

    If it wasn't stable, then you might something like:
    - Multiple, successive jumps on one would lead to a "blow up" where the player would be wildly, and unexpectedly shot through the roof and to the outer edge of the game universe.

    For both realistic and cartoon physics, you need accurate and stable computation methods.
  • by Junta ( 36770 ) on Tuesday December 18, 2001 @12:07PM (#2719994)
    I'll bet that in at least 90% of the games out there realistic physics would completely ruin the whole fun. What is the point of interacting with a fake environment when the environment reacts the exact same way real life reacts? Racing games are a good example. People want cares to handle well, generally. They don't want realism, in real life operating cars at high speeds is a lot more difficult than most games.

    And in *any* game where people jump, realistic jumping becomes completely pointless. People want to be superhuman. Imagine quake with realistic physics....

    Realism is not the high mark of games in all aspects, that is the whole point of games, to escape reality...
  • by iainl ( 136759 ) on Tuesday December 18, 2001 @12:14PM (#2720035)
    Some of my all time favorite games are great because they move 'right'. Can you imagine playing Spindizzy or Marble Madness with crap physics? I can, on a dire MM ripoff. It was frankly painful. The unexpected coolness that can result from engines that do things properly is very nice - although they cheated on the amount of kickback to do it, rocket jumping would never exist if they hadn't used impulses and momentum to control movement rather than old style 'move forward if the player pushes forward' arcade method.
  • by akiaki007 ( 148804 ) <aa316@nyWELTYu.edu minus author> on Tuesday December 18, 2001 @12:21PM (#2720072)
    Is that it gives you all the important physics equations that I learned throughout freshman and sophomore years of college all in one place. They equations are easy to find (though the variables used aren't the traditional ones in physics).

    The book is a good reference to have. To me this would be good to have because I already "learned" all of this, but like most don't remember all of it. Having all these equations right in front of you will enable you to remember everything swiftly and apply what you need to.

    Like most O'Reilly books, this is a good reference to have, and I think it should be bought by people that already know most of the basic physics stuff.
  • by alephnull42 ( 202254 ) on Tuesday December 18, 2001 @12:24PM (#2720095) Homepage Journal
    Quote: "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?"

    The book is not ring bound because it costs 25 quid (= British Pounds).

    'Twould be far too easy to stick it through a photocopier or scanner if it were.
  • by RobertFisher ( 21116 ) on Tuesday December 18, 2001 @12:29PM (#2720144) Journal
    This is a good point, and a valid one.

    It is true that in some games -- a combat flight simulator, for instance -- one design goal would be to achieve the most realistic physics possible. The average kid popping quarters into an arcade game isn't looking for realism, but some folks certainly will.

    However, I think the importance of _self-consistent_ physics is quite critical. You can invent your own set of "physical laws", which may be similar to those we are familiar with (ie, with moon gravity instead of Earth gravity) or completely and utterly different (ie, with Matrix-like time and space distortions or a Tolkien-like world replete with magic). However, the world you put forth should appear at once both plausible and self-consistent. Indeed, knowing real physics and the numerical methods to treat it would certainly help game programmers in their own constructions, if only by serving as an intricately balanced example which can serve as a launching point for their work.

    Bob
  • by Zathrus ( 232140 ) on Tuesday December 18, 2001 @12:48PM (#2720322) Homepage
    Of course they're not about realistic physics.

    But, guess what - even if your computer game has a G that would crush humans, or elasticity that makes super balls look tame, or centers of gravity below the pavement, it still needs to be CONSISTENT.

    And being consistent means modeling real world physics. With tweaks.

    If you don't model real world physics properly though you're going to end up with erratic behavior that will either lead to frustrated players or exploitation by players (which often trivializes the game).

    And while I never thought about it, now I understand the purpose behind some of the abysmal math courses I took in college. I guess it's a good thing I don't do game coding, since I certainly don't recall much at all from those courses. (Another reason why math profs should come out of their theoretical world and mention real world uses for some of the stuff upon occasion).
  • by bloggins02 ( 468782 ) on Tuesday December 18, 2001 @12:50PM (#2720340)
    The real solution here would be to stay with real world physics. "Real World" physics work on the moon too. If a physics library would allow you to specify mass instead of weight and also the mass of the main body to witch he is on the surface, this library would indeed encompass ANY physical situation. You could even have a "G-Field" which could temporarily change the value of G (the gravitational constant) or even your mass to make you be able to "defy the laws of physics" and jump 20 feet in the air while you have said "G Field" suit on.

    I think accurate physics MUST be present in most motion-oriented games so that the control feels more natural (i.e. feels like what would happen in THIS universe). You can change masses, gravitational constants, whatever, to make objects in your world to WHATEVER you want them to do, but F=ma should still apply :)
  • by shaunak ( 304231 ) <shaunak AT gmx DOT net> on Tuesday December 18, 2001 @01:40PM (#2720746) Homepage
    For those of you interested in the physics of motion (mechanics), I would suggest you pick up the books "Vector Mechanics for Engineers" by Prof. Beer and Prof. Johnston. It is easily the best book for mechanics I have personally read. And it is modular in the sense you can skip some chapters if they're not interesting without going "DUH" while reading the later ones. Although it says 'For engineers,' the books are understandable even if you haven't got too much math experience. If you're scared of vectors, they also have a "Mechanics for Engineers."
  • by pclminion ( 145572 ) on Tuesday December 18, 2001 @02:21PM (#2721103)
    What do you mean by realistically? If you mean, for example, that the sun in your game solar system attracts the spacecraft, and if you wait long enough you'll unfortunately fall into the sun, then I'd say your game is a bit too realistic. The gravitational force on a spacecraft at any significant distance from a star extremely slight in comparison to the forces acting on the craft locally, such as thrust and collisions.

    Even if the gravitational force were extremely strong, it wouldn't make much difference in space battle. You still point the missile at the enemy and shoot it. You, the missile, and the enemy are all accelerating toward the sun at equal rates. The fact that a G-field is present makes absolutely no difference, since the field is uniform, at least when you are quite far from the sun and the spacecraft are relatively close to each other. In a fun game, the craft will not be far apart, since it's not much fun to fire on an an enemy when you can't see the pretty explosion.

    Look, I'm going on and on about a specific example, when my real point was much more general. "Realistic" behavior of objects can be very closely approximated with very simple methods. The player will not know the difference. Problems such as integrations "blowing up" can be easily avoided simply by looking for the blow-up and correcting for it.

  • by Anonymous Coward on Tuesday December 18, 2001 @02:34PM (#2721189)
    In order to willingly take 'programmatic' licence with kinematics, you have to know it. Just like a poet needs to know the language very well before he can successfully use poetic licence.
  • "real" physics?! (Score:3, Insightful)

    by TopherC ( 412335 ) on Tuesday December 18, 2001 @05:19PM (#2722479)
    First, physics is inherently descriptive and approximate anyway, so if there is a "real" physics, it has yet to be discovered. Physicists are always working with approximations and numerical methods, but it's best to go as far as you can with "exact" solutions and generalities. Then, when you need to start doing numerical work, you have some place to start from. You can't do numerical integration unless you know what to integrate.

    Second, the usual 100-level undergrad textbook in physics tells you a lot of things that you probably don't need to know when designing games (like E&M and some quantum mechanics), but also leaves out the more practical aspects of classical mechanics when dealing with less-than-ideal objects. Once you work with the motion of objects that are not spherically symmetric, you need mechanics at the next level, and you need to work with matrices and vectors. This stuff isn't difficult, but it's not in the typical undergrad textbook. And it does require a bit of mathematics, like most things that are worthwhile.

    So it sounds to me like this book does have an important niche to fill, combining undergrad classical mechanics, a sampling of junior-level classical mechanics, and some numerical methods to boot.

Our OS who art in CPU, UNIX be thy name. Thy programs run, thy syscalls done, In kernel as it is in user!

Working...