quaternions paul taylor 2010. swizzle what is a swizzle?

40
Quaternions Paul Taylor 2010

Upload: cori-small

Post on 02-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Quaternions

Paul Taylor 2010

Page 2: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Swizzle

• What is a swizzle?

Page 3: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Matrix Rotations

• A simple 2D (xy) Rotation Matrix:

• Rotates in the xy plane counterclockwise by Ɵ Degrees ( In a RH Space)

• The Determinant is always 1

http://en.wikipedia.org/wiki/Rotation_matrix

Page 4: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

3D Rotations

• Rx rotates Y towards Z• Ry rotates Z towards X• Rz rotates X towards Y

http://en.wikipedia.org/wiki/Rotation_matrix#Rotations_in_two_and_three_dimensions

Page 5: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Euler Angles (Yaw Pitch Roll)

Pros: Easy to visualiseCons: Gimbal LockIf any one axis is exactly 0 or PI or –PI(Where Cos Ɵ = 1 and Sin Ɵ = 0)The freedom of rotation is limited to a plane.

Page 6: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Rotation about an Axis

• U is a unit vector specifying the rotation plane

• C = Cos Ɵ• S = Sin Ɵ

http://en.wikipedia.org/wiki/Rotation_matrix#Rotations_in_two_and_three_dimensions

Page 7: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

The quaternion solution

Q Quaternion is a 4-tuple of numbers, typically called (s, x, y, z)

S acts as a scalar, and xyz act as a 3D vectorThe vector xyz should satisfy X2 + Y2 + Z2 = XYZ = -1 Where s2 + X2 + Y2 + Z2 = 1 the quaternion can be

considered a rotation.

Page 8: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Quaternion Addition

Page 9: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Quaternion Subtraction

Page 10: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Quaternion Multiplication

Page 11: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Quaternion Division

• Don’t divide by Zero!

Page 12: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Quaternion Advantages

StorageMatrix must be at minimum 3x3 typically stored

as a 4x4.A quaternion may be stored as 4 values.Chaining (preparing) an xyz rotation:

Method # multiplies # add/subtracts

total operations

Rotation matrices

27 18 45Quaternions 16 12 28

Page 13: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

• Performing an xyz Rotation

Method # multiplies

# add/subtracts

# sin/cos total operations

Rotation matrix

9 6 0 15Quaternions

21 18 0 39Angle/axis 23 16 2 41

Page 14: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Smooth rotation interpolation

• Ideal for animation• LERPING between one rotation and a second

will take the shortest path using a quaternion

Page 15: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Collision Detection

• What is it?

• What does it need to be?– Precise– Efficient– Fast

Page 16: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

What collides?

• Almost everything• This would result in roughly n2 different

collisions to detect each frame.

Page 17: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Cheating the system

• We CAN mathematically check every single polygon against every other.

• Just not in this lifetime!

Page 18: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Bounding Spheres

• Collision if r1 + r2 < distance from P1 to P2

http://www.gamasutra.com/view/feature/3190/advanced_collision_detection_.php?page=2

Page 19: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Bounding Boxes

• Axis Aligned (AABB)– This allows for compatibility with oct trees, and

extreme simplification of intersections• Oriented (OBB)– These will fit strange objects better– Only need to be created once– Much more complex to intersect

Page 20: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

A strange object

Page 21: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Bounding Trees

• We can create a small tree to bound a stupid object.– These must be built offline– Each vertex must be matrix multiplied into the

world, so things will start to get expensive

Page 22: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

The Separating Axis Theorem

• This theorem allows for collision detection utilising separating axis’ is 2D

• It dumbs down to the ability to put a line between the two objects.

• This extends to 3D using aseparating plane

Convex Only!!!

Page 23: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Stationary Objects

• Do not move, so we do not have to detect collisions by the object, only on the object!

Page 24: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

BSP Trees and Collisions

• Using Cartesian plane equations and the vertices of our object BSP intersections are extremely quick.

• ax + by + cz + d = 0• ax + by + cz + d > 0 positive side of the BSP

polygon• ax + by + cz + d < 0 negative side of the BSP

polygon

Page 25: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Size == Time step

• A small object will need a smaller change in time to detect collisions.

• How can you make this work?• The two special cases:– Small vs Small object (both high

resolution)– Small vs large object (large is at a low

resolution)

http://www.gamasutra.com/view/feature/3190/advanced_collision_detection_.php?page=2

Page 26: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Bezier Curve Collisions• We can use the Convex Hull Property for coarse

collision detection• This extends to 3D easily.• For more accurate collisions you will need to do

something like Cartesian plane equations, by calculating the plane at points on the curve

• Tessellation into polygons remains the most common approach

Page 27: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

For only low detail Bezier collisions...

• We can use a vastly simplified polygon surface• This is useless as a basis for a high accuracy collision

detection algorithm, as it will reject some collisions

http://www.gamasutra.com/view/feature/3190/advanced_collision_detection_.php?page=3

Page 28: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Per-Pixel Collision Detection

• The Atari had built-in collision detection in the video chip, this made collisions extremely simple

• Can we do something similar today?

Page 29: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

GPU Based Collision Detection

• There is a bit of research in this area– GPU only solutions– CPU-GPU hybrids

Page 30: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Direction Based Collision Trees

• What if we only consider colliding with objects in the direction that the focus object is travelling?

• Calculating the Normal Plane for the rear-most point would give us an instant cull of many objects

Page 31: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

A Solid BSP Tree Tutorial

• http://www.gamedev.net/reference/articles/article657.asp

Page 32: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Advanced Collision Detection

• http://www.realtimerendering.com/intersections.html

• This is a great table for finding algorithms

Page 33: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Specular Lighting

• This is the light that an object reflects into the viewers eye

• For this we will need to add a few more variables to our light system

http://prosjekt.ffi.no/unik-4660/lectures04/chapters/jpgfiles/specular.jpg

Page 34: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

http://www.baylee-online.net/Projects/Raytracing/Algorithms/Glossy-Reflection-Transmission

Page 35: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Creating a cone

• A modified version of Lambert’s Cosine law is implemented, giving an easily controlled and calculated light cone.

“the radiant intensity observed from a "Lambertian" surface is directly proportional to the cosine of the angle θ between the observer's line of sight and the surface normal.” - Wikipedia

Page 36: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

So the intensity of the light drops off as the angle increases.

• Light intensity = cos()n • n is an exponent which changes the speed of

the falloff for a specular highlight.• A high n will create a very sharp reflection

through a small cone• A low n (n is always >=1) will create a much

broader cone of reflection.

Page 37: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

Creating a conehttp://fooplot.com/

Page 38: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

The new variables

• View (eye) PositionWe generate a vector from the surface point to

the eye:toEye = view – vertex.position;toEye = Normalize(toEye);

Page 39: Quaternions Paul Taylor 2010. Swizzle What is a swizzle?

The new jobs:specularPower = vertex.specular.a // a = power// Minimum of 1.0fspecularPower = max(specularPower, 1.0f);Float3 reflection = reflect(light.vector, vertex.normal);specularFactor = dot(reflection, toEye);// No specular if light is behindspecularFactor = max(specularFactor, 0.0f);// Now we add the exponentspecularFactor = pow(specularFactor, specularPower);light += specularFactor * vertex.specular * light.specular;Return light;