Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concepts for Programming a Rigid Body Physics Engine Part 2 Presented by Scott Hawkins.

Similar presentations


Presentation on theme: "Concepts for Programming a Rigid Body Physics Engine Part 2 Presented by Scott Hawkins."— Presentation transcript:

1 Concepts for Programming a Rigid Body Physics Engine Part 2 Presented by Scott Hawkins

2 Game Physics Engine Development by Ian Millington source:

3 Topics Collision Resolution –Resolving Velocities –Resolving Positions Friction

4 Collision Resolution We have been given a list of contacts, each containing: –references to the two rigid bodies –contact point in world coordinates, q (Vector) –contact normal in world coordinates, n (Vector) –penetration depth, d (real) –coefficient of restituion, c (real) –coefficient of friction, μ (real)

5 Collision Resolution After we detect collisions, what needs to be updated? –positions –orientations –velocities –angular velocities

6 Collision Resolution Two steps –Resolve velocities (and angular velocities) –Resolve positions (and orientations)

7 Collision Resolution Resolving velocities –Find the closing velocity for each contact –Choose the contact with greatest closing velocity –Resolve velocities for this contact (do not remove it from the list of contacts) –Repeat this until either (1) all contacts are resolved, or (2) we run out of iterations

8 Finding Closing Velocity “Closing velocity” is the speed at which the two objects are moving toward each other at the point of contact. We need this information to determine which contact to resolve first.

9 Finding Closing Velocity How to find the closing velocity? –Find the velocity at the contact point for each object –Take the component in the direction of the contact –Sum them together

10 Finding Closing Velocity How do you find the velocity of a point on a spinning object? –Start with the object’s overall velocity and add the component due to rotation v point = p’ + θ’ x (p point - p) –where p point is the position of the point in world coordinates

11 Finding Closing Velocities For a rigid body collision, some component of the closing velocity may result from rotation vs

12 Finding Closing Velocity We want the velocity of each object at the contact point –We were given q, the contact point in world coordinates v contact,a = p’ a + θ’ a x (q - p a ) v contact,b = p’ b + θ’ b x (q - p b )

13 Finding Closing Velocity Find the component of this in the direction of the contact –We were given n, the contact normal, which is a unit vector pointing from object a to object b v c,a = v contact,a · n v c,b = v contact,b · -n –Use -n in the second equation because we want the component going from object b to object a

14 Finding Closing Velocity Sum the components from each object to get the total closing velocity, v c v c = v c,a + v c,b

15 Choosing the Greatest Closing Velocity We now have the closing velocity for each contact Choosing the greatest closing velocity will take O(n) time normally –use a max-heap?

16 Resolving Velocities How to update velocity and angular velocity for a pair of colliding objects? Accomplish this by applying some impulse, g (which we will find) g represents a change in momentum g = Δ(mv)

17 Resolving Velocities This also creates an impulsive torque, u, given by: u = (q - p) x g –Once again, q is the contact point in world coordinates

18 Resolving Velocities To apply g to the objects: Δp’ a = m a -1 g Δθ’ a = I a -1 u a = I a -1 [(q - p a ) x g] Δp’ b = m b -1 (-g) Δθ’ b = I b -1 u b = I b -1 [(q - p b ) x (-g)] –Be sure to use -g for object b –I -1 in these equations is in world coordinates

19 Resolving Velocities How to choose g ?

20 Resolving Velocities We want the objects to bounce off of each other according to the coefficient of restitution, c v c,after = -cv c At the contact point, and in the direction of the contact normal, we want a velocity change of Δv Δv = -cv c - v c = -(1 + c)v c

21 Resolving Velocities How can we choose g to generate this result? –We already know the direction of g It must point in the direction of the contact normal, since it is like a normal force acting over an infinitely short time interval –Find the magnitude g by expressing g as g = gn and plugging it into the previous equations

22 Resolving Velocities If we applied the impulse gn, we would get: Δp’ a = m a -1 gn Δθ’ a = I a -1 [(q - p a ) x gn] Δp’ b = -m b -1 gn Δθ’ b = -I b -1 [(q - p b ) x gn]

