Artificial Intelligence
Total Page:16
File Type:pdf, Size:1020Kb
4/11/2016 Introduction (1 of 2) • What is game physics? – Computing motion of objects in virtual scene Basic Game Physics • Including player avatars, NPC’s, inanimate objects – Computing mechanical interactions of objects • Interaction usually involves contact (collision) IMGD 4000 – Simulation must be real-time (versus high- precision simulation for CAD/CAM, etc.) With material from: Introduction to Game – Simulation may be very realistic, approximate, or Development, Second Edition. Edited by Steve Rabin. Cengage Learning, 2009. (Chapter 4.3) intentionally distorted (for effect) 2 Introduction (2 of 2) Physics Engines • And why is it important? • Similar buy vs. build analysis as game engines – Buy: – Can improve immersion • Complete solution from day one – Can support new gameplay elements • Proven, robust code base (hopefully) • Feature sets are pre-defined – Becoming increasingly prominent (expected) part of • Costs range from free to expensive high-end games – Build: • Choose exactly features you want – Like AI and graphics, facilitated by hardware • Opportunity for more game-specification optimizations developments (multi-core, GPU) • Greater opportunity to innovate • Cost guaranteed to be expensive (unless features extremely – Maturation of physics engine market minimal) 3 4 Physics Engines Basic Game Physics Concepts • Open source • Why are we studying this? – Box2D, Bullet, Chipmunk, JigLib, ODE, OPAL, OpenTissue, – To use an engine effectively, you need to understand PAL, Tokamak, Farseer, Physics2d, Glaze something about what it’s doing • Closed source (limited free distribution) – You may need to implement small features or – Newton Game Dynamics, Simple Physics Engine, True Axis, extensions yourself PhysX – Cf., owning a car without understanding anything • Commercial about how it works (possible, yes, but ideal?, no) – Havok, nV Physics, Vortex • Examples • Relation to Game Engines – Native, e.g,. C4 – Kinematics and dynamics – Integrated, e.g., UE4 + PhysX – Projectile motion – Pluggable, e.g., C4 + PhysX, jME + ODE (via jME Physics) – Collision detection and response 6 1 4/11/2016 Outline Kinematics (1 of 3) • Introduction (done) • Study of motion of objects without taking into account mass or force • Kinematics (next) • Basic quantities: position, time • Rigid Body Simulation • ... and their derivatives: velocity, acceleration • The Firing Solution • Basic equations: Where: • Collision Detection 1) d = vt t - (elapsed) time 2) v = u + at d - distance (change in position) • Ragdoll Physics v - (final) velocity (change in distance per unit time) 3) d = ut + ½at2 a - acceleration (change in velocity per unit time) 2 2 u - (initial) velocity • PhysX 4) v = u + 2ad Note, equation #3 is the integral of equation #2 with respect to time (see next slide). Equation #4 can be useful. 7 8 Kinematics (2 of 3) Kinematics (3 of 3) Non-accelerated motion Accelerated motion Prediction Example: If you throw a ball straight up into the air with an initial velocity of 10 m/sec, how high will it go? v = 0 2 2 v = u + 2ad d a = -10 u = 10 m/sec (initial speed upward) 2 a = -10 m/sec (approx gravity) v = 0 m/sec (at top of flight) 2 u = 10 d = ut d = ut + ½at 2 Example: Example: 0 = 10 + 2(-10)d 2 d = 5 meters u = 20 m/s, t = 300 s u=0 m/s, a=4m/s , t=3s (Note, answer independent of mass of ball!) d = 20 x 3000 = 6000 m d = 0x3 + 0.5x4x9 = 18m 10 Doing It In 3D Dynamics • Mathematically, consider all quantities • Notice that preceding kinematic descriptions say involving position to be vectors: nothing about why an object accelerates (or why its acceleration might change) d = vt • To get a full “modern” physical simulation you v = u + at need to add two more basic concepts: 2 d = ut + at /2 – force • Computationally, using appropriate 3-element – mass vector datatype • Discovered by Sir Isaac Newton • Around 1700 11 12 2 4/11/2016 Newton’s Laws Motion Without Newton’s Laws 1. A body will remain at rest or continue to • Pac-Man or early Mario style move in a straight line at a constant speed – Follow path with instantaneous changes in speed and unless acted upon by a force. direction (velocity) 2. The acceleration of a body is proportional to the resultant force acting on the body and is in the same direction as the resultant force. 3. For every action, there is an equal and – Not physically possible opposite reaction. • Note - fine for some casual games (especially with appropriate animations) 13 14 Newton’s Second Law How Are Forces Applied? F = ma • May involve contact At each moment in time: – Collision (rebound) F = force vector, in Newton’s – Friction (rolling, sliding) m = mass (intrinsic property of matter), in kg • Without contact a = acceleration vector, in m/sec2 – Rockets/Muscles/Propellers – Gravity Player cares about state of world (position of objects). Equation – Wind (if not modeling air particles) is fundamental driver of all physics simulations. – Magic • Force causes acceleration (a = F/m) • Dynamic (force) modeling also used for autonomous steering behaviors • Acceleration causes change in velocity • Velocity causes change in position 15 16 Computing Kinematics in Real Time Outline start = getTime() // start time p = 0 // initial position u = 10 // initial velocity • Introduction (done) a = -10 d • function update () { // in game loop a = -10 Kinematics (done) now = getTime() t = now – start • Rigid Body Simulation (next) simulate(t) } u, p • The Firing Solution function simulate (t) { d = ut + at2/2 d = (u + (0.5 * a * t)) * t • Collision Detection move object to p + d // move to loc. computed since start } • Ragdoll Physics Note! Number of calls and time values to simulate() depend on (changing) • PhysX game loop time (frame rate) Is this a problem? It can be! For rigid body simulation with colliding forces and friction (e.g., many interesting cases) … 17 18 3 4/11/2016 Rigid-Body Simulation Numerical Simulation of Newtonian • If no rotation, only gravity and occasional Equation of Motion (1 of 2) frictionless collision, basic Kinematic equations are fine • Suppose know position (p), – Closed form solution can be integrated velocity (v) and acceleration (e.g., d = ut + ½at2) (a) at time (ti) and frame time (∆t) • But in many games (and life!), interesting motion involves non-constant forces and collision impulse • Want position at next frame forces time (ti+1) – Don’t know exactly how forces vs. – Unfortunately, often no closed-form solutions are affecting (wind, friction, • What to do? Numerical simulation gravity …) • Can compute: Numerical Simulation techniques for incrementally solving equations of – pi+1 = si + vi * ∆t motion when forces applied to object are not constant, or when otherwise – vi+1 = vi + a * ∆t there is no closed-form solution What is this doing? Instead of integrating a curve, sum over discrete time-slices 19 Numerical Simulation of Newtonian Equation of Motion (2 of 2) Explicit Euler Integration (1 of 2) • Family of numerical sim techniques called finite difference methods – Incremental “solution” to equations of motion • A “one-point” method since solve using – Most common for rigid-body dynamics simulation • Derived from Taylor series expansion of properties interested in properties at exactly one point in time, t, prior (Taylor series are used to estimate unknown functions) to update time, t+Δt S(t+Δt) = S(t) + Δt d/dt S(t) + (Δt)2/2! d2/dt S(t) + … – S(t+Δt) is only unknown value so can solve without solving system of simultaneous equations • In general, not know values of any higher order. Truncate, remove higher terms – Every term on right side is evaluated at t, right S(t+Δt) = S(t) + Δt d/dt S(t) + O(Δt)2 – Can do beyond, but always higher terms before new time t+Δt 2 – O(Δt) is called truncation error • Size is proportional to Δt (step size) • View: S(t+Δt) = S(t) + Δt d/dt S(t) • Can use to update properties (position) – Called “simple” or “explicit” Euler integration new state prior state state derivative 22 Explicit Euler Integration (2 of 2) Explicit Euler Integration Example (1 of 2) • Write numerical integrator over arbitrary properties as change over Vinit = 30 m/s time Launch angle: 75.2 degrees (all motion in xz plane) • For single particle, S = (mV, p) and d/dt S = (F, V) Mass of projectile, m: 2.5 kg – Derivative of position (p) is velocity (V) • i.e., how position changes with respect to time – Derivative of momentum (mass m * V) is force (F) • i.e., how momentum changes with respect to time Position (m) Linear Momentum (kg-m/s) F o rc e (N ) Velocity (m/s) T im e p x p y p z mV x mV y mV z F x F y F z V x V y V z • Integrate state vector of length N 5 .0 0 1 0 .0 0 0 .0 0 2 .0 0 1 9 .2 0 0 .0 0 7 2 .5 0 0 .0 0 0 .0 0 -2 4 .5 3 7 .6 8 0 .0 0 2 9 .0 0 Vector ExplicitEuler(int N, Vector prior_S, Vector deriv_S, float delta_t) Vector new_S[N]; t p mV F=Weight = mg V for (i=0; i<N; i++) init init init init new_S[i] = prior_S[i] + delta_t * deriv_S[i]; return new_S; • Note, for 3D, mV and p have 3 values each – S(t) = (m V ,p ,m V ,p , …, m V ,p ) 1 1 1 2 2 2 N N N S = <mVinit, pinit > dS/dt = <mg,Vinit> – d/dt S(t) = (F1,V1,F2,V2, …,FN,VN) 23 24 4 4/11/2016 Pseudo Code for Numerical Integration (1 of 2) Explicit Euler Integration Example (2 of 2) Vector cur_S[2*N]; // S(t+Δt) Vector prior_S[2*N]; // S(t) Dt = .2 s Dt = .1 s Dt = .01 s Vector S_deriv[2*N]; // d/dt S at time t float mass[N]; // mass of particles 19 .2 0 .0 19 .2025 19 .2025 19 .2025 float t; // simulation time t 0 .0 0 .0 0 .0 0 .0 0 .0 void main() { d 72 .5 24 .53