Скачать презентацию Chapter 4 3 Real-time Game Physics Outline Скачать презентацию Chapter 4 3 Real-time Game Physics Outline

a452c99211db30e64d8d22577d296605.ppt

  • Количество слайдов: 54

Chapter 4. 3 Real-time Game Physics Chapter 4. 3 Real-time Game Physics

Outline n Introduction n Particle Physics n n n n Particle Kinematics Closed-form Equations Outline n Introduction n Particle Physics n n n n Particle Kinematics Closed-form Equations of Motion Numerical Simulation n n Motivation for including physics in games Practical development team decisions Finite Difference Methods Explicit Euler Integration Verlet Integration Brief Overview of Generalized Rigid Bodies Brief Overview of Collision Response Final Comments 2

Real-time Game Physics Introduction Real-time Game Physics Introduction

Why Physics? n The Human Experience n n Real-world motions are physically-based Physics can Why Physics? n The Human Experience n n Real-world motions are physically-based Physics can make simulated game worlds appear more natural Makes sense to strive for physically-realistic motion for some types of games Emergent Behavior n Physics simulation can enable a richer gaming experience 4

Why Physics? n Developer/Publisher Cost Savings n Classic approaches to creating realistic motion: n Why Physics? n Developer/Publisher Cost Savings n Classic approaches to creating realistic motion: n n Artist-created keyframe animations Motion capture Both are labor intensive and expensive Physics simulation: n n n Motion generated by algorithm Theoretically requires only minimal artist input Potential to substantially reduce content development cost 5

High-level Decisions n Physics in Digital Content Creation Software: n n n Many DCC High-level Decisions n Physics in Digital Content Creation Software: n n n Many DCC modeling tools provide physics Export physics-engine-generated animation as keyframe data Enables incorporation of physics into game engines that do not support real-time physics Straightforward update of existing asset creation pipelines Does not provide player with the same emergentbehavior-rich game experience Does not provide full cost savings to developer/publisher 6

High-level Decisions n Real-time Physics in Game at Runtime: n n n Enables the High-level Decisions n Real-time Physics in Game at Runtime: n n n Enables the emergent behavior that provides player a richer game experience Potential to provide full cost savings to developer/publisher May require significant upgrade of game engine May require significant update of asset creation pipelines May require special training for modelers, animators, and level designers Licensing an existing engine may significantly increase third party middleware costs 7

High-level Decisions n License vs. Build Physics Engine: n License middleware physics engine n High-level Decisions n License vs. Build Physics Engine: n License middleware physics engine n n Complete solution from day 1 Proven, robust code base (in theory) Most offer some integration with DCC tools Features are always a tradeoff 8

High-level Decisions n License vs. Build Physics Engine: n Build physics engine in-house n High-level Decisions n License vs. Build Physics Engine: n Build physics engine in-house n n n Choose only the features you need Opportunity for more game-specific optimizations Greater opportunity to innovate Cost can be easily be much greater No asset pipeline at start of development 9

Real-time Game Physics The Beginning: Particle Physics Real-time Game Physics The Beginning: Particle Physics

The Beginning: Particle Physics n What is a Particle? n n n A sphere The Beginning: Particle Physics n What is a Particle? n n n A sphere of finite radius with a perfectly smooth, frictionless surface Experiences no rotational motion Particle Kinematics n n Defines the basic properties of particle motion Position, Velocity, Acceleration 11

Particle Kinematics - Position n Location of Particle in World Space n SI Units: Particle Kinematics - Position n Location of Particle in World Space n SI Units: meters (m) n Changes over time when object moves 12

Particle Kinematics - Velocity and Acceleration n Velocity (SI units: m/s) n n First Particle Kinematics - Velocity and Acceleration n Velocity (SI units: m/s) n n First time derivative of position: Acceleration (SI units: m/s 2) n n First time derivative of velocity Second time derivative of position 13

Newton’s 2 nd Law of Motion n n Paraphrased – “An object’s change in Newton’s 2 nd Law of Motion n n Paraphrased – “An object’s change in velocity is proportional to an applied force” The Classic Equation: n n m = mass (SI units: kilograms, kg) F(t) = force (SI units: Newtons) 14

What is Physics Simulation? n The Cycle of Motion: n n Force, F(t), causes What is Physics Simulation? n The Cycle of Motion: n n Force, F(t), causes acceleration Acceleration, a(t), causes a change in velocity Velocity, V(t) causes a change in position Physics Simulation: n Solving variations of the above equations over time to emulate the cycle of motion 15