23 Resolving Velocities This causes a change in velocity at the contact point: Δv contact,a = Δp’ a + Δθ’ a x (q - p a ) = m a -1 gn + (I a -1 [(q - p a ) x gn]) x (q - p a ) = g[m a -1 n + (I a -1 [(q - p a ) x n]) x (q - p a )] Δv contact,b = Δp’ b + Δθ’ b x (q - p b ) = -m b -1 gn + (-I b -1 [(q - p b ) x gn]) x (q - p b ) = -g[m b -1 n + (I b -1 [(q - p b ) x n]) x (q - p b )]

24 Resolving Velocities This contributes to a change in closing velocity: Δv c,a = Δv contact,a · n = g[m a -1 n + (I a -1 [(q - p a ) x n]) x (q - p a )] · n = g(m a -1 + [(I a -1 [(q - p a ) x n]) x (q - p a )] · n) Δv c,b = Δv contact,b · -n = -g[m b -1 n + (I b -1 [(q - p b ) x n]) x (q - p b )] · -n = g(m b -1 + [(I b -1 [(q - p b ) x n]) x (q - p b )] · n)

25 Resolving Velocities Add these to get the total change in closing velocity: Δv c = Δv c,a + Δv c,b = g(m a -1 + [(I a -1 [(q - p a ) x n]) x (q - p a )] · n) + g(m b -1 + [(I b -1 [(q - p b ) x n]) x (q - p b )] · n) = -(1 + c)v c

26 Resolving Velocities Now we can isolate g g = -(1 + c)v c / (m a -1 + [(I a -1 [(q - p a ) x n]) x (q - p a )] · n + m b -1 + [(I b -1 [(q - p b ) x n]) x (q - p b )] · n) g = gn Apply g to the objects as we discussed earlier

27 Resolving Velocities After resolving the velocities for one contact, update its closing velocity to be v c,after = -cv c –Update the closing velocities for every contact involving one of the two bodies that were changed This contact was resolved These need to update v c

28 Resolving Velocities Continue resolving whichever contact has the greatest closing velocity until either –(1) all contacts are resolved (all closing velocities are less than or equal to zero) or –(2) we run out of iterations

29 Collision Resolution Resolving positions –Choose the contact with greatest interpenetration depth –Resolve positions for this contact (do not remove it from the list of contacts) –Repeat this until either (1) all contacts are resolved, or (2) we run out of iterations

30 Choosing the Greatest Interpenetration Depth The interpenetration depth d at each contact is given to us by the collision detector Use another max-heap?

31 Resolving Positions How to update position and orientation for a pair of colliding objects? –We apply changes in position and orientation to each object to make d = 0 –The changes we need to find are Δp a, Δθ a, Δp b, and Δθ b

32 Resolving Positions Once we have Δp a, Δθ a, Δp b, and Δθ b, we can perform the update p a,after = p a + Δp a θ a,after = θ a + ½(Δθ a )(θ a ) –where Δθ a = 0 + Δθ a,x i + Δθ a,y j + Δθ a,z k and Δθ a,x, Δθ a,y, Δθ a,z are components of Δθ a p b,after = p b + Δp b θ b,after = θ b + ½(Δθ b )(θ b ) –where Δθ b = 0 + Δθ b,x i + Δθ b,y j + Δθ b,z k and Δθ b,x, Δθ b,y, Δθ b,z are components of Δθ b

33 Resolving Positions How do we decide what those changes should be?

34 Resolving Positions Millington uses an approximation –Treat distance moved around a circle as if it were in a straight line

35 Resolving Positions Interpenetration depth d resolved through a combination of linear and rotational movement from both objects d = linear movement of a + angular movement of a + linear movement of b + angular movement of b

36 Resolving Positions Each movement contributes some fraction of the total distance linear movement of a = w 1 d angular movement of a = w 2 d linear movement of b = w 3 d angular movement of b = w 4 d –where w 1, w 2, w 3, and w 4 are weights between 0 and 1 such that w 1 + w 2 + w 3 + w 4 = 1

37 Resolving Positions Impulse –To resolve velocities, the “impulse” we want is a force integrated over a short time units are kg·m/s –To resolve positions, we need an “impulse” that is force integrated twice over a short time units are kg·m

38 Resolving Positions Inertia –normally, an object’s tendency to resist a change in motion –in this case, we say “inertia” is an object’s response to one kg·m of “impulse”

39 Resolving Positions Choose w 1, w 2, w 3, and w 4 based on inertia w 1 = linear inertia of a / total inertia w 2 = angular inertia of a / total inertia w 3 = linear inertia of b / total inertia w 4 = angular inertia of b / total inertia total inertia = linear inertia of a + angular inertia of a + linear inertia of b + angular inertia of b

