9 Simulating moving objects

 

This chapter covers

  • Implementing Newton’s laws of motion in code to simulate realistic motion
  • Calculating velocity and acceleration vectors
  • Using Euler’s method to approximate the position of a moving object
  • Finding the exact trajectory of a moving object with calculus

Our asteroid game from chapter 7 was functional but not that challenging. In order to make it more interesting, we need the asteroids to actually move! And, to give the player a chance to avoid the moving asteroids, we need to make it possible to move and steer the spaceship as well.

To implement motion in the asteroid game, we’ll use many of the same calculus concepts from chapter 8. The numerical quantities we’ll consider are the x and the y positions of the asteroids and of the spaceship. If we want the asteroids to move, these values are different at different points in time, so we can consider them to be functions of time: x(t) and y(t). The derivative of a position function with respect to time is called velocity, and the derivative of velocity with respect to time is called acceleration. Because we have two position functions, we have two velocity functions and two acceleration functions. This allows us to think of velocities and accelerations as vectors, as well.

9.1 Simulating a constant velocity motion

9.1.1 Adding velocities to the asteroids

9.1.2 Updating the game engine to move the asteroids

9.1.3 Keeping the asteroids on the screen

9.1.4 Exercises

9.2 Simulating acceleration

9.2.1 Accelerating the spaceship

9.3 Digging deeper into Euler’s method

9.3.1 Carrying out Euler’s method by hand

9.3.2 Implementing the algorithm in Python

9.4 Running Euler’s method with smaller time steps

9.4.1 Exercises

Summary