Example: 3 D Projectile Motion n Constant Force n n n Weight of the Example: 3 D Projectile Motion n Constant Force n n n Weight of the projectile, W = mg g is constant acceleration due to gravity Closed-form Projectile Equations of Motion: n These closed-form equations are valid, and exact*, for any time, t, in seconds, greater than or equal to tinit 16

Example: 3 D Projectile Motion n Initial Value Problem n n Simulation begins at Example: 3 D Projectile Motion n Initial Value Problem n n Simulation begins at time tinit The initial velocity, Vinit and position, pinit, at time tinit, are known Solve for later values at any future time, t, based on these initial values On Earth: n If we choose positive Z to be straight up (away from center of Earth), g. Earth = 9. 81 m/s 2: 17

Concrete Example: Target Practice Projectile Launch Position, pinit Target 18 Concrete Example: Target Practice Projectile Launch Position, pinit Target 18

Concrete Example: Target Practice n Choose Vinit to Hit a Stationary Target n n Concrete Example: Target Practice n Choose Vinit to Hit a Stationary Target n n n ptarget is the stationary target location We would like to choose the initial velocity, Vinit, required to hit the target at some future time, thit. Here is our equation of motion at time thit: Solution in general is a bit tedious to derive… Infinite number of solutions! Hint: Specify the magnitude of Vinit, solve for its direction 19

Concrete Example: Target Practice n Choose Scalar launch speed, Vinit, and Let: n Where: Concrete Example: Target Practice n Choose Scalar launch speed, Vinit, and Let: n Where: 20

Concrete Example: Target Practice n If Radicand in tanf Equation is Negative: n n Concrete Example: Target Practice n If Radicand in tanf Equation is Negative: n n No solution. Vinit is too small to hit the target Otherwise: n n One solution if radicand == 0 If radicand > 0, TWO possible launch angles, f n n Smallest f yields earlier time of arrival, thit Largest f yields later time of arrival, thit 21

Target Practice – A Few Examples Vinit = 25 m/s Value of Radicand of Target Practice – A Few Examples Vinit = 25 m/s Value of Radicand of tanf equation: 969. 31 Launch angle f: 19. 4 deg or 70. 6 deg 22

Target Practice – A Few Examples Vinit = 20 m/s Value of Radicand of Target Practice – A Few Examples Vinit = 20 m/s Value of Radicand of tanf equation: 60. 2 Launch angle f: 39. 4 deg or 50. 6 deg 23

Target Practice – A Few Examples Vinit = 19. 85 m/s Value of Radicand Target Practice – A Few Examples Vinit = 19. 85 m/s Value of Radicand of tanf equation: 13. 2 Launch angle f: 42. 4 deg or 47. 6 deg (note convergence) 24

Target Practice – A Few Examples Vinit = 19 m/s Value of Radicand of Target Practice – A Few Examples Vinit = 19 m/s Value of Radicand of tanf equation: -290. 4 Launch angle f: No solution! Vinit too small to reach target! 25

Target Practice – A Few Examples Vinit = 18 m/s Value of Radicand of Target Practice – A Few Examples Vinit = 18 m/s Value of Radicand of tanf equation: 2063 Launch angle f: -6. 38 deg or 60. 4 deg 26

Target Practice – A Few Examples Vinit = 30 m/s Value of Radicand of Target Practice – A Few Examples Vinit = 30 m/s Value of Radicand of tanf equation: 668 Launch angle f: 39. 1 deg or 75. 2 deg 27

n Stop Here 28 n Stop Here 28

Real-time Game Physics Practical Implementation: Numerical Simulation Real-time Game Physics Practical Implementation: Numerical Simulation

What is Numerical Simulation? n Equations Presented Above n n n They are “closed-form” What is Numerical Simulation? n Equations Presented Above n n n They are “closed-form” Valid and exact for constant applied force Do not require time-stepping n Just determine current game time, t, using system timer n e. g. , t = Query. Performance. Counter / Query. Performance. Frequency or equivalent on Microsoft® Windows® platforms n n Plug t and tinit into the equations Equations produce identical, repeatable, stable results, for any time, t, regardless of CPU speed and frame rate 30

What is Numerical Simulation? n n The above sounds perfect Why not use those What is Numerical Simulation? n n The above sounds perfect Why not use those equations always? n Constant forces aren’t very interesting n n n Simple projectiles only Closed-form solutions rarely exist for interesting (nonconstant) forces We need a way to deal when there is no closed-form solution… Numerical Simulation represents a series of techniques for incrementally solving the equations of motion when forces applied to an object are not constant, or when otherwise there is no closed-form solution 31