40 Resolving Positions Linear Inertia –How much linear movement occurs in response to a unit impulse? –(different kind of impulse than before) linear inertia of a = m -1 a linear inertia of b = m -1 b

41 Resolving Positions Angular Inertia –How much angular movement occurs in response to a unit impulse? (in the direction of the contact) angular inertia of a = [(I -1 a [(q - p a ) x n]) x (q - p a )] · n angular inertia of b = [(I -1 b [(q - p b ) x n]) x (q - p b )] · n

42 Resolving Positions We can now find the weights w 1, w 2, w 3, and w 4 w 1 = m -1 a / total inertia w 2 = [(I -1 a [(q - p a ) x n]) x (q - p a )] · n / total inertia w 3 = m -1 b / total inertia w 4 = [(I -1 b [(q - p b ) x n]) x (q - p b )] · n / total inertia total inertia = m -1 a + [(I -1 a [(q - p a ) x n]) x (q - p a )] · n + m -1 b + [(I -1 b [(q - p b ) x n]) x (q - p b )] · n

43 Resolving Positions Using these weights, we can find how much of each type of movement should occur linear movement of a = w 1 d angular movement of a = w 2 d linear movement of b = w 3 d angular movement of b = w 4 d

44 Resolving Positions How can we generate this much movement?

45 Resolving Positions Linear movement Δp a = -n * linear movement of a Δp b = n * linear movement of b

46 Resolving Positions Angular Movement How much rotation? –We are given an amount of movement –Find ratio of impulse per movement –Find ratio of rotation per impulse –rotation = (movement) * (impulse / movement) * (rotation / impulse)

47 Resolving Positions How to find ratio of impulse per movement? –The angular inertia we used before was a ratio of movement per impulse (impulse/movement) a = (angular inertia of a) -1 (impulse/movement) b = (angular inertia of b) -1

48 Resolving Positions How to find ratio of rotation per impulse? (rotation/impulse) a = I -1 a ((q - p a ) x n) (rotation/impulse) b = I -1 b ((q - p b ) x (-n)) –Use -n for object b, since it must receive an impulse in the opposite direction

49 Resolving Positions Putting it all together Δθ a = rotation a = (movement) a * (impulse / movement) a * (rotation / impulse) a = w 2 d * (angular inertia of a) -1 * I -1 a ((q - p a ) x n) = (angular inertia of a / total inertia) * d * (angular inertia of a) -1 * I -1 a ((q - p a ) x n)

50 Resolving Positions Putting it all together Δθ b = rotation b = (movement) b * (impulse / movement) b * (rotation / impulse) b = w 4 d * (angular inertia of b) -1 * I -1 b ((q - p b ) x (-n)) = (angular inertia of b / total inertia) * d * (angular inertia of b) -1 * I -1 b ((q - p b ) x (-n))

51 Resolving Positions Apply Δp a, Δθ a, Δp b, and Δθ b to the objects as we discussed earlier

52 Resolving Positions After resolving the positions for one contact, update the penetration depth for related contacts This contact was resolved These need to update d

53 Resolving Positions To update the penetration depth d for a related contact Δd = (Δp + (Δθ x (q - p))) · n

54 Resolving Positions Continue resolving whichever contact has the greatest penetration depth until either –(1) all contacts are resolved (all penetration depths are less than or equal to zero) or –(2) we run out of iterations

55 Friction We want a frictional impulse g f ||g f || <= μ||g|| g f is in the direction that will eliminate perpendicular components of the objects’s relative motion

56 Friction To find the direction of g f –Find the components of the objects’ velocities perpendicular to the contact v perp,a = v contact,a - v contact,a (v contact,a · n)/||v contact,a || v perp,b = v contact,b - v contact,b (v contact,b · -n)/|| v contact,b || v perp,total = v perp,a - v perp,b g f = -||g f ||v perp,total

57 Friction To find the magnitude of g f –Find the greatest possible value for ||g f || that satisfies the inequalities ||g f || <= μ||g|| ||Δv perp,total || <= ||v perp,total ||


Download ppt "Concepts for Programming a Rigid Body Physics Engine Part 2 Presented by Scott Hawkins."

Similar presentations


Ads by Google