Sunteți pe pagina 1din 51

Game Physics

Introduction to Game Physics


Traditional game physics Particle system Rigid body dynamics Flexible body dynamics Some state-of-art topics Car physics Fluid dynamics Rag-doll physics Physics Rigid body kinematics Newtons Laws Forces Momentum Energy

Introduction to Particle System


Point mass Mass center Newtons Laws Forces Acceleration Velocity Position Easy to implement Good for game FXs Attach a billboard on each particle A basic feature of a game engine Fundamentals of Game Physics

Introduction to Rigid Body Dynamics


Mass of a body Mass center Force Linear momentum Torque Angular momentum Inertia tensor

Reference www-2.cs.cmu.edu/afs/cs/user/baraff/www/pbm
4

Introduction to Flexible Body Dynamics (1/2)


Particle-spring model F=kx Spring force Not a stress-strain model Lack of Elasticity, Plasticity, & Viscous-Elasticity Can be unstable

Introduction to Flexible Body Dynamics (2/2)


Finite element method Solver for ODE/PDE Boundary conditions Energy equation Stress-strain model Very complicated computing process Conservation of energy

Advanced Topics in Game Physics


Fracture mechanics () Fluid dynamics () Car dynamics () Rag-doll physics ()

Game Physics Applications in Games (1/2)


In general Particle system for combat effects First-person shooting Rag-doll system Physical character motion simulation Rigid-body dynamics Fracture mechanics Sports Rag-doll system Physical character motion simulation Fluid dynamics Car racing Car physics Rigid-body dynamics Fracture mechanics

Game Physics Applications in Games (2/2)


Fighting Flexible-body dynamics Cloth simulation Rag-doll Etc

Game Physics Middleware (Physics Engine)


Freeware ODE Open Dynamics Engine (ODE) http://www.ode.org/ Commercial ones Ageia PhysX http://www.ageia.com/ PPU Physics Processing Unit Hardware solution Harvok http://www.havok.com/

10

Differential Equation Basics


Initial value problems Ordinary differential equation Numerical solutions Eulers method The midpoint method Runge-Kutta method

11

Initial Value Problems


An ODE x = f (x, t) where f is a known function . x is the state of the system, x is the xs time derivative x & x are vectors x(t0) = x0, initial condition Vector field Solutions Symbolic solution Numerical solution
Start here Follow the vectors

12

Eulers Method
x(t + t) = x(t) + t f(x, t) A numerical solution A simplification from Tayler series Discrete time steps starting with initial value Simple but not accurate Bigger steps, bigger errors O(t2) errors Can be unstable Not even efficient

13

The Runge-Kutta Method


Runge-Kutta method of order 4 O(h5)

k1 = k2 = k3 = k4 =

h h h h

f(x0, t0) f(x0 + k1/2, t0 + h/2) f(x0 + k2/2, t0 + h/2) f(x0 + k3, t0 + h)

x(t0+h) = x0 + 1/6 k1 + 1/3 k2 + 1/3 k3 + 1/6 k4

14

Inertia
The tendency of an object at rest to remain at rest. The mass is a measure of the inertia of the object. The force is the general mechanism for changing the mechanical state of an object. A force is a vector quantity. An external force is one where source is outside the system of interest. The standard unit of measurement for the magnitude of a force is a newton. A newton is the required force magnitude to give 1kg mass 1m/sec2 acceleration.

15

Newtons Laws (1/3)


First Law In the absence of external forces, an object at rest will remain at rest. If the object is in motion and no external forces acting on it, the object remains in motion with constant velocity. Only forces can change an objects motion.

16

Newtons Laws (2/3)


Second Law For an object with constant mass over time, its acceleration a is proportional to the force F and inversely proportional to the mass m of the object : a = F/m. If the mass changes over time, the more general statement of the law is d dm F= (mv) = ma + v dt dt linear momentum An objects path of motion is determined from applied forces.

17

Newtons Laws (3/3)


Third Law If the force exerted on one object, there is a force with equal magnitude but opposite direction on some other that interacts with it. Action and reaction always occur between interacting objects.

