28
Technische Universit¨ at M ¨ unchen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin Schreiber, Kristof Unterweger 23.04.2012 M. Schreiber, K. Unterweger: PSE Game Physics Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 1

PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Embed Size (px)

Citation preview

Page 1: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane

Interpenetrations

Martin Schreiber, Kristof Unterweger

23.04.2012

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 1

Page 2: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Outline

Some very very basic math

Linear transformations

CollisionsSphere-SphereCollision NormalCollision points & Interpenetration depth

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 2

Page 3: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Outline

Some very very basic math

Linear transformations

CollisionsSphere-SphereCollision NormalCollision points & Interpenetration depth

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 3

Page 4: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Right-handed system• Our SBND-Engine is written in a Right-handed system

(OpenGL):

z+

x+

y+

Abbildung: right-handed coordinate system

• Use your hand to determine the orientation: Thumb (X-axis),forefinger (Y-axis), middlefinger (Z-axis).

• Thus moving an object further away means decreasing thez-coordinate!

• All formulae are given in left handed basis!

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 4

Page 5: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Cross product

a

b

n

~n = ~a× ~b =

a2b3 − a3b2a3b1 − a1b3a1b2 − a2b1

• We mainly use it to compute a vector ~n which is perpendicular to

two given vectors ~a and ~b• E. g. if a plane is given by a single point and 2 vectors, the plane

normal can be computed• Remember: This formula is given in a right-handed coordinate

system!M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 5

Page 6: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Dot product

~a · ~b =3∑

i=1

aibi

a

Abbildung: dot product

• Usage: Compute the cosine of the angle created by ~a and ~b:

~a · ~b = |~a||~b| cos(α)

• Later on, we use it to compute the projected length of ~a on anormal ~n

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 6

Page 7: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Outline

Some very very basic math

Linear transformations

CollisionsSphere-SphereCollision NormalCollision points & Interpenetration depth

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 7

Page 8: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Object properties

Video: NaturalMotion by Euphoria

http://www.youtube.com/watch?v=Qi5adyccoKI

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 8

Page 9: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Object properties

• Position• Orientation• Velocity• Mass• ...

How can we express e. g. Position and Orientation?

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 9

Page 10: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Degrees of Freedom for a rigid body object

”Modificationsof Position and Orientation which we need for ourphysics engine with rigid bodies:

• Translation: Move object from position p(t) to p(t + ∆t)• Orientation: Rotate an object q(t) to q(t + ∆t)

Other Modifications not necessary for our physics engine:• Shearing (Meaningless for rigid body objects)• Scaling (Meaningless for rigid body objects)• Projections (Only necessary for visualization, non-affine

transformation)

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 10

Page 11: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

How to describe the transformations?

Model- and World-space:• Model-Space: The space in which the object is setup without

any modifications.• The object is moved/rotated during initialization or by applying a

linear or rotational velocity in each timestep.• Applying this transformation projects the object from

model-space to world-space.Linear transformations:

• Store the position of the object in a vector ~p.• Store the orientation of the object in a quaternion ~q.• Use a 4× 4 matrix M which is created based on the position and

quaternion to describe the transformation.• (for most cases, only a 4× 3 matrix is necessary)

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 11

Page 12: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Translation

• A translation can be expressed by setting specific values in thelast column of our 4x4 matrix M:

Mtranslate =

1 tx

1 ty1 tz

1

• To allow a translation simply by applying the matrix M to a

postion, we extend the objects position ~p by a fourth componentwhich is set to 1 to the homogenous vector ~p.

• Example: ~p = (1,3,−2,1)T , tx = (10,10,10)Tp′xp′yp′z1

=

1 . . tx. 1 . ty. . 1 tz. . . 1

·

pxpypz1

=

px + txpy + typz + tz

1

=

1 + tx3 + ty−2 + tz

1

=

111381

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 12

Page 13: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Orientation

• One way to express 3D Orientation is to combine 2D rotations.• E. g. 2D rotation around Z-Axis:

MrotateZ =

cosα − sinα . .sinα cosα . .. . 1 .. . . 1

• Combining 3 rotation matrices (Around X, Y and Z axis), we can

express all possible rotations.

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 13

Page 14: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Putting everything together

