Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Game Programming 11 - Game Physics

2,310 views

Published on

Chapter 11 of the lecture Game Programming taught at HAW Hamburg.

Introduction to kinematics and dynamics, numerical integration, rigid bodies, collision detection and resolving.

Published in: Technology

Game Programming 11 - Game Physics

  1. 1. Game Programming Physics Nick Prühs
  2. 2. Objectives • To understand the basics of kinematics and dynamics in games • To get an overview of a simple numeric integration approach for phyics • To learn how to resolve rigid body collisions 2 / 83
  3. 3. Motivation • Next thing to make your game feel right, besides graphics and sound • Can be integral part of your gameplay • Usually just a close approximation to real physics will be enough “Speedy thing goes in, speedy thing comes out.” - GLaDOS 3 / 83
  4. 4. Kinematics vs. Dynamics • Kinematics is the study of movement over time. ▪ Doesn’t matter why things are where there are now ▪ Doesn’t matter what causes the movement ▪ Just deals with the actual movement itself • Dynamics is the study of forces and masses that cause kinematic quantities to change over time. 4 / 83
  5. 5. Kinematics – Velocity Velocity is the rate of change of position over time. 5 / 83 𝒗 = 𝒅𝒙 𝒅𝒕
  6. 6. Kinematics – Acceleration Acceleration is the rate of change of velocity over time. 6 / 83 𝒂 = 𝒅𝒗 𝒅𝒕
  7. 7. Change of velocity Solving for v and integrating yields the velocity after a given time t, aside from some unknown constant C: 7 / 83 𝐚 = 𝐝𝐯 𝐝𝐭 𝒅𝒗 = 𝒂 𝒅𝒕 𝒗(𝒕) = න 𝒂 𝒅𝒕 𝒗(𝒕) = 𝒂𝒕 + 𝑪
  8. 8. Change of velocity We can find the unknown constant to be the initial velocity by computing the initial velocity: 8 / 83 𝒗 = 𝒂𝒕 + 𝑪 𝒗 𝟎 = 𝟎𝒂 + 𝑪 𝒗 𝟎 = 𝑪
  9. 9. Change of velocity Thus, given the acceleration a and initial velocity v0, the velocity after any given time t is 9 / 83 𝒗(𝒕) = 𝒂𝒕 + 𝒗 𝟎
  10. 10. Change of position The position after any given time t can be found the same way: 10 / 83 𝐯 = 𝐝𝐱 𝐝𝐭 𝒅𝒙 = 𝒗 𝒅𝒕 𝒅𝒙 = 𝒂𝒕 + 𝒗 𝟎 𝒅𝒕 𝒙(𝒕) = න 𝒂𝒕 + 𝒗 𝟎 𝒅𝒕 𝒙(𝒕) = 𝟏 𝟐 𝒂𝒕 𝟐 + 𝒗 𝟎 𝒕 + 𝒙 𝟎
  11. 11. Kinematics – Momentum Momentum is the product of the mass and velocity of an object. 11 / 83 𝒑 = 𝒎𝒗
  12. 12. Dynamics – Force Force is the rate of change of momentum over time (Newton’s Second Law). 12 / 83 𝑭 = 𝒅𝒑 𝒅𝒕
  13. 13. Change of acceleration For constant mass, force and acceleration are related as follows: 13 / 83 𝐹 = 𝐝𝐩 𝐝𝐭 definition force = 𝒅 𝒎𝒗 𝒅𝒕 definition momentum = 𝒎 𝒅𝒗 𝒅𝒕 constant mass = 𝒎𝒂 definition acceleration
  14. 14. Numerical Integration • Start at a certain initial position and velocity • Take a small step forward in time to find the velocity and position at the next time value • Do this repeatedly to go forward in time in small increments, each time taking the results of the previous integration as the starting point for the next 14 / 83
  15. 15. Explicit Euler Integration C# 15 / 83 // Fixed time step and constant force. const float dt = 1; const float force = 10.0f; // Create new body without initial velocity. var body = new Body { Mass = 1.0f, Position = 0.0f, Velocity = 0.0f }; // Simulate ten steps. for (float t = 1; t <= 10; t++) { body.Position += body.Velocity * dt; var acceleration = force / body.Mass; body.Velocity += acceleration * dt; }
  16. 16. Explicit Euler Integration t position velocity 1 0 10 2 10 20 3 30 30 4 60 40 5 100 50 6 150 60 7 210 70 8 280 80 9 360 90 10 450 100 16 / 83 Explicit Euler integration with dt = 1
  17. 17. Inaccuracy 𝒙 = 𝟎. 𝟓𝒂𝒕 𝟐 + 𝒗𝒕 + 𝒙 𝟎 with 𝒂 = 𝟏𝟎, 𝒕 = 𝟏𝟎, 𝒗 = 𝟎, 𝒙 𝟎 = 𝟎 = 0.5 × 10 × 102 + 0𝑡 + 0 = 0.5 × 10 × 100 = 500 17 / 83 Exact physical position at t = 10 is: This implies an error of (500 – 450) / 500 = 10% after only ten seconds for dt = 1!
  18. 18. Explicit Euler Integration t position velocity 1 4.5 10 2 19 20 3 43.5 30 4 78 40 5 122.5 50 6 177 60 7 241.5 70 8 316 80 9 400.5 90 10 495 100 18 / 83 Explicit Euler integration with dt = 0.1
  19. 19. Variable vs. fixed time steps Usually, we’re working with variable time steps in game simulations: However, this approach has major drawbacks in when simulating physics. 19 / 83 public void Update(float deltaTime) { // Do something awesome here... }
  20. 20. Variable time steps in physics • Physics will “feel” slightly different depending on your framerate • Fast objects won’t collide as expected • Spring simulation will explode to infinity 20 / 83
  21. 21. Fixed time steps in physics • In order to ensure a fixed time step that feels right, we need to have the physics simulation … ▪ Don’t update too often if frames are rendered very fast ▪ Catch up if frames are rendered very slowly • This is achieved by accumulating deltas across frames, updating several times per frame if necessary. 21 / 83
  22. 22. Fixed time steps in physics C# 22 / 83 var random = new Random(); // Fixed time step and constant force. const float fixedDt = 1f / 60f; const float force = 10.0f; float totalTime = 0.0f; float accumulatedDt = 0.0f; // Create new body without initial velocity. var body = new Body { Mass = 1.0f, Position = 0.0f, Velocity = 0.0f }; // Simulate ten steps. for (int t = 0; t <= 10; t++) { // Random delta. float dt = (float)random.NextDouble() / 45; totalTime += dt; accumulatedDt += dt; while (accumulatedDt > fixedDt) { var acceleration = force / body.Mass; body.Velocity += acceleration * fixedDt; body.Position += body.Velocity * fixedDt; accumulatedDt -= fixedDt; } }
  23. 23. Fixed time steps in physics t dt accumulatedTime position velocity 0 0.022 0.022 0 0 0 0.022 0.005 0.003 0.167 1 0.020 0.026 0.003 0.167 1 0.020 0.009 0.008 0.333 2 0.005 0.014 0.008 0.333 3 0.003 0.017 0.008 0.333 3 0.003 0 0.017 0.500 4 0.011 0.011 0.017 0.500 5 0.019 0.030 0.017 0.500 5 0.019 0.013 0.028 0.667 23 / 83 Fixed time steps with dt = 1 / 60 = 0.016
  24. 24. Gotcha! Accumulated time steps can cause an infinite loop if your physics simulation takes more time than your fixed time step! Clamp at a maximum number of simulation steps per frame to avoid this. 24 / 83
  25. 25. Rigid bodies • All of the above assumes a constant mass concentrated in a single point • However, in games we have to deal with bodies having their mass distributed over their area (or volume) • Rigid bodies are shapes that don’t change or deform during physics simulation • We’ll focus on these for the time being 25 / 83
  26. 26. Rigid bodies • For the time being, we’ll model our rigid body as a set of point masses • The total momentum of the rigid body equals the sum of all momentums of all points that make up that body 𝑝 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 = ෍ 𝑖 𝑚𝑖 𝑣𝑖 26 / 83
  27. 27. Center of mass We define the center of mass of a rigid body as the linear combination of the position vectors of all points that make up that body, weighted by their masses, divided by the total mass of the body. 𝑥 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 = σ𝑖 𝑥𝑖 𝑚𝑖 𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 27 / 83
  28. 28. Center of mass Let’s modify this equation a bit: 28 / 12 𝑥 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 = σ𝑖 𝑥𝑖 𝑚𝑖 𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑥 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 = ෍ 𝑖 𝑥𝑖 𝑚𝑖 multiplied with 𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑑(𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑥 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠) 𝑑𝑡 = ෍ 𝑖 𝑑(𝑥𝑖 𝑚𝑖) 𝑑𝑡 𝑑/𝑑𝑡 = ෍ 𝑖 𝑚𝑖 𝑑𝑥𝑖 𝑑𝑡 constant mass = ෍ 𝑖 𝑚𝑖 𝑣𝑖 definition velocity = 𝑝 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 definition momentum
  29. 29. Center of mass Now, let’s take a look at the second part again: 29 / 83 𝑑(𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑥 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠) 𝑑𝑡 = 𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑑𝑥 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 𝑑𝑡 constant mass = 𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑣 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 definitio n velocity
  30. 30. Center of mass Combining both results yields a stunning property of the center of mass! 𝒑 𝑹𝒊𝒈𝒊𝒅𝒃𝒐𝒅𝒚 = 𝑴 𝑹𝒊𝒈𝒊𝒅𝒃𝒐𝒅𝒚 𝒗 𝑪𝒆𝒏𝒕𝒆𝒓𝑶𝒇𝑴𝒂𝒔𝒔 30 / 83
  31. 31. Center of mass Combining both results yields a stunning property of the center of mass! 𝒑 𝑹𝒊𝒈𝒊𝒅𝒃𝒐𝒅𝒚 = 𝑴 𝑹𝒊𝒈𝒊𝒅𝒃𝒐𝒅𝒚 𝒗 𝑪𝒆𝒏𝒕𝒆𝒓𝑶𝒇𝑴𝒂𝒔𝒔 For finding the momentums of any rigid body, we can treat that body as single point mass and velocity. 31 / 83
  32. 32. Center of mass This further applies to forces, as well: 32 / 83 𝐹𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 = 𝑑𝑝 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑑𝑡 = 𝑑(𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑣 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠) 𝑑𝑡 as we’ve just proven = 𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑑𝑣 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 𝑑𝑡 constant mass = 𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑎 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 definition acceleration
  33. 33. Center of mass This further applies to forces, as well: We can treat all forces acting our rigid body as if their sum is acting on a point at the center of mass with the mass of the entire body. 33 / 83 𝐹𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 = 𝑑𝑝 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑑𝑡 = 𝑑(𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑣 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠) 𝑑𝑡 as we’ve just proven = 𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑑𝑣 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 𝑑𝑡 constant mass = 𝑀 𝑅𝑖𝑔𝑖𝑑𝑏𝑜𝑑𝑦 𝑎 𝐶𝑒𝑛𝑡𝑒𝑟𝑂𝑓𝑀𝑎𝑠𝑠 definition acceleration
  34. 34. Rotation • So far, we’ve been talking all about linear momentum and linear acceleration. • Now, we want to figure out how forces applied to our rigid bodies make them rotate. • Where these forces are applied to the body will play an important role. 34 / 83
  35. 35. Kinematics – Orientation The orientation Ω of an object is the angular difference between the world coordinate system and a coordinate system fixed in that object, in radians. 35 / 83
  36. 36. Kinematics – Angular Velocity Angular Velocity is the rate of change of orientation over time. 36 / 83 𝝎 = 𝒅Ω 𝒅𝒕
  37. 37. Kinematics – Angular Acceleration Angular Acceleration is the rate of change of angular velocity over time. 37 / 83 𝜶 = 𝒅 𝜔 𝒅𝒕
  38. 38. Linear Velocity from Angular Velocity • We often need to find the velocity of an arbitrary point on our object ▪ i.e. velocity of colliding points to compute how hard they hit each other • Without rotation, the velocity of any point in the body is the same ▪ Velocity of the center of mass • With rotation, every point might have a different velocity ▪ Obviously, we can’t keep track of the velocity of each of the infinity of points 38 / 83
  39. 39. Linear Velocity from Angular Velocity Claim: The linear velocity of any point P inside an object that is rotating about its origin O, but not translating, is given by the following equation: 39 / 83 𝒗 𝑷 = 𝝎𝑶𝑷⊥
  40. 40. Hint In 2D, the perpendicular of a vector (x, y) is (-y, x). 40 / 83
  41. 41. Linear Velocity from Angular Velocity - Proof 𝝎𝑶𝑷⊥ has the correct magnitude, because and Ω 𝑶𝑷 is the length P is moving when moving Ω radians along the arc whose radius vector is 𝑶𝑷 , by definition of radians. 41 / 83 𝜔𝑂𝑃⊥ = 𝜔 𝑂𝑃⊥ = 𝜔 𝑂𝑃 perpendiculary doesn’t change length = 𝑑Ω 𝑑𝑡 𝑂𝑃 definition 𝜔 = 𝑑(Ω 𝑂𝑃 ) 𝑑𝑡 𝑂𝑃 is constant
  42. 42. Linear Velocity from Angular Velocity - Proof 𝝎𝑶𝑷⊥ has the correct direction, because a point rotating around another fixed point can only move perpendicularly to the vector between the points, or the movement wouldn’t be a simple rotation. 𝝎𝑶𝑷⊥ has the correct sign, because we’re measuring Ω in the counterclockwise direction. ω is positive when the point is rotating counterclockwise. The perpendicular operator points in the counterclockwise direction relative to the radius vector. 42 / 83
  43. 43. Linear Velocity from Angular Velocity The linear velocity of any point P inside an object that is rotating about its origin O, but not translating, is given by the following equation: 43 / 83 𝒗 𝑷 = 𝝎𝑶𝑷⊥
  44. 44. Linear Velocity from Angular Velocity The linear velocity of any point P inside an object that is rotating about its origin O, and is translating, is given by the following equation: 44 / 83 ?
  45. 45. Chasles’ Theorem • Chasles’ Theorem breaks up motion into linear and angular components. • We consider any movement of our rigid body as ▪ translating a single point in the body ▪ rotating the rest of the body around that point 45 / 83
  46. 46. Chasles’ Theorem The linear velocity of any point P inside a moving object that is rotating about its origin O is given by the following equation: (without proof) 46 / 83 𝒗 𝑷 = 𝒗 𝑶 + 𝝎𝑶𝑷⊥
  47. 47. Kinematics – Angular Momentum The Angular Momentum of a point P tells us how much of the linear momentum pP of P is rotating around the origin. 47 / 83 𝑳 𝑶𝑷 = 𝑶𝑷⊥ × 𝒑 𝑷
  48. 48. Kinematics – Angular Momentum The Angular Momentum of a point P tells us how much of the linear momentum pP of P is rotating around the origin. Note how angular momentum of a point P needs a reference (here: O), in contrast to linear momentum. 48 / 83 𝑳 𝑶𝑷 = 𝑶𝑷⊥ × 𝒑 𝑷
  49. 49. Dynamics – Torque Torque is the rate of change of angular momentum over time. 49 / 83 𝝉 𝑶𝑷 = 𝒅 𝑳 𝑶𝑷 𝒅𝒕
  50. 50. Dynamics – Torque We can use the torque to determine how much of the force applied at point P is causing the object to rotate: 50 / 83 𝜏 𝑂𝑃 = dLOP dt definition torque = d(OP⊥× pP) dt definition angular momentum = OP⊥ × dpp dt + dOP⊥ dt × pp product rule = (OP⊥ × 𝐹𝑃) + (vP × 𝑝P) def. linear force, def. linear velocity = OP⊥ × 𝐹𝑃 velocity and momentum of P are parallel
  51. 51. Calculating Angular Momentum Again, just like change in velocity can be numerically integrated using acceleration, change in angular momentum can be integrated using torque, from an applied force and position of application: 𝐿 𝑂𝑃 𝑡 = න 𝜏 𝑂𝑃 𝑑𝑡 = න OP⊥ × 𝐹𝑃 𝑑𝑡 51 / 83
  52. 52. Moment of Inertia • The moment of inertia I of an object is a measure of how hard it is to rotate the object about its center of mass. • It is the sum of the squared distances from the center of mass to each other point in the body, scaling each squared distance by the mass of the respective point. 𝐼 = ෍ 𝑖 𝑚𝑖Oi⊥ 2 52 / 83
  53. 53. Moment of Inertia The moment of inertia can be used to derive the total angular momentum: 𝐿 = ෍ 𝑖 Oi⊥ × pi definition angular momentum = ෍ 𝑖 Oi⊥ × (𝑚𝑖 𝑣𝑖) Definition linear momentum = ෍ 𝑖 Oi⊥ × (𝑚𝑖 𝜔𝑂𝑖⊥) Linear velocity from angular velocity = 𝜔 ෍ 𝑖 Oi⊥ × (𝑚𝑖 𝑂𝑖⊥) Angular velocity same for all points i = 𝜔 ෍ 𝑖 𝑚𝑖Oi⊥ 2 = 𝜔𝐼 definition moment of intertia
  54. 54. Hint As the moment of inertia is based on the mass and relative position of all points of a rigid body, only, it is constant and has to be computed only once! 54 / 83
  55. 55. Dynamics – Torque Integration shows how torque and angular acceleration are related: 𝜏 = 𝑑𝐿 𝑑𝑡 definition torque = 𝑑𝜔𝐼 𝑑𝑡 as we’ve just proven = 𝐼 𝑑𝜔 𝑑𝑡 moment of inertia constant = 𝐼𝛼 Definition angular acceleration 55 / 83
  56. 56. Dynamics – Torque Thus, knowing the torque on our body, we can compute angular acceleration, and then find angular velocity and orientation by numeric integration. 𝝉 = 𝑰𝜶 56 / 83
  57. 57. Full Physics Simulation – Setup For each rigid body: 1. Calculate center of mass and moment of inertia at the center of mass. 2. Set initial position, orientation, linear velocity and angular velocity. 57 / 83
  58. 58. Full Physics Simulation – Loop For each rigid body: 1. Collect all forces on the body, including their points of application. 2. Sum all forces and compute linear acceleration. 3. Compute the torque caused by each force. 4. Sum all torque and compute angular acceleration. 5. Numerically integrate linear acceleration and angular acceleration to update linear velocity and angular velocity, and position and orientation. 58 / 83
  59. 59. Hint Usually, games will treat both mass and moment of inertia as properties of the rigid body. 59 / 83
  60. 60. Collision Response • Given we know that there is a collision, the task is to find out how to handle that collision. • We need to decide where the colliding objects move, and if and how they start spinning. • By now, velocities never changes instantly, but by means forces applied over time, only. 60 / 83
  61. 61. Impulse • An impulse changes the momentum (and thus, the velocity) of a rigid body instantly, without the need of integration over time. • We’re going to use Newton’s Law of Restitution for Instantaneous Collisions with No Friction to find the impulses to apply in case of a collision. 61 / 83
  62. 62. Newton’s Law of Restitution for Instantaneous Collisions with No Friction • Instantaneous: in no time • Restitution: coefficient of restitution models the compression and restitution of impacting bodies with a single scalar • No friction: impulse is entirely pointed in the direction of the collision normal 62 / 83
  63. 63. Collision Data • Collision point P • Center of mass of both bodies A, B • Velocity of the collision point of both bodies 𝑣 𝐴, 𝑣 𝐵 • Collision normal n 63 / 83
  64. 64. Derived Collision Data Relative velocity 𝑣 𝐴𝐵 = 𝑣 𝐴 − 𝑣 𝐵 Relative normal velocity 𝑣 𝐴𝐵 𝑛 = 𝑣 𝐴 − 𝑣 𝐵 𝑛 64 / 83
  65. 65. Coefficient of Restitution • The Coefficient of Restitution 𝜖 tells us how much of the incoming energy is dissipated during the collision. • 𝜖 = 1 yields a totally elastic collision (super ball) • 𝜖 = 0 yields a totally plastic collision (all energy absorbed) 𝑣 𝐴𝐵 ′ 𝑛 = −𝜖𝑣 𝐴𝐵 𝑛 65 / 83
  66. 66. Collision Impulse By understanding the collision impulse j as change of momentum, we expect the resulting velocities 𝑣 𝐴 ′ and 𝑣 𝐵 ′ to yield the following: 𝑣 𝐴 ′ = 𝑣 𝐴 + 𝑗 𝑀𝐴 𝑛 𝑣 𝐵 ′ = 𝑣 𝐵 − 𝑗 𝑀 𝐵 𝑛 66 / 83
  67. 67. Finding the Collision Impulse Now that we’ve got everything in place, we can finally compute the collision impulse. 67 / 83 −𝜖𝑣 𝐴𝐵 𝑛 = 𝑣 𝐴𝐵 ′ 𝑛 definition coefficient of restitution = (𝑣 𝐴 ′ − 𝑣 𝐵 ′ )𝑛 definition relative velocity = (𝑣 𝐴 + 𝑗 𝑀𝐴 𝑛 − 𝑣 𝐵 + 𝑗 𝑀 𝐵 𝑛)𝑛 definition collision impulse = 𝑣 𝐴 𝑛 + 𝑗 𝑀𝐴 𝑛𝑛 − 𝑣 𝐵 𝑛 + 𝑗 𝑀 𝐵 𝑛𝑛 distribution = 𝑣 𝐴 𝑛 − 𝑣 𝐵 𝑛 + 𝑗 𝑀𝐴 𝑛𝑛 + 𝑗 𝑀 𝐵 𝑛𝑛 commutative property = 𝑣 𝐴𝐵 𝑛 + 𝑗 𝑀𝐴 𝑛𝑛 + 𝑗 𝑀 𝐵 𝑛𝑛 definition relative velocity
  68. 68. Finding the Collision Impulse Now that we’ve got everything in place, we can finally compute the collision impulse. 68 / 83 𝑣 𝐴𝐵 𝑛 + 𝑗 𝑀𝐴 𝑛𝑛 + 𝑗 𝑀 𝐵 𝑛𝑛 = −𝜖𝑣 𝐴𝐵 𝑛 𝑗 𝑀𝐴 𝑛𝑛 + 𝑗 𝑀 𝐵 𝑛𝑛 = −𝜖𝑣 𝐴𝐵 𝑛 − 𝑣 𝐴𝐵 𝑛 𝑗( 1 𝑀𝐴 𝑛𝑛 + 1 𝑀 𝐵 𝑛𝑛) = −𝜖𝑣 𝐴𝐵 𝑛 − 𝑣 𝐴𝐵 𝑛 distribution 𝑗 = −𝜖𝑣 𝐴𝐵 𝑛 − 𝑣 𝐴𝐵 𝑛 1 𝑀𝐴 𝑛𝑛 + 1 𝑀 𝐵 𝑛𝑛
  69. 69. Finding the Collision Impulse Now that we’ve got everything in place, we can finally compute the collision impulse. 69 / 83 𝑗 = −𝜖𝑣 𝐴𝐵 𝑛 − 𝑣 𝐴𝐵 𝑛 1 𝑀𝐴 𝑛𝑛 + 1 𝑀 𝐵 𝑛𝑛 = −𝑣 𝐴𝐵 𝑛(1 + ϵ) 1 𝑀𝐴 𝑛𝑛 + 1 𝑀 𝐵 𝑛𝑛 distribution = −𝑣 𝐴𝐵 𝑛(1 + ϵ) 𝑛𝑛( 1 𝑀𝐴 + 1 𝑀 𝐵 ) distribution
  70. 70. Finding the Collision Impulse Now that we’ve got everything in place, we can finally compute the collision impulse. 70 / 83 𝑗 = −𝜖𝑣 𝐴𝐵 𝑛 − 𝑣 𝐴𝐵 𝑛 1 𝑀𝐴 𝑛𝑛 + 1 𝑀 𝐵 𝑛𝑛 = −𝑣 𝐴𝐵 𝑛(1 + ϵ) 1 𝑀𝐴 𝑛𝑛 + 1 𝑀 𝐵 𝑛𝑛 distribution = −𝑣 𝐴𝐵 𝑛(1 + ϵ) 𝑛𝑛( 1 𝑀𝐴 + 1 𝑀 𝐵 ) distribution Plugging in j back into our collision impulse equation yields the new velocities of A and B!
  71. 71. Collision Impulse II Finally, we need to understand how to have the collision impulse j change the angular momentum: 𝜔 𝐴 ′ = 𝜔 𝐴 + 𝐴𝑃⊥ 𝑗𝑛 𝐼𝐴 𝜔 𝐵 ′ = 𝜔 𝐵 − 𝐵𝑃⊥ 𝑗𝑛 𝐼 𝐵 71 / 83
  72. 72. Finding the Collision Impulse Let’s close by computing the collision impulse with spin: 72 / 12 −𝜖𝑣 𝐴𝐵 𝑛 = 𝑣 𝐴𝐵 ′ 𝑛 definition coefficient of restitution = (𝑣 𝐴𝑃 ′ − 𝑣 𝐵𝑃 ′ )𝑛 definition relative velocity = (𝑣 𝐴 ′ + 𝜔 𝐴 ′ 𝐴𝑃⊥ − (𝑣 𝐵 ′ + 𝜔 𝐵 ′ 𝐵𝑃⊥))𝑛 Chasles’ Theorem = (𝑣 𝐴 ′ + 𝜔 𝐴 ′ 𝐴𝑃⊥ − 𝑣 𝐵 ′ − 𝜔 𝐵 ′ 𝐵𝑃⊥)𝑛 = (𝑣 𝐴 + 𝑗 𝑀𝐴 𝑛 + (𝜔 𝐴 + 𝐴𝑃⊥ 𝑗𝑛 𝐼𝐴 )𝐴𝑃⊥ − (𝑣 𝐵 − 𝑗 𝑀 𝐵 𝑛) − (𝜔 𝐵 − 𝐵𝑃⊥ 𝑗𝑛 𝐼 𝐵 )𝐵𝑃⊥)𝑛 definition collision impulse = (𝑣 𝐴 + 𝑗 𝑀𝐴 𝑛 + 𝜔 𝐴 𝐴𝑃⊥ + 𝐴𝑃⊥ 2 𝑗𝑛 𝐼𝐴 − 𝑣 𝐵 + 𝑗 𝑀 𝐵 𝑛 − 𝜔 𝐵 𝐵𝑃⊥ + 𝐵𝑃⊥ 2 𝑗𝑛 𝐼 𝐵 )𝑛 = 𝑣 𝐴 𝑛 + 𝑗 𝑀𝐴 𝑛𝑛 + 𝜔 𝐴 𝐴𝑃⊥ 𝑛 + 𝐴𝑃⊥ 2 𝑗𝑛𝑛 𝐼𝐴 − 𝑣 𝐵 𝑛 + 𝑗 𝑀 𝐵 𝑛𝑛 − 𝜔 𝐵 𝐵𝑃⊥ 𝑛 + 𝐵𝑃⊥ 2 𝑗𝑛𝑛 𝐼 𝐵 distribution
  73. 73. Finding the Collision Impulse Let’s close by computing the collision impulse with spin: 73 / 12 𝑗 𝑀𝐴 𝑛𝑛 + 𝐴𝑃⊥ 2 𝑗𝑛𝑛 𝐼𝐴 + 𝑗 𝑀 𝐵 𝑛𝑛 + 𝐵𝑃⊥ 2 𝑗𝑛𝑛 𝐼 𝐵 = −𝑣 𝐴 𝑛 − 𝜔 𝐴 𝐴𝑃⊥ 𝑛 + 𝑣 𝐵 𝑛 + 𝜔 𝐵 𝐵𝑃⊥ 𝑛 − 𝜖𝑣 𝐴𝐵 𝑛 j to left side, non-j to right side 𝑗𝑛𝑛( 1 𝑀𝐴 + 𝐴𝑃⊥ 2 𝐼𝐴 + 1 𝑀 𝐵 + 𝐵𝑃⊥ 2 𝐼 𝐵 ) = −𝑛(𝑣 𝐴 + 𝜔 𝐴 𝐴𝑃⊥ − 𝑣 𝐵 − 𝜔 𝐵 𝐵𝑃⊥ + 𝜖𝑣 𝐴𝐵) distribution 𝑗𝑛𝑛( 1 𝑀𝐴 + 𝐴𝑃⊥ 2 𝐼𝐴 + 1 𝑀 𝐵 + 𝐵𝑃⊥ 2 𝐼 𝐵 ) = −𝑛(𝑣 𝐴𝑃 − 𝑣 𝐵𝑃 + 𝜖𝑣 𝐴𝐵) Chasles’ Theorem 𝑗𝑛𝑛( 1 𝑀𝐴 + 𝐴𝑃⊥ 2 𝐼𝐴 + 1 𝑀 𝐵 + 𝐵𝑃⊥ 2 𝐼 𝐵 ) = −𝑛(𝑣 𝐴𝐵 + 𝜖𝑣 𝐴𝐵) definition relative velocity 𝑗𝑛𝑛( 1 𝑀𝐴 + 𝐴𝑃⊥ 2 𝐼𝐴 + 1 𝑀 𝐵 + 𝐵𝑃⊥ 2 𝐼 𝐵 ) = −𝑛𝑣 𝐴𝐵(1 + 𝜖) distribution 𝑗 = −𝑛𝑣 𝐴𝐵(1 + 𝜖) 𝑛𝑛( 1 𝑀𝐴 + 𝐴𝑃⊥ 2 𝐼𝐴 + 1 𝑀 𝐵 + 𝐵𝑃⊥ 2 𝐼 𝐵 )
  74. 74. Collision Detection • Now that we know how to handle collisions, all that’s left is to understand how to detect them • As detecting intersections between arbitrary polygons is quite expensive, they are usually wrapped by some kind of collision shape • With these shapes, typical test like shape-shape intersection and ray-shape intersection become far cheaper • The quality of the collision detection depends on how good the shapes fit the actual body 74 / 83
  75. 75. Collision Spheres Detecting whether two spheres A and B intersect is as easy as comparing their distance to the sum of their radii. 75 / 83
  76. 76. Collision Spheres The potential collision point lies on the ray from sphere A to B at the exact radius of A. 76 / 83
  77. 77. Collision Spheres Detecting whether a ray intersects a sphere requires some tedious, but basic math (see References) 77 / 83
  78. 78. Axis-Aligned Bounding Boxes Detecting whether two axis-aligned bounding boxes 𝐴 𝑚𝑖𝑛, 𝐴 𝑚𝑎𝑥 and (𝐵 𝑚𝑖𝑛, 𝐵 𝑚𝑎𝑥) intersect can be easily checked using the separating axis theorem: 78 / 83
  79. 79. Axis-Aligned Bounding Boxes Detecting whether two axis-aligned bounding boxes 𝐴 𝑚𝑖𝑛, 𝐴 𝑚𝑎𝑥 and (𝐵 𝑚𝑖𝑛, 𝐵 𝑚𝑎𝑥) intersect can be easily checked using the separating axis theorem: 𝐴 𝑚𝑖𝑛 𝑥 > 𝐵 𝑚𝑎𝑥 𝑥 or 𝐵 𝑚𝑖𝑛 𝑥 > 𝐴 𝑚𝑎𝑥 𝑥 or 𝐴 𝑚𝑖𝑛 𝑦 > 𝐵 𝑚𝑎𝑥 𝑦 or 𝐵 𝑚𝑖𝑛 𝑦 > 𝐴 𝑚𝑎𝑥 𝑦 or 𝐴 𝑚𝑖𝑛 𝑧 > 𝐵 𝑚𝑎𝑥 𝑧 or 𝐵 𝑚𝑖𝑛 𝑧 > 𝐴 𝑚𝑎𝑥 𝑧 79 / 83
  80. 80. Tunneling • If your objects move too fast, you run in danger of missing collisions due to your numerical integration step size. • Imagine a sphere moving fast towards a thin wall. 80 / 83
  81. 81. Tunneling – Possible Solutions • Make the wall thicker. ▪ Need to instruct all level designers. • Impose an upper bound on object speed. • Find the speculative contact through the bounding box of the previous position of the moving object and the current one, and sub- step from the contact point ▪ Arbitrary convex polygons are a challenge (see Continuous Collision by Erin Catto in References) 81 / 83
  82. 82. Octrees • Checking every pair of objects can be very expensive • The number of required checks can be reduced by subdividing the space into smaller parts, and checking only pairs of objects who are found within the same part 82 / 83 Image by Bill Jacobs
  83. 83. Octree Construction • Start with an empty root node covering the entire world • Whenever you add an object to the world, start at the root node and traverse the tree, finding the node farthest from the root that fully contains the object • If the node has reached its maximum capacity now, subdivide it into children 83 / 83
  84. 84. Collision Detection using Octrees • Given an octree containing shapes, we only need to check all pairs of shapes that are found within the same node • Shaped in non-leaf nodes need to be checked against all shapes of all child nodes, and their children 84 / 83
  85. 85. Optimizing Octrees • If the average object size begins to exceed the node size, objects will start to be put in parent nodes more often. • Limiting the depth of the octree helps avoiding this issue. • For the same reason, it might be necessary to merge nodes again if objects have moved away. 85 / 83
  86. 86. Future Work • 3D (Matrices & Quaternions) • Joints • Non-rigid bodies • Detecting Arbitrary Collisions 86 / 83
  87. 87. References • Fiedler. Game Physics – Integration Basics. http://gafferongames.com/game-physics/integration- basics/, 2006. • Fielder. Game Physics – Fix Your Timestep! http://gafferongames.com/game-physics/fix-your- timestep/, 2006. • Hecker. Physics, The Next Frontier. Game Developer Magazine, October/November 1996. • Hecker. Physics, Part 2: Angular Effects. Game Developer Magazine, December 1996/January 1997. • Hecker. Physics, Part 3: Collision Response. Game Developer Magazine, March 1997. • Catto. Box2D User Manual. http://box2d.org/manual.pdf, 2007. • Catto. Physics for Game Programmers – Continuous Collision. http://www.gdcvault.com/play/1018239/Physics-for-Game-Programmers-Continuous, 2013. • Baraff. Physically Based Modelling – Rigid Body Simulation. http://www.pixar.com/companyinfo/research/pbm2001/pdf/notesg.pdf, 2001. • Jacobs. OpenGL Tutorial – Collision Detection. http://www.videotutorialsrock.com/opengl_tutorial/collision_detection/text.php, 2014. • Su. Ray-Sphere Intersection. http://www.cs.tufts.edu/~sarasu/courses/comp175- 2009fa/pdf/comp175-15-ray-sphere.pdf, November 11, 2009. 87 / 83
  88. 88. Thank you! http://www.npruehs.de https://github.com/npruehs @npruehs nick.pruehs@daedalic.com
  89. 89. 10 Minute Review Session • What’s the difference between kinematics and dynamics? • What is velocity? • What is acceleration? • What is momentum? • What is force? • In your own words: How does Explicit Euler Integration work? • Why are fixed time steps important in physics simulation? • What is a rigid body?
  90. 90. 10 Minute Review Session • What is the center of mass? • In your own words: Explain Chasles’ Theorem! • What is torque? • What is moment of inertia? • What is an impulse? • Which data is required for resolving collisions? • Which collision shapes do you know? • How can you prevent tunneling? • How can you reduce the number of collisions to check for?

×