18

Forces
Gravitational forces Spring forces Friction and other dissipative forces

19

Gravitational Forces (1/2)


Given two point masses m and M that have gravitational interaction, they attract each other with forces of equal magnitude but opposite direction, as indicated by Newtons third law.

Fgravity =

GmM r2

G = 6.67 x 10-11 newton-meter2 per kg2 (universal gravitational constant)

20

Gravitational Forces (2/2)


Assume the earths surface is flat and the direction of gravitation force is normal to the plane, the gravitational force exerted on the object by the earth is : F = -mgU g = 9.81 meters per sec2 U as unit-length upward direction

21

Spring Forces
Hookes Law F = -c U c > 0, spring constant U, displacement F pull U > 0 F push U < 0

22

Dissipative Forces
A dissipative force is one for which energy of the system decreases when motion takes place. A simple model for a dissipative force :

F = -c |v|n Two common cases : Friction Viscosity

23

Friction (1/2)
A frictional force between two objects in contact opposes the sliding of one (moving) object over the surface of adjacent object (nonmoving). The friction force is tangent to the contact surface and opposite in direction to the velocity of the moving object. The magnitude of the frictional force is assumed to be proportional to the magnitude of the normal force between surfaces. ck = Ffriction / Fnormal The kinetic friction force is modeled as F = - ck v / |v|, if v is not zero = 0, if v is zero ck is referred to as the coefficient of kinetic friction

24

Friction (2/2)
Static friction cs, coefficient of static friction cs = max(Ffriction)/ Fnormal

1 static case

2 > 1 kinetic case

25

Viscosity
A typical occurrence of this type of force is when an object is dragged through a thick fluid. The force is modeled to have the direction opposite to that of the moving object.

F = -Fdissipative v/|v| = -(c|v|) v/|v| = -cv c>0

26

Torque
Moment of force F is the applied force. r is the position of the applied force relative to the mass center. The torque is the quantity :

=rxF

The torque will generate a local rotation with respect to the mass center. Two forces of equal magnitude, opposite direction, but different lines of action are said to be a couple.

27

Equilibrium
Forces on an object are said to be concurrent if the lines of action all pass through a common point. Balanced forces mean the sum of the forces is zero.

concurrent not balanced

concurrent balanced

F not concurrent not balanced F 2F not concurrent balanced

28

Momentum
Linear momentum Angular momentum

29

Linear Momentum
The linear momentum is the product : p=mv m is the mass v is the velocity For a system of p particles of masses mi and velocity vi for i is in the range of (1, p), the linear momentum is p = mi vi
i=1 p

If the object is a continuum of mass that occupies a region R, the linear momentum is p=

v dm = v dR
R R

dm = dR infinitesimal measurement of mass


30

Angular Momentum
Angular momentum can be treated as a measure of inertia to continue rotating about an axis. L=rxp=rxmv Angular momentum for a set of particles, L = ri x mi vi
i=1 p

If the object is a continuum of mass that occupies a region R, the angular momentum is L=

r x v dm = r x v dR
R R

dm = dR infinitesimal measurement of mass


31

Moment of Inertia
The measure of the rotational inertia of a body about an axis is the moment of inertia. For a single particle, moment of inertia is I = m r2 m is the mass r is the distance to an axis

But for a rigid body object, the inertia will be integrated as a function of mass and the geometry shape. We will discuss it in the section of Rigid-body Dynamics Moment of inertia in one dimension Moment of inertia in two dimension Moment of inertia in three dimension Mass and Inertia tensor of a solid polyhedron

32

Particle Dynamics
Particles are objects with Mass Position Velocity Respond to forces But no spatial extent (no size!) Point mass Based on Newton Laws f = ma .. x=f/m . . v = f / m, v = x

33

Eulers Method for Particle System


Discrete time steps starting with initial value t = t0, t1, t2, Fixed frame rate but adaptive sampling rate Simple but not accurate Bigger steps, bigger errors O(t2) errors Can be unstable Not even efficient If the time step is very tiny Calculate the velocity first Then the position v(t + t) = v(t) + t f(v, t) x(t + t) = x(t) + t f(x, t)

