lecture15 game
lecture15 game
2
Collision Response
• What happens after a collision is detected?
1. Prologue
• Check if collision should be ignored
• Sound / visual effects
2. Collision
• Resolve collision
3. Epilogue
• Propagate the effects
– destroy object(s), play sound…
3
Collision Resolution
• Animation based
– An artist models collision
• A rocket hits a target…
– Motion-capture
• Sport games
• Physics based
– Generated by an algorithm
– Based on (more or less) realistic models
4
Recall: Classic Game Structure
• A convexity
• Starts with a single choice, widens to many
choices, returns to a single choice
5
Why Physics?
• Responsive behaviour
– Infinitely many possibilities
• For centauries people were describing the
world
– We can use the equations to model the world
• Can be hard
– Knowledge of physics
– “Real” physics is too expensive computationally
6
“Motion Science” in Games
• Kinematics
– Motion of bodies without considering
forces, friction, acceleration,…
– Not realistic
• Dynamics
– Interaction with forces and torques
7
Keep It Simple
Separate translation and rotation
• Particle physics
– A sphere with a perfect smooth,
frictionless surface. No rotation
– Interaction with forces and environment
• Position, Velocity, Acceleration
• Solid body physics
– Torques, angular velocity, angular
momentum
8
Continuous Motion
• Particles move in a “smooth way”
– Position as a function of time
P(t) is the position of P in the moment t
– The derivative
d P(t)
dt
describes how P(t) changes over time
• Velocity (speed)
V
P(t)
9
Discrete Particle Motion
• Uniform motion
– Nothing affects the
motion
• Gravitational pull
10
Integrators
• The process of computing the position of a
body based on forces and interaction with
other bodies in called integration
11
Newton’s Laws
1. Every body remains in a state of rest or
uniform motion unless it is acted on by an
external force
2. A body of mass m subject to force F
accelerates as described by Vectors
F = ma
3. Every action has an equal and opposite
reaction
12
Position and Velocity
Continuous physics Discrete physics
d P(t)
• V(t) = dt • V(t) = P(t) = Pi+1 Pi
t tpf
13
Recall: Arbitrary Translation Start
Initialise
• Every iteration update the
position
Update Game
P = P + speedtpfU(t)
Draw Scene
Are we
• U(t) - the direction of movement done?
– Depends on time!!
Cleanup
• speed is speed
• tpf is time per frame End
14
Velocity and Acceleration
Continuous physics Discrete physics
• a(t) = d V(t) • a(t) = V(t) = Vi+1 Vi
dt t tpf
Time per
Main loop iteration frame
15
Example: Gravitational Pull
• a(t) = g = 9.8 N/kg
• Vi+1 = Vi + tpf · g g
16
Acceleration and Force
Newton’s second law: a body of mass m subject
to force F accelerates as described by
F(t) = ma(t)
19
Simulation Recipe
• Add up all the forces acting on the object
– Gravity, drag, thrust, spring pull,…
• Represent the motion as discrete steps
ai+1 = g + Fi+1 /m
Vi+1 = Vi + tpf · ai+1 Euler steps
20
Rotation
• Rotation of a uniform (again simplification)
solid body can be described mathematically
– Speed vs angular speed
– Force vs torque
• Represent as discrete motion
• Use Euler steps to compute the rotation
matrix
• Combine with translation
Hard but doable
21
Accuracy of Simulation
• How accurate this simulation is?
• Does it matter?
• But…
22
Physics: Prediction
• Consider the targeting problem: a gun takes
aim at a target
– Given: S – distance to the target
– Compute the bullet velocity vector
• Incomplete information
S 23
Targeting Problem (1)
• Consider horizontal and vertical components
of the velocity vector V
• Assume that
– the horizontal component is given and
– it does not change (no wind / drag)
• Flying time is S
V
tflying =
Vv Vh
Vh
S 24
Targeting Problem (2)
• Vertically, the motion is up and down
Vv (t) = Vv gt
• Assume that
– the gun and target are levelled
• At the highest point Vv (t) = 0
– time to the highest point is half the flying time
V
Vv
g Vh
S 25
Targeting Problem (3)
• Thus, 0 = Vv g(tflying )/2
S
tflying =
Vh
gS
Vv =
2Vh
V
Vv
g Vh
S 26
HelloAiming
float distance = 100f;
bullet.setLocalTranslation(0, 0, 0);
target.setLocalTranslation(distance, 0, 0);
...
float vx = 20f; X-component of
float vy = (g*distance) / (2*vx); velocity vector.
“Horizontal” speed.
velocity = new Vector3f(vx,vy,0);
...
pubic void simpleUpdate() {
if(bullet.getLocalTranslation().getY() >= 0 ) {
velocity = velocity.add(gravity.mult(tpf));
bullet.move(velocity.mult(tpf));
}
}
• Cheat
– E.g. in our aiming example, if the bullet speed is
high, consider it travel along a straight line
– Adjust its position if necessary
S 29
Computer Science Approach:
Iterations
• Shoot at will
• See where it land
• If undershot, increase power
• If overshot, decrease power
30