Presentation is loading. Please wait.

Presentation is loading. Please wait.

Maths & Technologies for Games Linear Dynamics Particle Based Physics CO3303 Week 16-17.

Similar presentations


Presentation on theme: "Maths & Technologies for Games Linear Dynamics Particle Based Physics CO3303 Week 16-17."— Presentation transcript:

1 Maths & Technologies for Games Linear Dynamics Particle Based Physics CO3303 Week 16-17

2 Lecture Contents 1.Recap: Particle System Update 2.Issue with Simple Update 3.Initial Value Problems 4.Euler’s Method 5.Mid-point Method 6.Basic Verlets 7.Particle-Based Physics – Springs 8.Other Connectors 9.Solving Constraints

3 Particle System Basics Data we might store for a particle in a system: –Position, velocity, maybe mass Particle velocity must change, or it will only move in a straight line –Change in velocity is called an acceleration –Acceleration caused by forces on particle –Gravity is the most common force, but also wind, explosions, or artificial effects –In connected systems, forces from springs, elastic… Sum all the forces acting on a particle to get the overall force (and hence acceleration) at any one time

4 Particle Update How to convert force to acceleration? –Newtonian physics: F = ma m = mass of particle, –For particles mass sometimes assumed to be 1 (i.e. force = accel) Use above data to update the particle each frame: –Find overall force acting on particle E.g. gravity + spring –Turn this to an acceleration Take account of particle mass or not –Add acceleration to velocity –Add velocity to position –All these operations scaled to take to account of frame time E.g. Velocity = Velocity + Acceleration * frame time

5 Particle Update Example

6 Approximation in this Update This basic physics of forces, accelerations and velocities doesn’t just apply to particles –A starting point for model physics too –But focusing on particle systems in this lecture Problem: this approach is only an approximation –We only update things once per frame –Assumes the velocity was constant over the entire time period of the frame Not correct – the forces/acceleration will change the velocity gradually throughout the frame –Whereas our simple approach changes the velocity instantly to a new value each frame

7 Example of Error Trying to follow an orbit At each frame the velocity is parallel to the surface of the sphere –So should follow surface Move the object along this velocity vector –Amount of movement depends on frame time Object leaves surface – it shouldn’t –Velocity should have been a curve Worse when frame time is longer

8 Initial Value Problems Updating particle positions is an example of an Initial Value Problem: –We know the value of an equation at an initial point in time –Want to calculate value at some future point in time In this case: –We know position and velocity from this frame –Want to know position and velocity for next frame The simple (and flawed) method just shown is one way of solving an initial value problem –Will present others, some with better accuracy –However, each method is an approximation to the solution

9 Formal Definition Initial value problems formally: –Have a function, which changes over time: p(t) E.g. Position (or velocity) of particle –We know the initial value: p 0 = p(t 0 ) Position (or velocity) this frame –We have a time period: h Frame time –Want to know the value: p(t 0 + h) Position (& velocity) next frame –We will need derivatives: p´(t), p´´(t) 1st derivative of position = velocity ( i.e. p´(t) = v(t) ) 2nd derivative of position = acceleration ( p´´(t) = v´ (t) = a(t) )

10 Euler’s Method The Taylor series is a representation of a function based on the derivatives at a single point (in time): –Arranged here to suit our problem, we want to find p(t + h) –p is position, p´ is velocity, p´´ is acceleration, p´´´ is acceleration of acceleration… –As h becomes smaller, this approximation become more accurate –But it is an infinite series – can’t be completely calculated Euler’s Method uses just the first two terms in the series –Assumes the rest are small enough to ignore

11 Euler’s Method Algorithm Translating this equation into game terms: position next frame = current position + frame time * current velocity and if we use v instead of p velocity next frame = current velocity + frame time * current accel. This is exactly the method presented earlier for updating particles in a particle system –We know this is not ideal. Now we know why: The terms ignored are not always small Especially when large forces or complex variations in forces are involved –Still widely useful where accuracy is not at a premium

12 Mid-point Method The problem with Euler’s method is that we take the velocity / acceleration at the start of the frame –And assume they will remain the same throughout the frame The mid-point method takes the velocity / acceleration half- way through the frame –Likely to be nearer the average for the frame Calculates next frame’s values based on that

13 Mid-point Method Algorithm More steps in this algorithm: –1 st use Euler’s method to approximate half-way position / velocity: position after half frame = current position + half frame time * current velocity velocity after half frame = current velocity + half frame time * current acceleration –Calculate forces at half-way position to get half-way acceleration –Then use Euler’s method again with updated info: position next frame = current position + frame time * halfway velocity velocity next frame = current velocity + frame time * halfway acceleration This has better accuracy than Euler’s method –Not perfect since the half-way values are themselves approximate

14 Basic Verlet Method Verlet methods are an alternative approach –Less reliant on velocity –Somewhat restrictive because of that –OK for particle systems if only concerned with position The most basic verlet method: –Uses the position from the current and previous frame –Uses current acceleration, but not velocity Formula: Position next frame = 2 * current position – position last frame + (frame time) 2 * current acceleration

15 Basic Verlet Method in Full So the position is calculated like this: Position next frame = 2 * current position – position last frame + (frame time) 2 * current acceleration If we need velocity can use this approximation: Velocity next frame = (position next frame - current position) / update time or current velocity = (position next frame – position last frame) / (2 * update time) Has similar accuracy to mid-point method –Verlets have other advantages we will see later –Lack of direct velocity calculation can be problem

16 Particle Physics - Springs With these more robust methods for particle dynamics, we can consider particle physics Will start simple: a collection of particles connected by springs Forces involved are –Gravity –Spring compression –Spring stretch Will consider particle mass Use one of the previous initial value methods to update particle position based on these forces