Finite Difference Methods n What are They? n n n The most common family Finite Difference Methods n What are They? n n n The most common family of numerical techniques for rigid-body dynamics simulation Incremental “solution” to equations of motion Derived using truncated Taylor Series expansions See text for a more detailed introduction “Numerical Integrator” n This is what we generically call a finite difference equation that generates a “solution” over time 32

Finite Difference Methods n n n The Explicit Euler Integrator: Properties of object are Finite Difference Methods n n n The Explicit Euler Integrator: Properties of object are stored in a state vector, S Use the above integrator equation to incrementally update S over time as game progresses Must keep track of prior value of S in order to compute the new For Explicit Euler, one choice of state and state derivative for particle: 33

Explicit Euler Integration Vinit = 30 m/s Launch angle, f: 75. 2 deg (slow Explicit Euler Integration Vinit = 30 m/s Launch angle, f: 75. 2 deg (slow arrival) Launch angle, q: 0 deg (motion in world xz plane) Mass of projectile, m: 2. 5 kg Target at <50, 0, 20> meters tinit pinit m. Vinit S = F=Weight = mg Vinit d. S/dt = 34

Explicit Euler Integration Dt =. 2 s Dt =. 1 s Dt =. 01 Explicit Euler Integration Dt =. 2 s Dt =. 1 s Dt =. 01 s 35

A Tangent: Truncation Error n n The previous slide highlights values in the numerical A Tangent: Truncation Error n n The previous slide highlights values in the numerical solution that are different from the exact, closed-form solution This difference between the exact solution and the numerical solution is primarily truncation error Truncation error is equal and opposite to the value of terms that were removed from the Taylor Series expansion to produce the finite difference equation Truncation error, left unchecked, can accumulate to cause simulation to become unstable n This ultimately produces floating point overflow n Unstable simulations behave unpredictably 36

A Tangent: Truncation Error n Controlling Truncation Error n Under certain circumstances, truncation error A Tangent: Truncation Error n Controlling Truncation Error n Under certain circumstances, truncation error can become zero, e. g. , the finite difference equation produces the exact, correct result n n n More often in practice, truncation error is nonzero Approaches to control truncation error: n n n For example, when zero force is applied Reduce time step, Dt Select a different numerical integrator See text for more background information and references 37

Explicit Euler Integration – Truncation Error Lets Look at Truncation Error (position only) Truncation Explicit Euler Integration – Truncation Error Lets Look at Truncation Error (position only) Truncation Error 38

Explicit Euler Integration – Truncation Error (1/Dt) * Truncation Error is a linear (firstorder) Explicit Euler Integration – Truncation Error (1/Dt) * Truncation Error is a linear (firstorder) function of Dt: explicit Euler Integration is First-Order-Accurate in time This accuracy is denoted by “O(Dt)” 39

Explicit Euler Integration Computing Solution Over Time n The solution proceeds step-by-step, each time Explicit Euler Integration Computing Solution Over Time n The solution proceeds step-by-step, each time integrating from the prior state 40

Finite Difference Methods n n n The Verlet Integrator: Must store state at two Finite Difference Methods n n n The Verlet Integrator: Must store state at two prior time steps, S(t) and S(t-Dt) Uses second derivative of state instead of the first Valid for constant time step only (as shown above) For Verlet, choice of state and state derivative for a particle: 41

Verlet Integration n n Since Verlet requires two prior values of state, S(t) and Verlet Integration n n Since Verlet requires two prior values of state, S(t) and S(t-Dt), you must use some method other than Verlet to produce the first numerical state after start of simulation, S(tinit+Dt) Solution: Use explicit Euler integration to produce S(tinit+Dt), then Verlet for all subsequent time steps p a=<0, 0, -g> S =

d 2 S/dt 2 = 42

Verlet Integration n The solution proceeds step-by-step, each time integrating from the prior two Verlet Integration n The solution proceeds step-by-step, each time integrating from the prior two states S(t-Dt) S(t+Dt) n n n For constant acceleration, Verlet integration produces results identical to those of explicit Euler But, results are different when non-constant forces are applied Verlet Integration tends to be more stable than explicit Euler for generalized forces 43

Real-time Game Physics Generalized Rigid Bodies Real-time Game Physics Generalized Rigid Bodies

Generalized Rigid Bodies n Key Differences from Particles n n n Not necessarily spherical Generalized Rigid Bodies n Key Differences from Particles n n n Not necessarily spherical in shape Position, p, represents object’s center-of-mass location Surface may not be perfectly smooth n n Friction forces may be present Experience rotational motion in addition to translational (position only) motion 45

Generalized Rigid Bodies – Simulation n Angular Kinematics n n Additional Object Properties n Generalized Rigid Bodies – Simulation n Angular Kinematics n n Additional Object Properties n n n Orientation, 3 x 3 matrix R or quaternion, q Angular velocity, w As with translational/particle kinematics, all properties are measured in world coordinates Inertia tensor, J Center-of-mass Additional State Properties for Simulation n Orientation Angular momentum, L=Jw Corresponding state derivatives 46

Generalized Rigid Bodies Simulation n Torque n n Analogous to a force Causes rotational Generalized Rigid Bodies Simulation n Torque n n Analogous to a force Causes rotational acceleration n n Cause a change in angular momentum Torque is the result of a force (friction, collision response, spring, damper, etc. ) 47

Generalized Rigid Bodies – Numerical Simulation n Using Finite Difference Integrators n n n Generalized Rigid Bodies – Numerical Simulation n Using Finite Difference Integrators n n n Translational components of state are the same S and d. S/dt are expanded to include angular momentum and orientation, and their derivatives Be careful about coordinate system representation for J, R, etc. Otherwise, integration step is identical to the translation only case Additional Post-integration Steps n Adjust orientation for consistency n n Adjust updated R to ensure it is orthogonal Normalize q Update angular velocity, w See text for more details 48

Collision Response n Why? n n n Performed to keep objects from interpenetrating To Collision Response n Why? n n n Performed to keep objects from interpenetrating To ensure behavior similar to real-world objects Two Basic Approaches n Approach 1: Instantaneous change of velocity at time of collision n Benefits: n n n Visually the objects never interpenetrate Result is generated via closed-form equations, and is perfectly stable Difficulties: n n Precise detection of time and location of collision can be prohibitively expensive (frame rate killer) Logic to manage state is complex 49

Collision Response n Two Basic Approaches (continued) n Approach 2: Gradual change of velocity Collision Response n Two Basic Approaches (continued) n Approach 2: Gradual change of velocity and position over time, following collision n Benefits n n Does not require precise detection of time and location of collision State management is easy Potential to be more realistic, if meshes are adjusted to deform according to predicted interpenetration Difficulties n n Object interpenetration is likely, and parameters must be tweaked to manage this Simulation can be subject to numerical instabilities, often requiring the use of implicit finite difference methods 50

Final Comments n Instantaneous Collision Response n Classical approach: Impulse-momentum equations n n See Final Comments n Instantaneous Collision Response n Classical approach: Impulse-momentum equations n n See text for full details Gradual Collision Response n Classical approach: Penalty force methods n n Resolve interpenetration over the course of a few integration steps Penalty forces can wreak havoc on numerical integration n n Implicit finite difference equations can handle it n n Instabilities galore But more difficult to code Geometric approach: Ignore physical response equations n Enforce purely geometric constraints once interpenetration has occurred 51

Fixed Time Step Simulation n Numerical simulation works best if the simulator uses a Fixed Time Step Simulation n Numerical simulation works best if the simulator uses a fixed time step n n n e. g. , choose Dt = 0. 02 seconds for physics updates of 1/50 second Do not change Dt to correspond to frame rate Instead, write an inner loop that allows physics simulation to catch up with frame rate, or wait for frames to catch up with physics before continuing This is easy to do Read the text for more details and references! 52

Final Comments n Simple Games n n Closed-form particle equations may be all you Final Comments n Simple Games n n Closed-form particle equations may be all you need Numerical particle simulation adds flexibility without much coding effort Collision detection is probably the most difficult part of this Generalized Rigid Body Simulation n n Includes rotational effects and interesting (nonconstant) forces See text for details on how to get started 53

Final Comments n Full-Up Simulation n n The text and this presentation just barely Final Comments n Full-Up Simulation n n The text and this presentation just barely touch the surface Additional considerations n n n n n Multiple simultaneous collision points Articulating rigid body chains, with joints Friction, rolling friction, friction during collision Mechanically applied forces (motors, etc. ) Resting contact/stacking Breakable objects Soft bodies Smoke, clouds, and other gases Water, oil, and other fluids 54