Game Physics
Total Page:16
File Type:pdf, Size:1020Kb
3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 3D video games Game Physics Marco Tarini Animation in games but, a caveat on terminology: in some context procedural means “produced by a simple procedure” as opposed to “physically simulated” Non procedural Procedural Assets! Physics engine Fully controlled by Less control artist/designer (dramatic effects!) Realism: depends on Physics-driven artist’s skill realism Does not adapt to Auto adaptation context to context Repetition artefacts Naturally repretition free Game Physics - 1 1 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 Physics simulation in videogames 3D, or 2D “soft” real-time efficiency 1 frame = 33 msec (at 30 FpS) physics = 5% - 30% max of computation time plausibility (not necessarily realism) robustness (should almost never “explode”) Physics engine: intro Game engine module executed at game run time An high-demanding computation on a very limited time budget! …but highly parallelizable “embarrassingly parallel” ==> good fit for hardware support ( just like the Rendering Engine) Game Physics - 1 2 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 Game engine tasks: (Physics simulation!) Dynamics (Newtonian) for object types such as: Rigid bodies Soft bodies “ragdolling” Free-form deformation bodies: Specific solutions for Ropes Cloth Hair… Fluids Air (e.g. wind) etc Collision handling Collision detection Collision response Hardware for Physics engine To exploit a strong parallelism, you need a strongly parallel hardware! Recently: PPU “Physical Processing Unit” HW unit specialized on physics More recently: GP-GPU “General Purpose Graphics Processing Unit” Use of the graphics card for generic tasks (not related with 3D computer graphics) Ex.: Cuda (nVidia) Game Physics - 1 3 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 Software: libraries / SDK HW accelerated (CUDA) HW accelerated (OpenCL) by open source, free, HW accelerated open source, free 2D! open source, free PhysX … Game Physics - 1 4 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 Physics in games: cosmetics or gameplay? Just a graphic accessory? (for realism!) e.g.: particle effects (w/o feedback) secondary animations Ragdolling Or a gameplay component? e.g. physics based puzzles Popular approach in 2D (since always!) Physics in games: cosmetics or gameplay? Just a graphic accessory? (for realism!) e.g.: particle effects (w/o feedback) secondary animations Ragdolling Or a gameplay component? e.g. physics based puzzles Rising trend in 3D Game Physics - 1 5 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 Physics engine: Dynamics Physics simulation (Newtonian) Revision: object = mass Object state: . position and derivative: velocity . (and momentum) . direction and angular velocity . (and angular momentum) State change: . forces => acceleration, torque Reminder: Spatial location of an object 2D Physics 3D Physics Position: Position: (x,y) (x,y,z) Orientation: Orientation: (α) – angle (scalar) quaternion or axis,angle or axis x angle or 3x3 matrix or Euler angles Game Physics - 1 6 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 Newtonian dynamics: summary Actual Rate of change “with mass” What changes the “with mass” object of rate of change location (momentum) 2 2 (d / dt ) (d / dt ) Position 푝 Velocity 푣⃗ Momentum Acceleration Force 푓⃗ ̇ 푝 = (x,y,z) 푣⃗ = 푝̇ 푣⃗ 푚 푎⃗ = 푣⃗ = 푝̈ 푓⃗ = 푎⃗ 푚 ( |푣⃗| = “speed” ) Orientation Angular velocity 휔 Angular momentum Angular acc. α Torque τ (e.g. quaternion) 휔 퐼 τ = 푎⃗ 퐼 퐼 = moment of inertia (for axis) (“mechanic (“rotational inertia”) momentum”) state (is kept! inertia!) Change the state (changes, but only continuously) (no memory) A few constants per object A few quantities associated to each object constants: they don’t (usually) change input of the physical simulation, not output Mass: resistance to change of velocity Moment of Inertia: resistance to change of angular velocity Barycenter: the center of mass Game Physics - 1 7 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 Mass resistance to change of velocity inertial mass also, incidentally: ability to attract every other object gravitational mass happens to be the same what you measure with a scale Unity of measure: kg, g… Moment of inertia Resistance to change of angular velocity low high (an object rotates around its barycenter) Game Physics - 1 8 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 Moment of inertia Scalar moment of inertia Resistance to change of angular velocity Depends on the mass, and on its distribution the farthest one sub-mass from the axis, the > the resistance In 3D: its different for each axis of rotation It can be computed for any axis, thanks to… Moment of inertia as a 3x3 Matrix a matrix A used to extract the scalar, for any given axis given an axis a (a = unit vector), the moment of inertia is aT A a matrix A can be computed once and for all for a rigid object how: that’s beyond this course in practice: use given formulas for common shapes or sum the contributions for each sub-mass Barycenter Aka the center of mass (a position) In the discrete setting: simply the weighted average of the positions of the subparts composing an object (literally “weighted”: with their masses) Does not necessarily coincide with the origin of the local frame of that object (but it can) Game Physics - 1 9 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 State of an object Point position current Rotation orientation updated by Vector velocity physics current rates of change Rotation angular_velocity Scalar mass Matrix moment_of_inertia setup at initialization, Point barycenter constants (rarely) changed Scalar drag e.g. by scripts frictions; … see later Note: acceleration/forces/torques are not part of the state In Point position part of Transform component Rotation orientation Vector velocity the RigidBody component Rotation angular_velocity Scalar mass Matrix moment_of_inertia Adding a “RigidBody” component to a Game Object is to say: Point barycenter “please let the Phys. engine take care of this object” Scalar drag … Game Physics - 1 10 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 In (using Unity terminology) note: they are the components of the global transformation! Vector3 position part of Transform component Quaternion rotation note: speed = velocity.magnitude Vector3 velocity the RigidBody component Quaternion angularVelocity per second float mass (not per frame) Vector3 inertiaTensor Quaternion inertiaTensorRotation Vector3 centerOfMass moment of inertia matrix the Vector3 = a diagonal matrix D float drag by rotating it RTDR the final matrix … the barycenter (in object space) State of a point-particle Point position not used ! Rotation orientation Vector velocity Rotation angular_velocity Scalar mass One trend in game phys engines Matrix moment_of_inertia is to simulate point-particles only. Much simpler! Point barycenter E.g. no rotation needed! Scalar drag We will see later how to still get complex objects back (with “PBD”) … For now, we focus on this simpler case. Game Physics - 1 11 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 Dynamics (Newtonian) describe the forces given the particle positions (and more) f function( p ,...) a fm/ v v adt 0 p p v dt 0 Dynamics (Newtonian) f fun( p,...) forces a f / m v v a dt 0 positions acceler. p p v dt 0 velocity Game Physics - 1 12 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 An (obvious) precisation tC = virtual time != real time e.g.: game paused t costant. Fast forward, replay, rallenty, reverse change of speed/flow direction of t occasionally, gameplay exploit this difference in spectacular ways! PoP – the sands of times serie (Ubisoft, 2003-…) Braid (Jonathan Blow, 2008) Computing physics evolution Analytical solutions: Numerical solutions: state = function( t ) 1. state( t = 0) init 2. state( t + 1) Given force functions (and acc), find the functions (pos, vel,…) in the specified relations: evolve( state ) t f (t ) funz( p(t ),...) C C 3. goto 2 a(tC ) f (tC ) / m tC v(t ) v a(t)dt C 0 0 tC p(t ) p v(t) dt C 0 0 Game Physics - 1 13 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 Analytical solutions ft(C ) function( pt ( C ),...) at() ft ()/ m C C pos, acc, vel, forces: t in function of C current time t vt() v atdt () C C 0 0 tC p() t p v () t dt C 0 0 Analytical solutions that is, find position as function p of time s.t. pt( ) function( pt ( )) / m with sometimes, of other things too (e.g. velocity). p(0) v0 Even harder! p(0) p0 Game Physics - 1 14 3D video gaming - Marco Tarini 2019-03-21 Univ Milano 2019/2020 «ballistic shooting» Simple example: of a mass, analytical solution in 2D, ignoring friction... 0 a is in this specific case: y f m one constant, 9.8 does not depend on pos v v x 0 vy x 0 p0 0 Simple example: analytical solution Solving… f (tC ) fun( p(tC ),...) a(tC ) f (tC ) / m 0 tC f (t ) m v(t ) v a(t) dt C C 0 9.8 0 tC p(t ) p v(t)dt 0 C 0 a(tC ) f (tC ) / m 0 9.8 v tC 0 v v(t ) x dt x C v v 9.8t y 0 9.8 y C tC 0 tC v v t p(t ) p v(t)dt x dt x C C 0 v 9.8t v t 9.8/ 2t 2 0 0 0 y y C C Game Physics - 1 15 3D video gaming - Marco Tarini 2019-03-21 Univ