Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- Simple Verlet Physics by Stuart All... by mochimedia 20756 views
- Kingdom Rush 'Behind The Scenes'! b... by mochimedia 23576 views
- Ali Batı - Bağımsız Geliştiricinin ... by gamedevelopersturkey 1768 views
- Icycle: On Thin Ice, A Postmortem b... by mochimedia 27288 views
- What's New at Mochi! (2013 Edition)... by mochimedia 27233 views

No Downloads

Total views

1,292

On SlideShare

0

From Embeds

0

Number of Embeds

4

Shares

0

Downloads

34

Comments

0

Likes

1

No embeds

No notes for slide

- 1. 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
- 2. Contents• Euler and Verlet Integration• Gauss-Seidel RelaxationChapter 2 Introduction to Game Physics with Box2D 2
- 3. Chapter 2 Introduction to Game Physics with Box2D 3
- 4. Discrete CalculusChapter 2 Introduction to Game Physics with Box2D 4
- 5. Time Line For An Object Frame Number: 1 2 3 NowChapter 2 Introduction to Game Physics with Box2D 5
- 6. Computing PositionChapter 2 Introduction to Game Physics with Box2D 6
- 7. 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
- 8. ImplementationWe implement this by storing each object’sposition, 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
- 9. ExampleChapter 2 Introduction to Game Physics with Box2D 9
- 10. 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 small…Chapter 2 Introduction to Game Physics with Box2D 10
- 11. Chapter 2 Introduction to Game Physics with Box2D 11
- 12. Velocity as a Continuous Function of TimeChapter 2 Introduction to Game Physics with Box2D 12
- 13. Velocity as a Discrete Function of TimeChapter 2 Introduction to Game Physics with Box2D 13
- 14. 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
- 15. 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 don’t 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
- 16. Verlet’s ThinkingChapter 2 Introduction to Game Physics with Box2D 16
- 17. Verlet’s Thinking 2Chapter 2 Introduction to Game Physics with Box2D 17
- 18. Summary of Verlet IntegrationChapter 2 Introduction to Game Physics with Box2D 18
- 19. Compare and Contrast Euler: Verlet:Chapter 2 Introduction to Game Physics with Box2D 19
- 20. ImplementationWe implement this by storing each object’s 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
- 21. 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
- 22. But, But, But… const int ITERATIONS = 42; //Why 42? Why not. D3DXVECTOR2 vTemp; for(int i=0; i<ITERATIONS; i++){ vTemp = m_vP; //save m_vP += m_vP - m_vOldP + m_vA; //update m_vOldP = vTemp; //remember } //forChapter 2 Introduction to Game Physics with Box2D 22
- 23. ImplementationStore each object’s 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
- 24. Implementationdt is typically in the range of tens of millisecs. D3DXVECTOR2 vTemp; for(int i=0; i<dt; i++){ vTemp = m_vP; //save m_vP += m_vP - m_vOldP + m_vA; //update m_vOldP = vTemp; //remember } //for m_nLastMoveTime = t; //update timeChapter 2 Introduction to Game Physics with Box2D 24
- 25. Satisfying ConstraintsChapter 2 Introduction to Game Physics with Box2D 25
- 26. Satisfying Constraints• We mentioned earlier that Verlet integration makes it easy to enforce constraints on the particles.• For example, let’s 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
- 27. 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
- 28. fLenA Sticky Situation m_vP1 m_vP2 LENThen we find the difference between the sticknow and what it should be. vStick *= (fLen–LEN)/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
- 29. One Stick Summary Remember this code. We’ll useDeclarations: it again 3 slides from now D3DXVECTOR2 vStick; float fLen;Code: vStick = m_vP1 - m_vP2; fLen = D3DXVec2Length(&vStick); vStick *= (fLen–LEN)/fLen; m_vP1 += 0.5f * vStick; m_vP2 -= 0.5f * vStick;Chapter 2 Introduction to Game Physics with Box2D 29
- 30. Two SticksBut what if we’ve 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
- 31. 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
- 32. We saw Treat the Sticks Independentlythis code 3slides ago vStick = m_vP1 - m_vP2; fLen = D3DXVec2Length(&vStick); vStick *= (fLen–LEN)/fLen; m_vP1 += 0.5f * vStick; Remember m_vP2 -= 0.5f * vStick; this code. We’ll use it vStick = m_vP2 - m_vP3; again 2 slides fLen = D3DXVec2Length(&vStick); from now Ditto vStick *= (fLen–LEN)/fLen; m_vP2 += 0.5f * vStick; m_vP3 -= 0.5f * vStick; Chapter 2 Introduction to Game Physics with Box2D 32
- 33. Details• The code is not exactly as we drew it in the picture.• When we move m_vP2 the second time, it’s not starting from its original position.• But it’s making progress towards where it needs to be. m_vP2 m_vP2 m_vP2Chapter 2 Introduction to Game Physics with Box2D 33
- 34. RelaxationRepeat the process. It’s called relaxation. const int ITERATIONS = 7; for(int i=0; i<ITERATIONS; i++){ vStick = m_vP1 - m_vP2; fLen = D3DXVec2Length(&vStick); vStick *= (fLen–LEN)/fLen; m_vP1 += 0.5f * vStick; m_vP2 -= 0.5f * vStick; We saw this code 2 vStick = m_vP2 - m_vP3; slides ago. fLen = D3DXVec2Length(&vStick); vStick *= (fLen–LEN)/fLen; m_vP2 += 0.5f * vStick; m_vP3 -= 0.5f * vStick; } //forChapter 2 Introduction to Game Physics with Box2D 34
- 35. SpringsTo make springs instead of sticks, replace: vStick *= (fLen–LEN)/fLen;with the following, where m_fRestitution is acoefficient of restitution between 0 and 1: vStick *= m_fRestitution*(fLen–LEN)/fLen; 0 1Chapter 2 Introduction to Game Physics with Box2D 35
- 36. 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
- 37. ConclusionChapter 2 Introduction to Game Physics with Box2D 37
- 38. Suggested ReadingSections 2.3, 2.4.Chapter 2 Introduction to Game Physics with Box2D 38

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment