prev

next

of 38

View

892Download

3

Tags:

Embed Size (px)

DESCRIPTION

- 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 * vS