Download presentation
Presentation is loading. Please wait.
Published byPhilippa Walters Modified over 9 years ago
1
Concepts for Programming a Rigid Body Physics Engine Part 1 Presented by Scott Hawkins
2
Game Physics Engine Development by Ian Millington source:
3
Topics Mathematical Data Types Rigid Body Data Algorithm for Updating the Physics –Finding the timestep –Applying forces and torques –Integration –Collision Detection –Collision Resolution
4
Mathematical Data Types real –time, t Vector –position, p Quaternion –orientation, θ 3x3 Matrix –inertial tensor, I 4x4 Matrix
5
Quaternions q represented using four reals, w, x, y, z : q = w + xi + yj + zk relationship between i, j, and k : ij = -ji = k jk = -kj = i ki = -ik = j ii = jj = kk = ijk = -1
6
Quaternion Operations q 1 + q 2 = (w 1 + w 2 ) + (x 1 + x 2 )i + (y 1 + y 2 )j + (z 1 + z 2 )k q 1 * q 2 = w 1 w 2 - x 1 x 2 - y 1 y 2 - z 1 z 2 + (w 1 x 2 + x 1 w 2 + y 1 z 2 - z 1 y 2 )i + (w 1 y 2 - x 1 z 2 + y 1 w 2 + z 1 x 2 )j + (w 1 z 2 + x 1 y 2 - y 1 x 2 + z 1 w 2 )k
7
Rigid Body Data mass, m (real) inertia tensor, I (3x3 Matrix) position, p (Vector) velocity, p’ (Vector) acceleration, p” (Vector) orientation, θ (Quaternion) angular velocity, θ’ (Vector) angular acceleration, θ” (Vector)
8
Rigid Body Data force accumulator, f (Vector) torque accumulator, τ (Vector) inverse mass, m -1 (real) inverse inertia tensor, I -1 (3x3 Matrix) I -1 in world coordinates(3x3 Matrix) transform matrix(4x4 Matrix)
9
Computing the Inertia Tensor, I I =[ I x -I xy -I xz ] [-I xy I y -I yz ] [-I xz -I yz I z ] I x = Σm i (y i 2 + z i 2 ) I y = Σm i (x i 2 + z i 2 ) I z = Σm i (x i 2 + y i 2 ) I xy = Σm i x i y i I yz = Σm i y i z i I xz = Σm i x i z i
10
Meaning of Orientation, θ θ is of the form: θ = cos(θ / 2) + n x sin(θ / 2)i + n y sin(θ / 2)j + n z sin(θ / 2)k θ represents a rotation by angle θ about a vector axis n. If θ is zero, the object is not rotated, and θ = 1 + 0i + 0j + 0k
11
Meaning of Angular Velocity, θ’ θ’ is of the form: θ’ = n x ωi + n y ωj + n z ωk This is called “axis-angle” form. θ’ represents an angular rate of ω about a vector axis n. Angular acceleration is also in axis-angle form.
12
Computing the Transform Matrix M = [2θ x 2 +2θ w 2 -1 2θ x θ y +2θ z θ w 2θ x θ z -2θ y θ w p x ] [2θ x θ y -2θ z θ w 2θ y 2 +2θ w 2 -1 2θ y θ z +2θ x θ w p y ] [2θ x θ z +2θ y θ w 2θ y θ z -2θ x θ w 2θ z 2 +2θ w 2 -1 p z ] [ 0 0 0 1] Useful for transforming geometry from local coordinates into world coordinates
13
Updating the Physics Find the timestep Δt Apply forces and torques Integrate Collision detection Collision resolution
14
Computing Δt Remember the clock value for the previous frame Δt = t current - t previous
15
Adding Forces and Torques Force and Torque Generators –in C++, use classes with virtual methods –in Java, use interfaces –user implements updateForce() or updateTorque() method All force and torque generators should be registered before the frame begins
16
Adding Forces and Torques p” = m -1 f θ” = I -1 τ = I -1 [(p point - p) x f]
17
Adding Forces and Torques Call the updateForce() or updateTorque() method for every registered force or torque generator These add to the force and torque accumulators in the rigid bodies f = Σf i τ = Στ i
18
Integration p” = m -1 f θ” = I -1 τ p next = p + p’Δt p’ next = p’ + p”Δt θ next = θ + (Δt/2)ωθ ω = 0 + θ’ x i + θ’ y j + θ’ z k θ’ next = θ’ + θ”Δt
19
Integration Use Newton’s second law to get p” and θ” f = mp” p” = f / m p” = m -1 f τ = Iθ” θ” = I -1 τ
20
Integration Use the explicit version of Euler’s method to integrate position and velocity p next = p + p’Δt p’ next = p’ + p”Δt
21
Integration Also use explicit Euler’s method for orientation and angular velocity θ next = θ + (Δt/2)ωθ ω = 0 + θ’ x i + θ’ y j + θ’ z k why it works: magic? θ’ next = θ’ + θ”Δt
22
Collision Detection Return a list of contacts Each contact consists of –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)
23
Collision Detection Implementation is very complicated Two phases: –Coarse Collision Detection –Fine Collision Detection
24
Collision Detection Use spatial data structures to perform coarse collision detection Examples of spatial data structures: –bounding volume hierarchy –binary space partitioning tree –quad-tree –oct-tree –grids Coarse collision detection eliminates pairs of objects that could not possibly be in contact
25
Collision Detection Run fine collision detection on the remaining pairs of objects Fine collision detection generates the list of contacts For polygon meshes, two types of contacts must be checked: –point-face contacts –edge-edge contacts
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.