## Playing Around Theoretically

So let's use our preceding example to play with an object falling toward earth. Let's assume that we let the loop iterate ten times and that getDeltaTime() will always return 0.1 seconds. We'll get the following positions and velocities for each iteration:

 time=0 1, position= (0 0, -0 1) , time=0 2, position= (0 0, -0 3) , time=0 3, position= (0 0, -0 6) , time=0 4, position= (0 0, -1 0) , time=0 5, position= (0 0, -1 5) , time=0 6, position= (0 0, -2 1) , time=0 7, position= (0 0, -2 8) , time=0 8, position= (0 0, -3 6) , time=0 9, position= (0 0, -4 5) , time=1 0, position= (0 0, -5 5) ,

velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity velocity

: (0.0, : (0.0, : (0.0, : (0.0, : (0.0, : (0.0, : (0.0, : (0.0, : (0.0, : (0.0,

After 1 second, our object falls 5.5 meters and has a velocity of (0,-10) m/s, straight down to the core of the earth (until it hits the ground, of course).

Our object will increase its downward speed without end, as we don't factor in air resistance. (As I said before, we can easily cheat our own system.) We can just enforce a maximum velocity by checking the current velocity length, which equals the speed of our object.

All-knowing Wikipedia tells us that a human in free fall can have a maximum, or terminal, velocity of roughly 125 miles per hour. Converting that to meters per second (125 x 1.6 x 1000 / 3600), we get 55.5 m/s. To make our simulation more realistic, we can modify the loop as follows:

while(simulationRuns) {

float deltaTime = getDeltaTime(); if(velocity.len() < 55.5)

velocity.add(acceleration.x * deltaTime, acceleration.y * deltaTime); position.add(velocity.x * deltaTime, velocity.y * deltaTime);

As long as the speed of our object (the length of the velocity vector) is smaller than 55.5 m/s, we increase the velocity by the acceleration. When we've reached the terminal velocity, we simply don't increase it by the acceleration anymore. That simple capping of velocities is a trick used heavily in many games.

We could add some wind to the equation by adding another acceleration in the x direction, say (-1,0) m/s2. For this we just need to add up the gravitational acceleration and the wind acceleration before we add it to the velocity:

Vector2 gravity = new Vector2(0,-10); Vector2 wind = new Vector2(-1,0); while(simulationRuns) {

float deltaTime = getDeltaTime(); acceleration.set(gravity).add(wind); if(velocity.len() < 55.5)

velocity.add(acceleration.x * deltaTime, acceleration.y * deltaTime); position.add(velocity.x * deltaTime, velocity.y * deltaTime);

We can also ignore acceleration altogether and let our objects have a fixed velocity. We did exactly this in the BobTest earlier. We changed the velocity of each Bob only if he hit an edge, and we did so instantly.

0 0