17 Spring Forces Force exerted by a spring is from Hooke’s Law: F = -kx x is displacement from the spring’s equilibrium position k is the spring coefficient (controls stiffness) Practically: –Measure current length of spring L –Get equilibrium (or inertial) length E and spring coefficient k –Force is a vector length k(L-E) along length of the spring –Pulls each end of the spring equally, but in opposite directions –Must be careful to have force pull in the correct direction depending on which end and compression / stretch

18 Simulation 1 Create a network of particles and springs: –Store overall list of particles & overall list of springs –Each spring has two particle pointers Plus equilibrium (inertial) length and stiffness constant Calculate current length each frame from particle positions –Don’t store this –Each particle has a list of attached springs Particles can be pinned (unmovable) –So springs can hang off something solid Particles also store position, (& prev. position, velocity etc.) –For methods described earlier Also store a mass for more interesting systems

19 Simulation 2 Simulate with material covered in this lecture: –Use timed game loop Physics systems tend to work better with a fixed tick –Each frame sum forces on each particle Start with gravity Iterate through each attached spring –Add force from that spring using Hooke’s Law –Convert overall force to acceleration Acceleration = total force / particle mass –Use an initial value method to update particle Use accurate method as springs tend to create large forces Verlet method will also prove very useful later

20 Practicalities Real life systems slow down with friction –Without friction, springs will bounce forever Instead of friction, we will damp the motion –Will also help numerical stability When using springs we add a damping force: F d = -cv –Where v is the velocity, c the damping coefficient The value c is in range [0,1], typically around 0.1 to 0.3 –This force works against the current velocity to create damping –Add this to the total of other forces –For verlets get the velocity from the approximation: v = (current position - previous position) / update time

21 Spring-Based Physics: Uses –Tweaking the individual spring stiffness helps The system is often not rendered, but used as a “skeleton” for model, which is to appear soft –Similar to skinning Spring-based particle systems can be used to model rope, cloth and jelly-like objects For the latter two you need many connections to help keep the objects form

22 Different Connectors We can extend such a system with new connector types, such as: –Elastic –Rods –String Key differences introduced with these types: –Some types behave differently when stretched and when compressed –Some are constrained, they cannot be stretched and/or compressed –Some don’t exert forces at some times

23 Table of Connectors Summary of different connector properties: CompressedStretched Springs Force Elastic No forceForce Rod Not allowed String No forceNot allowed

24 Constraints We can implement elastic with only a minor change to the previous method –Same as spring but no force when compressed However, rods and string have constraints: –Rods must always remain same length –String cannot be longer than original length For implementation simplicity, we retain the concept of equilibrium length for rods and string –These types don’t exert forces –Instead the length will refer to the constraint on length

25 Solving Constraints –However, particles are constrained if attached to a rod or string –Distance between pairs of particles must remain constant, or is limited –Particles can be attached to several rods/strings – multiple constraints How can we resolve the motion equations but maintain the constraints on movement? All the initial value methods shown assume the particles are free to move

26 Mathematical Approaches Each constraint can be written as an equation illustrating the fixed length between particles, such as: With several constraints we have several equations –All must be satisfied simultaneously to solve problem Known as a system of linear equations –Several well known approaches for solving these –Common are LU Decomposition and the Gauss-Seidel method –Typically fill a matrix with the equation elements and follow an iterative process to solve –However, not enough time in module to look into these

27 Solving Constraints Of the various mathematical solutions, most have a similar repeated iterative approach: –Start with unconstrained positions for particles I.e. Calculate positions without rods or strings –Process all constraints (rods, strings), one at a time Correct the position of the attached particles for each However, correcting one pair of particles may invalidate another constraint But likely to be nearer solution, so: –Repeat the process until: All constraints satisfied (rods / strings have valid lengths) Or reached a maximum number of iterations

28 Simple Approach - Algorithm Can use a simplified version of this method: –Not completely accurate, but may be good enough First move all particles ignoring constraints –Same process as before Then iterate all rods & strings and find any that now have an invalid length –Correct the attached particle positions like this: SpringVector = Particle 2 position – Particle 1 position CurrLength = Length of SpringVector LengthError = CurrentLength – Equilibrium (Inertial) Length CorrectionVector = SpringVector * LengthError / CurrentLength Particle 1 Position += 50% * CorrectionVector Particle 2 Position -= 50% * CorrectionVector

29 Simple Approach - Detail Idea is to shift the particle pairs back towards each other –To get required length –Move each particle equally But see later We should repeatedly iterate though all the rods/strings until they are all correct –Or multiple constraints will be inaccurate Can stop sooner: –Avoid over processing –If changes become too small to notice

30 Simple Approach - Mass Correcting the position of each particle equally seems natural, but ignores mass –The constraint makes the particles change their motion So they are accelerating –But heavier particles are harder to accelerate F=ma, more mass = less acceleration from same force –So a particle with more mass should be shifted less Instead of a 50:50 split use this formula: Total mass = particle 1 mass + particle 2 mass Particle 1 split = 1 – particle 1 mass / total mass Particle 2 split = 1 – particle 2 mass / total mass [Splits in range 0-1]

31 Verlets for Constraints We used an initial value method to process equations of motion for particles quite accurately –Then ignored the result by correcting for constraints Surely this ruins the accuracy of the simulation? In particular, our velocity values will be incorrect –The particles ultimately didn’t follow the given velocity –Euler & mid-point methods rely on velocity value So verlet methods are very useful here: –No reliance on velocity –Calculates purely based on position and acceleration Which are correct even with constraints

32 Other Constraint Types


Download ppt "Maths & Technologies for Games Linear Dynamics Particle Based Physics CO3303 Week 16-17."

Similar presentations


Ads by Google