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 the previous chapter was functional but not that challenging. In order to make it more interesting, we need the asteroids to actually move! To give the player a chance to avoid the moving asteroids, we’ll need to make it possible not only to move the spaceship, but also to change its motion -- increasing and decreasing its speed or changing its direction.
This will be a relatively simple coding exercise compared to the last few chapters, but it introduces a completely new branch of math for us: calculus. Calculus is the study of continuously changing quantities. The way physical objects move in the real world is a great example of continuous change. Rather than hopping instantly from “point A” to “point B”, real objects pass through a number of other points between A and B to complete their journey. If we want to create a compelling simulation of, say, an asteroid, we’ll have to respect this fact.
Figure 9.1 : On a journey from “point A” to “point B”, an asteroid should pass through every point on a path between A and B.