34

Particle States
Position Velocity Mass Life Etc Bounce rate Opacity Color In game application, we always put a billboard object with texture animation on a particle to simulate the geometry shape of the particle.

35

Basic Particle System (1/5)


typedef struct { float m; /* float *x; /* float *v; /* float *f; /* } *Particle; mass */ position */ velocity */ force accumulator */ x v f m states

typedef struct { Particle *p /* array of pointers to particles */ int n; /* number of particles */ float t; /* simulation clock */ } *ParticleSystem;

Particle n time

x v f m

x v f m

x v f m

x x v v f f m m

36

Basic Particle System (2/5)


/* gather states from the particles */ void ParticleGetState(ParticleSystem p, float *dst) { int i; for (i = 0; i < p->n; i++) { *(dst++) = p->p[i]->x[0]; *(dst++) = p->p[i]->x[1]; *(dst++) = p->p[i]->x[2]; *(dst++) = p->p[i]->v[0]; *(dst++) = p->p[i]->v[1]; *(dst++) = p->p[i]->v[2]; } }

37

Basic Particle System (3/5)


/* scatter states into the particles */ void ParticleSetState(ParticleSystem p, float *src) { int i; for (i = 0; i < p->n; i++) { p->p[i]->x[0] = *(src++); p->p[i]->x[1] = *(src++); p->p[i]->x[2] = *(src++); p->p[i]->v[0] = *(src++); p->p[i]->v[1] = *(src++); p->p[i]->v[2] = *(src++); } }

38

Basic Particle System (4/5)


/* calculate derivative, place in dst */ void ParticleDerivative(ParticleSystem p, float *dst) { int i; ClearForce(p); ComputeForce(p); for (i = 0; *(dst++) *(dst++) *(dst++) *(dst++) *(dst++) *(dst++) } } i = = = = = = < p->n; i++) { p->p[i]->v[0]; p->p[i]->v[1]; p->p[i]->v[2]; p->p[i]->f[0]/p->p[i]->m; p->p[i]->f[1]/p->p[i]->m; p->p[i]->f[2]/p->p[i]->m;

39

Basic Particle System (5/5)


/* Euler Solver */ void EulerStep(ParticleSystem p, float DeltaT) { ParticleDeriv(p, temp1); ScaleVector(temp1, DeltaT); ParticleGetState(p, temp2); AddVector(temp1, temp2, temp2); ParticleSetState(p, temp2); p->t += DeltaT; }

40

The Rigid Body


Non-deformable body Within the body, the distance between two points are constant. Rigid body motion Kinematics Dynamics Degree of freedom 3 rotations 3 translations Mass of a body Mass center Force Linear momentum Torque Angular momentum Inertia tensor
41

Inertia Tensor (1/2)

42

Inertia Tensor (2/2)


We always use bounding box to simplify the calculation of the inertia tensor. Rectangle block Cylinder Sphere Pros Simple Cons Inaccurate

43

Calculate Inertia Tensor for a Rectangle Block

44

Body Space & World Space

p(t) = R(t)p0 + x(t)


45

Linear Velocity
We call x(t) and R(t) the position and orientation of the body at time t.

* R(t) is the rotation matrix.

46

Angular Velocity (1/2)


(t) is the angular velocity of the body.

(t) is not R(t)


(t) is a vector. But R(t) is a matrix

47

Angular Velocity (2/2)

48

Rigid Body Equation of Motion

49

Flexible Body Dynamics (1/2)


Deformable body Cloth Flag Sand Hair Solvers Particle-spring model Particles simulate the mass and the inertia. Springs simulate the material. Particle has translation only.

50

Flexible Body Dynamics (2/2)


Spring Hooks Law Rigid-body-spring Model Use rigid body objects or balls to replace the particles. Rigid body has translation and rotation. Translation springs Rotation springs Finite element method

Stress Strain

51

S-ar putea să vă placă și