O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

class SpaceShip
{
private:
vector m_Position;
vector m_Velocity;
float m_fMass;
public:
};
Given the time interval since the last update and a force to be applied, we
can create a method that updates the ship’s position and velocity. Here’s
how:
void SpaceShip::Update(float TimeElapsedSinceLastUpdate, float ForceOnShip)
{
float acceleration = ForceOnShip / m_fMass;
First of all, calculate the acceleration due to the force using equation
(1.93).
m_Velocity += acceleration * TimeElapsedSinceLastUpdate;
Next, update the velocity from the acceleration using equation (1.80).
m_vPosition += m_Velocity * TimeElapsedSinceLastUpdate;
}
Finally, the position can be updated with the updated velocity using equa-
tion (1.77).
Summing Up
This chapter covers a lot of ground. If much of this stuff is new to you,
you’ll be feeling slightly confused and perhaps a little intimidated. Don’t
worry though. Soldier on and, as you read through the book, you’ll see how
each principle is applied to a practical problem. When you see the theory
used in real-world contexts, you’ll find it a lot easier to understand.
40 | Chapter 1
Summing Up
Simplification of Equation (1.90)
Let me show you how that pesky-looking equation is simplified. Here it is
again in all its glory.
First, let’s work on the rightmost term. From the rule shown by equation
(1.29) we can change the equation to read:
We can now tidy up the as a little:
Let’s now dispose of the parentheses in the (v u)
2
term using the rule
given by equation (1.28).
Let’s remove the other parentheses too.
Now to get rid of the fractional parts by multiplying every term by 2a:
Almost there now! We just need to group like terms together.
And rearrange to give the final equation.
A Math and Physics Primer | 41
Summing Up
2
1
2
vu vu
xu a
aa
--
æöæö
D= +
ç÷ç÷
èøèø
2
2
1( )
2
vu vu
xu a
aa
--
æö
D= +
ç÷
èø
2
()
2
vu vu
xu
aa
--
æö
D= +
ç÷
èø
22
2
2
vu v u vu
xu
aa
-+-
æö
D= +
ç÷
èø
222
2
2
uv u v u vu
x
aa
-+-
D= +
222
222
2
22 2
2
222 2
uv u v u vu
ax a a
aa
a x uv u v u vu
æöæ ö
-+-
D= +
ç÷ç ÷
èøè ø
D= - + + -
22
2ax v uD= -
22
2vu ax=+D
This page intentionally left blank.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required