• Now we can express rigid body transformations by applying alltransformations to the object’s position (or at any point on theobject’s surface:

Mtranslate ·Mrotate · ~p

• By setting M = Mtranslate ·Mrotate the transformation can beexpressed by only a single matrix-vector expression:

M · ~p

• Model matrix M gives the transformation of an object frommode-space to world-space.

• (Model matrix is also called world matrix when programming withDirectX).

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 14

Page 15: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Inverse Transformations

• It’s also possible to transform objects from world-space to themodel-space.

• This becomes handy when intersection tests can be easilyhandled in the model-space of an object (e. g. ball-boxintersection tests)

• The transformation of an object O1 to the model-space ofanother object O2 can be simply handled by using the inverse ofthe model-matrix:

M−12 ·M1

• Hint for a better understanding: read this formula from right toleft

• Firstly, object 1 is transformed to the world space• By applying the inverse transformation for object 2,

transforms object 1 to the model-space of object 2

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 15

Page 16: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

How to transform Vectors?

• A vector at an arbitrary position cannot be transformed simplyby applying the model-matrix!!!

• To transform a vector ~v from one space to another given thepoint transformation matrix M, the vector ~v can be eithertransformed by

• Applying M to the origin point as well as to the vector ~v andsubtracting both values to get the appropriate solution or

• by applying the inverse transpose of M (see next slide)

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 16

Page 17: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Inverse-transpose of a matrix (1/2)

• Here we give a short sketch (Seehttp://www.faqs.org/faqs/graphics/algorithms-faq/ formore details)

• We start by splitting up the matrix M into the 3x3 matrix L andthe translation vector~t :

M =(

L |~t)

• Assuming that ~p and ~q lie on an object’s surface, the vectorconnecting both points is defined by ~v = ~p − ~q.

• Then we can describe the transformation of an arbitrary vector ~vconnecting with the transformation of its starting and end point ~pand ~q:

~v ′ = (L~p +~t)− (L~q +~t) = L(~p − ~q) = L(~v)

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 17

Page 18: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Inverse-transpose of a matrix (2/2)

• When transforming two vectors, the angle remains the samewhich can be expressed by a dot-product.

• In this case, our second vector is represented by a normal ~n onthe surface point ~q.

• Since we know, that ~v can be transformed by applying L, we canrewrite the equation to

~n′ · ~v ′ = (N~n) · (L~v)

with N being the matrix we like to figure out.• By replacing the dot product using the transposed:

~n′ · ~v ′ = (N~n)T (L~v) = ~nT NT L~v = ~n′T~v ′

• Since the angle between the 2 vectors ~n and ~v also have to beequal in model-space, we can assume that NT L = I.

• Thus, we get N = L−T for the matrix transforming vectors tothe basis given in M.

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 18

Page 19: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Overview

• So far we are able to mathematically describetransformations from model- to world-space.

• Use homogenous coordinates (x1, x2, x3,1) for translations andposition.

• Use homogenous coordinates (x1, x2, x3,0) for vectors.• Use transposed inverse matrices M−T to transform vectors.• Use inverse M−1 of transformation matrix to transform

”backward”.

• Seems to be easy, isn’t it? Not for rotations!

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 19

Page 20: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Overview

• So far we are able to mathematically describetransformations from model- to world-space.

• Use homogenous coordinates (x1, x2, x3,1) for translations andposition.

• Use homogenous coordinates (x1, x2, x3,0) for vectors.• Use transposed inverse matrices M−T to transform vectors.• Use inverse M−1 of transformation matrix to transform

”backward”.• Seems to be easy, isn’t it? Not for rotations!

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 19

Page 21: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Outline

Some very very basic math

Linear transformations

CollisionsSphere-SphereCollision NormalCollision points & Interpenetration depth

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 20

Page 22: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Our schedule for collision tests

• Collision tests:Plane Sphere Box

Plane o - -Sphere a X -

Box a X X• o: meaningless• a: not in this session, but available via additional slides.• -: Symmetric

• Resolving interpenetration

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 21

Page 23: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Collision: Sphere-Sphere

• Really easy to detect this collision:Test whether the distance of the 2 center points is less thanthe sum of both radii

• But we need more collision data in order to handle collisions...(next slide)

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 22

Page 24: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Intersection data

Which intersection data has to be generated generated?

• Collision normal• Collision points• Interpenetration depth

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 23

Page 25: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Collision Normal

• We need to know in which direction we have to apply someforce due to the collision.

• For spheres we assume to determine the collision normal whenthe 2 spheres are very close.

• Thus the normal from the first sphere aiming to the other one isgiven by computing the collision normal based upon bothsphere’s centers.

n

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 24

Page 26: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Collision points & Interpenetration depth• Since an interpenetration can occur during a timestep (not only

at the very end), the collision point for every object has to beapproximated.

• This collision point gets important when applying an impulse toan object which results in a angular momentum.

• The distance of those collision points also gives us theinterpenetration depth.

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 25

Page 27: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Resolving Interpenetration (1/2)

• If a binary-collision for 2 objects was detected, thisinterpenetration has to be solved somehow

• The first assumption is, that the interpenetration can be solvedby moving the objects parallel to the collision normal

• Question: How far should every object be moved?

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 26

Page 28: PSE Game Physics - Technische Universität München fileTechnische Universit¨at Munc¨ hen PSE Game Physics Session (2) Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations Martin

Technische Universitat Munchen

Resolving Interpenetration (2/2)

• We start by considering 2 extreme cases assuming thatresolving the interpenetration depends on the rigid-bodys mass

• O1 has∞ mass, O2 has mass 1⇒ Only O2 is moved• O2 has∞ mass, O1 has mass 1⇒ Only O1 is moved

• Linearly interpolating inbetween these 2 extreme cases, thefraction d1 of the interpenetration distance for O1 can becomputed by:

d1 =m2

m1 + m2=

m−11

m−11 + m−1

2

with m−1i being the inverse mass of the corresponding objects.

M. Schreiber, K. Unterweger: PSE Game Physics

Session (2)Collisions: Sphere-Sphere, Sphere-Plane Interpenetrations, 23.04.2012 27