intro to game physics with box2d chapter 2 part 2

38
Introduction to Game Physics with Box2D 2. Mathematics for Game Physics Lecture 2.2: Digital Calculus Ian Parberry Dept. of Computer Science & Engineering University of North Texas

Upload: ian-parberry

Post on 06-Sep-2014

915 views

Category:

Documents


4 download

DESCRIPTION

 

TRANSCRIPT

  • Introduction to Game Physics with Box2D2. Mathematics for Game Physics Lecture 2.2: Digital Calculus Ian Parberry Dept. of Computer Science & Engineering University of North Texas
  • Contents Euler and Verlet Integration Gauss-Seidel RelaxationChapter 2 Introduction to Game Physics with Box2D 2
  • Chapter 2 Introduction to Game Physics with Box2D 3
  • Discrete CalculusChapter 2 Introduction to Game Physics with Box2D 4
  • Time Line For An Object Frame Number: 1 2 3 NowChapter 2 Introduction to Game Physics with Box2D 5
  • Computing PositionChapter 2 Introduction to Game Physics with Box2D 6
  • Euler IntegrationThis corresponds to Euler integration. Instead ofintegrating the curve, we are summing overdiscrete time-slices.Chapter 2 Introduction to Game Physics with Box2D 7
  • ImplementationWe implement this by storing each objectsposition, velocity, acceleration, and last move time. D3DXVECTOR2 m_vP; //position D3DXVECTOR2 m_vV; //velocity D3DXVECTOR2 m_vA; //acceleration int m_nLastMoveTime; //time of last moveWe then update position and velocity once per frame. int t = timeGetTime(); //current time in msec int dt = t - m_nLastMoveTime; //frame time m_vP += m_vV * dt; //update position m_vV += m_vA * dt; //update velocity m_nLastMoveTime = t; //update timeChapter 2 Introduction to Game Physics with Box2D 8
  • ExampleChapter 2 Introduction to Game Physics with Box2D 9
  • But in a Game? We compute the distance moved in each frame and accumulate all those distances to get the total distances. We have to assume that the velocity is constant within each frame. We end up with the following set of distances, which is too smallChapter 2 Introduction to Game Physics with Box2D 10
  • Chapter 2 Introduction to Game Physics with Box2D 11
  • Velocity as a Continuous Function of TimeChapter 2 Introduction to Game Physics with Box2D 12
  • Velocity as a Discrete Function of TimeChapter 2 Introduction to Game Physics with Box2D 13
  • Verlet Integration Loup Verlet, 1951- Developed the concept that is now called Verlet integration for use in particle physics simulation.Chapter 2 Introduction to Game Physics with Box2D 14
  • Why Do We Care? There are mathematical reasons for using Verlet integration instead of Euler integration when simulating real particle systems. But what about in games? We dont care so much about reality. One useful feature of Verlet integration is that it is easy to incorporate constraints, for example, to fix lengths and angles. This means that Verlet integration makes it easier to code soft-body animation including cloth and ragdoll.Chapter 2 Introduction to Game Physics with Box2D 15
  • Verlets ThinkingChapter 2 Introduction to Game Physics with Box2D 16
  • Verlets Thinking 2Chapter 2 Introduction to Game Physics with Box2D 17
  • Summary of Verlet IntegrationChapter 2 Introduction to Game Physics with Box2D 18
  • Compare and Contrast Euler: Verlet:Chapter 2 Introduction to Game Physics with Box2D 19
  • ImplementationWe implement this by storing each objects position,previous position, acceleration, and last move time. D3DXVECTOR2 m_vP; //position D3DXVECTOR2 m_vOldP; //previous position D3DXVECTOR2 m_vA; //acceleration int m_nLastMoveTime; //time of last moveWe then update position and velocity once per frame. int t = timeGetTime(); //current time in millisec int dt = t - m_nLastMoveTime; //frame time D3DXVECTOR2 vTemp = m_vP; //save m_vP += m_vP - m_vOldP + m_vA*dt*dt/2.0f; //update m_vOldP = vTemp; //remember m_nLastMoveTime = t; //update timeChapter 2 Introduction to Game Physics with Box2D 20
  • OptimizationAssume dt is constant. In fact, make it 1. D3DXVECTOR2 m_vP; //position D3DXVECTOR2 m_vOldP; //previous position D3DXVECTOR2 m_vA; //accelerationEven better, ignore the divide by 2. Ramp the accelerationdown to compensate if you need to. D3DXVECTOR2 vTemp = m_vP; //save m_vP += m_vP - m_vOldP + m_vA; //update m_vOldP = vTemp; //rememberChapter 2 Introduction to Game Physics with Box2D 21
  • But, But, But const int ITERATIONS = 42; //Why 42? Why not. D3DXVECTOR2 vTemp; for(int i=0; i
  • ImplementationStore each objects position, acceleration, and lastmove time. D3DXVECTOR2 m_vP, m_vOldP, m_vA; //as before int m_nLastMoveTime; //time of last move We then update position multiple times per frame. int t = timeGetTime(); //current time in ms int dt = t - m_nLastMoveTime; //frame timeChapter 2 Introduction to Game Physics with Box2D 23
  • Implementationdt is typically in the range of tens of millisecs. D3DXVECTOR2 vTemp; for(int i=0; i
  • Satisfying ConstraintsChapter 2 Introduction to Game Physics with Box2D 25
  • Satisfying Constraints We mentioned earlier that Verlet integration makes it easy to enforce constraints on the particles. For example, lets model a stick by applying Verlet integration to two particles at the ends of the stick. The constraint is that the distance between the particles must remain constant. We move the particles at the ends of the stick independently, then try to fix their positions before rendering if they are the wrong distance apart.Chapter 2 Introduction to Game Physics with Box2D 26
  • fLenA Sticky Situation m_vP1 m_vP2Suppose its ends are at positions m_vP1 andm_vP2, and it is supposed to have length LEN. const float LEN = 42.0f; D3DXVECTOR2 m_vP1, m_vP2;First we get a vector vStick along the stickand find its length fLen. D3DXVECTOR2 vStick = m_vP1 - m_vP2; float fLen = D3DXVec2Length(&vStick);Chapter 2 Introduction to Game Physics with Box2D 27
  • fLenA Sticky Situation m_vP1 m_vP2 LENThen we find the difference between the sticknow and what it should be. vStick *= (fLenLEN)/fLen;We split the difference between the two ends. m_vP1 += 0.5f * vStick; m_vP2 -= 0.5f * vStick;So far, so good.Chapter 2 Introduction to Game Physics with Box2D 28
  • One Stick Summary Remember this code. Well useDeclarations: it again 3 slides from now D3DXVECTOR2 vStick; float fLen;Code: vStick = m_vP1 - m_vP2; fLen = D3DXVec2Length(&vStick); vStick *= (fLenLEN)/fLen; m_vP1 += 0.5f * vStick; m_vP2 -= 0.5f * vStick;Chapter 2 Introduction to Game Physics with Box2D 29
  • Two SticksBut what if weve got 2 sticks joined at the ends? m_vP3 m_vP3 m_vP1 m_vP2 m_vP1 m_vP2 Satisfying one constraint may violate the other. m_vP3 m_vP1 m_vP2Chapter 2 Introduction to Game Physics with Box2D 30
  • DeclarationsUsing the same declarations as before: const float LEN = 42.0f D3DXVECTOR2 m_vP1, m_vP2, m_vP3; D3DXVECTOR2 vStick1, vStick2; float fLen;Chapter 2 Introduction to Game Physics with Box2D 31
  • We saw Treat the Sticks Independentlythis code 3slides ago vStick = m_vP1 - m_vP2; fLen = D3DXVec2Length(&vStick); vStick *= (fLenLEN)/fLen; m_vP1 += 0.5f * vStick; Remember m_vP2 -= 0.5f * vStick; this code. Well use it vStick = m_vP2 - m_vP3; again 2 slides fLen = D3DXVec2Length(&vStick); from now Ditto vStick *= (fLenLEN)/fLen; m_vP2 += 0.5f * vStick; m_vP3 -= 0.5f * vStick; Chapter 2 Introduction to Game Physics with Box2D 32
  • Details The code is not exactly as we drew it in the picture. When we move m_vP2 the second time, its not starting from its original position. But its making progress towards where it needs to be. m_vP2 m_vP2 m_vP2Chapter 2 Introduction to Game Physics with Box2D 33
  • RelaxationRepeat the process. Its called relaxation. const int ITERATIONS = 7; for(int i=0; i
  • SpringsTo make springs instead of sticks, replace: vStick *= (fLenLEN)/fLen;with the following, where m_fRestitution is acoefficient of restitution between 0 and 1: vStick *= m_fRestitution*(fLenLEN)/fLen; 0 1Chapter 2 Introduction to Game Physics with Box2D 35
  • Jacobi/Gauss/Seidel Iteration This is Jacobi or Gauss-Seidel iteration. Jacobi It is a general method for satisfying multiple constraints that works quite well. Works quite well means that if the Gauss conditions are right, it will converge. The number of ITERATIONS will depend on the physical system being modeled, and details such as the speeds and the floating point precision. SeidelChapter 2 Introduction to Game Physics with Box2D 36
  • ConclusionChapter 2 Introduction to Game Physics with Box2D 37
  • Suggested ReadingSections 2.3, 2.4.Chapter 2 Introduction to Game Physics with Box2D 38