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

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

Embed Size (px)

Citation preview

Concepts for Programming a Rigid Body Physics Engine

Part 1Presented by Scott Hawkins

Game Physics Engine

Developmentby Ian Millington

source:

Topics

• Mathematical Data Types

• Rigid Body Data

• Algorithm for Updating the Physics– Finding the timestep– Applying forces and torques– Integration– Collision Detection– Collision Resolution

Mathematical Data Types

• real– time, t

• Vector– position, p

• Quaternion– orientation, θ

• 3x3 Matrix– inertial tensor, I

• 4x4 Matrix

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

Quaternion Operations

• q1 + q2 = (w1 + w2) + (x1 + x2)i +(y1 + y2)j + (z1 + z2)k

• q1 * q2 = w1w2 - x1x2 - y1y2 - z1z2 +(w1x2 + x1w2 + y1z2 - z1y2)i +(w1y2 - x1z2 + y1w2 + z1x2)j +(w1z2 + x1y2 - y1x2 + z1w2)k

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)

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)

Computing the Inertia Tensor, I

• I = [ Ix -Ixy -Ixz][-Ixy Iy -Iyz][-Ixz -Iyz Iz ]

• Ix = Σmi(yi2 + zi

2)

• Iy = Σmi(xi2 + zi

2)

• Iz = Σmi(xi2 + yi

2)

• Ixy = Σmixiyi

• Iyz = Σmiyizi

• Ixz = Σmixizi

Meaning of Orientation, θ

• θ is of the form:• θ = cos(θ / 2) +

nxsin(θ / 2)i +nysin(θ / 2)j +nzsin(θ / 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

Meaning of Angular Velocity, θ’

• θ’ is of the form:

• θ’ = nxωi + nyωj + nzω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.

Computing the Transform Matrix

M = [2θx2+2θw

2-1 2θxθy+2θzθw 2θxθz-2θyθw px]

[2θxθy-2θzθw 2θy2+2θw

2-1 2θyθz+2θxθw py]

[2θxθz+2θyθw 2θyθz-2θxθw 2θz2+2θw

2-1 pz]

[ 0 0 0 1]

• Useful for transforming geometry from local coordinates into world coordinates

Updating the Physics

• Find the timestep Δt

• Apply forces and torques

• Integrate

• Collision detection

• Collision resolution

Computing Δt

• Remember the clock value for the previous frame

• Δt = tcurrent - tprevious

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

Adding Forces and Torques

• p” = m-1f

• θ” = I-1τ = I-1[(ppoint - p) x f]

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 = Σfi

• τ = Στi

Integration

• p” = m-1f

• θ” = I-1τ

• pnext = p + p’Δt

• p’next = p’ + p”Δt

• θnext = θ + (Δt/2)ωθ

• ω = 0 + θ’x i + θ’y j + θ’z k

• θ’next = θ’ + θ”Δt

Integration

• Use Newton’s second law to get p” and θ”

• f = mp”• p” = f / m• p” = m-1f

• τ = Iθ”

• θ” = I-1τ

Integration

• Use the explicit version of Euler’s method to integrate position and velocity

• pnext = p + p’Δt

• p’next = p’ + p”Δt

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

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)

Collision Detection

• Implementation is very complicated

• Two phases:– Coarse Collision Detection– Fine Collision Detection

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

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