7.5 Preserving your physics

As a rule, once you have a cCritter::move that gives you a visually convincing simulation of some reasonably accurate physics, you shouldn't override it at all. If you ignore this rule, you run the risk of developing a game that runs great on your home machine, but which behaves badly when you bring the program in for a classroom demo ? or, if you're working alone, when you try and show it off at a conference or at a friend's house. The critters may seem to be barely moving at all ? or flying around the screen like neutrinos.

All the careful work in calculating a real time dt and passing it to your critters is going to make no difference if you make the cCritter::move(dt) method a virtual method and override it with a method that ignores dt and does something like _position += _velocity. Remember that it's only if you have the proper move code of _position += dt * _velocity that your program is going to adjust the motions of your critters to take into account the actual speed at which your game is running.

It should be possible to make all your changes to your critter motions by adding in forces to get used in the update call to feelforce, or by directly editing the update method.

  • To apply a steady, ongoing force to a critter, give the critter a cForce that returns a non-zero vector.

  • To apply a sharp impulse to a critter, such as when you hit it, change the critter's _velocity by using the setVelocity mutator to make the change. This call can be made from inside a cForce::force or directly within the update method or possibly some other method.

  • To change a critter's position if you want to 'teleport' it from one spot to another, use the moveTo mutator.

We'll talk more about the user controls and the cListener class in Chapter 12: Listeners. For now though, we might as well mention some of the ways that some of our standard listeners change the main player critter's motion in response to an arrow key or a mouse action. Most of the time, a critter using a listener will ignore any acceleration due to forces. We ignore physics in order to have the player critter be fully responsive to the game's user.

  • Arrow key. As long as an arrow key is pressed, set the velocity to the arrow key direction. If no arrow key is pressed, set the velocity to zero. Ignore any acceleration.

  • Scooter. If the Up Arrow key is pressed, set the critter's velocity to be at the maximum speed in the critter's current direction. Pressing the Down Arrow key moves the critter in reverse. If neither the Up nor the Down Arrow key is depressed, the speed immediately drops to zero. The Left and Right Arrow keys rotate the critter's current direction. Ignore any acceleration.

  • Car. As long as the Up key is pressed, add to the critter's forward acceleration. The Left and Right Arrow keys rotate the critter's current direction.

  • Cursor. Move the critter to the current cursor position and give it a velocity that matches the motion from the old position to the new position. Ignore any acceleration.

    Part I: Software Engineering and Computer Games
    Part II: Software Engineering and Computer Games Reference