Title: Euler Steps and Simple Physics
1Euler Steps and Simple Physics
- We often need the most basic physics
- Position, velocity, force, and acceleration
- Springs
- Limits
- Interpolations
- Simple momentum preservation
2How we describe position
- Vector3 position
- All there really is to it.
- Vector3 position
- Coordinate System?
- Usually described in world coordinates, but could
be object coordinates
3Scalar Positions
- A position may be scalar if theres only one
degree of freedom - As the object moves it can only move in one
direction. - Roller coasters, items in a track, etc.
- We have to convert to 3D to display our object
- Position translated to a point in a track system.
That point is in 3D.
Scalar positions are relatively rare in 3D
graphics systems
4Velocity
Velocity Alternatives
- Velocity Vector
- Velocity is described as a vector
- Vector3 velocity
- Usually means velocity is independent of
orientation
- Velocity Scalar
- Velocity is a single number
- float velocity
- Usually means velocity is dependent on orientation
5Examples
- Scalar Velocities
- Aircraft
- Automobiles and wheeled vehicles
- Items locked in a track
- Characters
- Velocity Vectors
- Spacecraft
- Ballistic objects
- Items subject to gravity
Velocity vectors are a more general solution in
many applications
TT
6Velocity vs. Position
- Velocity is the derivative of position
- To update the position given the velocity and a
given amount of time we need to integrate
- Eulers Method
- We update position by adding the velocity times
the time step duration
This is often called an Euler Step. We are
stepping in time.
Vector3
position velocity (float)gameTime.ElapsedGam
eTime.TotalSeconds
TT
7Scalar Velocities
- Velocity might be a scalar
- float velocity
- You cant add a scalar to a vector we need to
convert the scalar velocity to a velocity vector
- We had to point the object the right direction
- A transformation matrix must exist.
- We can determine the direction it is going by
multiplying a vector by this matrix. - The vector should point onward in the object
space.
Vector3 velocityDir Vector3.TransformNormal(new
Vector3(0, 0, 1), transform) position
velocityDir velocity (float)gameTime.ElapsedGa
meTime.TotalSeconds
8XNA Transform functions
Vector3.Transform(vector, matrix) Use to
transform a coordinate (point) Vector3.TransformN
ormal(vector, matrix) Use to transform a
vector
Vector3 velocityDir Vector3.TransformNormal(new
Vector3(0, 0, 1), transform) position
velocityDir velocity (float)gameTime.ElapsedGa
meTime.TotalSeconds
9Acceleration
- Acceleration Vector
- Acceleration is described as a vector
- Vector3 acceleration
- Usually means acceleration is independent of
orientation
- Acceleration Scalar
- Acceleration is a single number
- float acceleration
- Usually means acceleration is dependent on
orientation
10Common Accelerations
- Thrust
- Gravity
- Drag/Wind resistance
- Braking
11Acceleration
- Acceleration is the derivative of velocity
- To update the velocity based on the current
acceleration, we need to integrate
- Also uses Euler Steps
- New velocity is old velocity plus acceleration
times time step
Vector3
velocity acceleration (float)gameTime.Elapsed
GameTime.TotalSeconds
TT
12Scalars vs. Vectors
Once we become a vector, we stay a vector
Which variables need to be persistent? (member
variables)
13Gravity
- Force of gravity is an acceleration vector
- (0, -980, 0) 980cm/sec2
- Clearly, could be different if force is not
down
- Implementation
- Just add to any other acceleration
- acceleration new Vector3(0, -980, 0)
Be careful, though. Gravity may be canceled if
object is on the ground. Ground is an equal and
opposite force.
14Drag
- Simple Linear Drag
- Called Stokes drag
- Drag is linearly proportional to velocity
- We define a drag coefficient b
- Implementation
- Drag is a force, not an acceleration
- force velocity -drag
- acceleration force / mass
Its common to ignore mass in simple systems.
Just assume the mass is 1 or that the
coefficients are assumed to be divided by the
mass already.
15Force and Mass
- Newtons Second Law
- F ma
- Force equals mass times acceleration
- a F / m
- Again, force may be a scalar or a vector
- Mass is sometimes ignored
- Assume any thrust is measured in cm/sec2
- Step 2 works this way
16Order of Operations
Accumulate Forces
Vector3 force engineDirection engineThrust
velocity
-drag Vector3 acceleration force / mass
new Vector3(0, -980,
0) velocity acceleration
deltaTime position velocity deltaTime
Accumulate Accelerations
Velocity Euler Step
Position Euler Step
17Limits
- Clearly, if sitting on the ground, gravity is
canceled - Have to think of how to handle that situation
- Both chairs are subject to gravity, but only one
will be accelerated
- Something we are touching cancels all
acceleration and velocity in that direction - Easy when that direction is (0, -1, 0)
Vector3 acceleration force / mass
new Vector3(0, -980, 0) if (positionlt 0)
acceleration.Y 0
18A More Complete Example
Vector3 force engineDirection
engineThrust velocity -drag
Vector3 acceleration force / mass new
Vector3(0, -980, 0) if (position.Y
lt 0 acceleration.Y lt 0)
acceleration.Y 0 velocity
acceleration deltaTime if
(position.Y lt 0 velocity.Y lt 0)
velocity.Y 0 position
velocity deltaTime if (position.Y
lt 0) position.Y 0
Corrected slide!
19Common interpolations get to point A
- You need to get something to point A in T seconds
- Two things to keep track of
- Where we are going
- How much time is left
Remember, we dont have a loop to compute the
locations. When update is called, we only know
where we are and where we are going and any
another other state we have decided to save.
20Example solution
if (timeRemaining gt 0) float delta
gameTime.ElapsedGameTime.TotalSeconds if
(delta gt timeRemaining) delta
timeRemaining Vector3 direction target -
position float distance direction.Length()
if (distance 0)
timeRemaining 0 else
position direction delta / timeRemaining
timeRemaining - delta
How much to step?
What Direction andhow far?
Take the step.
Consume the time
Works for scalars and vectors. Scalars may be
angles.
TT
21Opening/Closing Actions
float delta gameTime.ElapsedGameTim
e.TotalSeconds if (deployed
wingAngle lt 0.20f)
wingAngle (float)(0.20 delta /
wingDeployTime) if(wingAngle gt
0.20f) wingAngle 0.20f
else if(!deployed
wingAngle gt 0)
wingAngle - (float)(0.20 delta /
wingDeployTime) if(wingAngle lt
0) wingAngle 0
Code from Step 2
The difference is we expect a constant movement
rate rather than a constant time
22General Linear Interpolation
- Going from A to B in T seconds
- Amount of time that has elapsed t
- Where should we be?
Youll use this a lot! A, B, and p can be vectors
or scalars
23Simple Momentum (really velocity) Preservation
- Rock becomes bunch of rocks
- How do we ensure this looks right?
- Cloud of rock should be moving at same velocity
as original rock - But, each has its own velocity
- How would you determine the velocity of the cloud?
24Velocity Preservation
- Average the velocity vectors for all objects
- Sum and divide by the count
- Add to every object
- desiredVelocity - currentVelocity
How would real momentum preservation be different
than this?
25Springs
- Springs are a handy thing
- Springs allow us to attach things softly rather
than rigidly. Well use this to make a chase
camera
Desired Position
- Spring Parameters
- Stiffness How much force per unit of length
- Damping How much resistance there is to the
spring collapsing
Current Position
Damping keeps the spring from oscillating
26Spring Equations
Desired Position
- Force is the sum of a positive pull and negative
damping - Damping is dependent on velocity
// Calculate spring force
Vector3 stretch desiredPosition - position
Vector3 force stiffness stretch -
damping velocity // Apply
acceleration Vector3 acceleration
force / mass velocity
acceleration elapsed // Apply
velocity position velocity
elapsed
Current Position
27Simple Collision Detection
- Bounding Sphere
- Minimum sphere that contains an object
- Advantages
- Invariant under rotation
- XNA support
Well examine more complex methods later.
28Physics well do later
- This is the most basic stuff, all linear
positions and velocities - Later well do
- Rotation (10x as hard as translation)
- Impact/bounce
- More general mass/spring systems