0% found this document useful (0 votes)
3 views

lecture15 game

game design

Uploaded by

qamarmemon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lecture15 game

game design

Uploaded by

qamarmemon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Principles of Computer Game

Design and Implementation


Lecture 15
We already learned
• Collision Detection
– two approaches (overlap test, intersection test)
– Low-level, mid-level, and high-level view

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

• A program that computes it is an integrator

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

• P(t) = . . . (maths) • Pi+1 = Pi + tpf · V(t)


Main loop iteration Time per frame

13
Recall: Arbitrary Translation Start

Initialise
• Every iteration update the
position
Update Game

P = P + speedŸtpfŸU(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

• V(t) = . . .(maths) • Vi+1 = Vi + tpf · a(t)

Time per
Main loop iteration frame

15
Example: Gravitational Pull
• a(t) = g = 9.8 N/kg
• Vi+1 = Vi + tpf · g g

• Pi+1 = Pi + tpf · Vi+1


Vector3f velocity = new Vector3f(10,10,0);
Vector3f gravity = new Vector3f(0, -9.8f, 0);

public void simpleUpdate() {
velocity = velocity.add(gravity(tpf));
ag.move(velocity.mult(tpf));
}

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)

a(t) = F(t)/m Use more


often for
Example: Engine thrust Fengine = kUV simplicity

Linear drag FD(t) = -bV(t)

Quadratic drag FQD(t) = -c|V(t)|2V (t) 17


Example: Pull + Drag
Fi+1 = bVi
ai+1 = g + Fi+1 /m V

Vi+1 = Vi + tpf · ai+1 FD


g
Pi+1 = Pi + tpf · Vi+1

Vector3f force = velocityB.mult(-b);


accelerationB = gravity.add(force.divide(m));
velocityB =
velocityB.add(accelerationB.mult(tpf));
bg.move(velocityB.mult(tpf));
18
Example: Pull + Drag + Thrust
Unit vector in the direction of V
Fi+1 = bVi + kUV
V
ai+1 = g + Fi+1 /m
thrust
Vi+1 = Vi + tpf · ai+1 FD
g
Pi+1 = Pi + tpf · Vi+1

Vector3f directionC = velocityC.normalize();


Vector3f forceC = velocityC.mult(-b).
add(directionC.mult(thrust));
accelerationC = gravity.add(forceC.divide(m));
velocityC = velocityC.add(accelerationC.mult(tpf));
cg.move(velocityC.mult(tpf));

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

Pi+1 = Pi + tpf · Vi+1

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?

– It’s all about illusion, if the behaviour looks right,


we do not care.

• 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));
}
}

Run it with different vx!! 27


Euler Steps: Advantages and
Disadvantages
• Work well when motion is slow (small
simulation steps) and forces are well-defined
– F, a and V remain same in the time interval
• Does not work well when
– Simulation steps are large
– Approximation errors accumulate
– F, a and V change rapidly over time

Inaccurate for serious applications (e.g. flying a real rocket)


Widely used in computer games for its simplicity
28
If Accuracy Matters
• Use other integration methods
– Typically, much more computationally demanding

• 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

But what will the user think?

30

You might also like