Rotation and Orientation - PowerPoint PPT Presentation

1 / 77
About This Presentation
Title:

Rotation and Orientation

Description:

Easy to do pitch, yaw, and roll. Can be interpolated, but is not exact. Disadvantages ... Example: Roll, Pitch, Yaw. private Vector3 position = Vector3.Zero; ... – PowerPoint PPT presentation

Number of Views:137
Avg rating:3.0/5.0
Slides: 78
Provided by: cbo4
Category:

less

Transcript and Presenter's Notes

Title: Rotation and Orientation


1
Rotation and Orientation
  • Rotation 6 ways
  • Angles
  • Matrices
  • Two Vectors
  • Euler Angles
  • Scaled axis
  • Quaternions

2
Translation is easy
  • Position is position is position
  • (x, y, z)
  • Sometimes (x, y, z, w)
  • Vectors are vectors
  • (x, y, z)
  • Sometimes (x, y, z, 0)

We can do all we need to do with just (x, y, z).
Few reasons for alternative representations.
3
Rotation is hard
  • There is no single way to represent rotation that
    doesnt have some major disadvantage
  • We will use several ways to represent rotation

4
Orientation vs. Rotation
  • Rotation an operation that rotates something
  • Orientation the way something has been rotated
  • We can specify an orientation by specifying a
    rotation that gets us into that orientation

Well often use these terms interchangeably.
Were often interested in either knowing
something about the current orientation or
achieving some desired orientation.
5
What we really care about
  • Getting something pointing the way we want
  • Incrementally rotating something
  • Figuring out which way something is pointing
  • Getting something from one orientation to another
  • Usually called interpolation

6
Simple Angles
  • Handy mostly for 2D orientation
  • Which way is our guy pointing?
  • Easy to express this as a single rotation value

What angle is this, BTW?
7
Which way are we pointing?
  • Depends on which way we started out pointing
  • Common (0, 0, 1)
  • Just apply a 2D rotation around Y
  • x xcosqzsinq
  • z -xsinqzcosq
  • Ultra easy in this case

Vector in the direction our guy will spit (sinq,
0, cosq)
8
Getting from q1 to q2
  • We are at angle q1. We want to rotate to q2 in t
    seconds.
  • How much should I step if d time has passed?
  • Pretty obvious, huh?

This can actually do some really strange things!
9
The Problem
  • All of these rotate to the same orientation!
  • Why?
  • You wanted 0.21 to 5.34
  • But, the current orientation is actually 31.626.
  • What will happen?

Even if you are lucky enough that the current
orientation is 0.21, you may not get what you
want or expect! (Why?)
10
How to fix
  • Difference in the angles should be less than or
    equal to PI
  • To avoid going around too many times or going the
    long way around

if (angleTo gt angle)
while ((angleTo - angle) gt Math.PI)
angleTo - 2
(float)Math.PI else
while ((angleTo -
angle) lt -Math.PI) angleTo
2 (float)Math.PI

11
Simple Angles
  • Advantages
  • Good way to deal with 2D orientation
  • Simple to implement
  • Interpolate well
  • Compose using simple addition
  • orientation orientation rotation
  • Disadvantages
  • Not unique for a given orientation
  • Requires expensive trig functions

12
Transformation Matrices
Very powerful Can combine rotation, scaling,
and translation into one item.
13
Observation Matrices for orientation and
position
Any transformation consisting of rotations and
translations in any order can be broken down into
a rotation followed by a translation. The
rotation is our orientation, the translation is
our position.
TT
14
Rotation Submatrix
The 3x3 upper left submatrix is called the
rotation submatrix. It tells us all we need to
know about rotation/orientation.
15
What we care about
  • We have two parts rotation and translation
  • What I really care about now is rotation
  • Translation is really easy to understand
  • Were really interested in that 3x3 rotation
    sub-matrix
  • We want to know what they mean
  • We want to know how to create them

16
Which was is something pointing?
  • After we rotate we want to know which way is
    forward, backward, etc.
  • We want to know what these vectors rotate to
  • (1, 0, 0)
  • (0, 1, 0)
  • (0, 0, 1)

17
Very easy
  • You can determine the directions something is
    facing directly from the matrix
  • You dont have to multiply anything!
  • XNA has properties to access these rows

18
XNA properties
r.Right r.Up r.Backward r.Translation
Directions assume you are facing the object from
the Z direction. Strange, I know. Also provides
Right, Down, and Forward
Vector3 laserDirection xwing.Transform.Backward

TT
19
Notation
Youll see versions like this or transposed
version in many places including the textbook.
20
Creating Rotation Matrices
  • Product of simple rotations
  • CreateRotationX, CreateRotationY, CreateRotationZ
  • CreateFromAxisAngle
  • Rotates around an arbitrary (normalized) vector
    by a given angle.
  • Manually built from direction vectors
  • We often know that something needs to point that
    way
  • that way is a vector
  • That way is not really enough by itself
  • We usually want an up vector as well

21
XNA properties
r.Right r.Up r.Backward r.Translation
If we know where we want the ship to point, we
know what the Z axis rotates to. If we know what
direction is up for the ship, we know which
direction the Y axis rotates to. Can we figure
out X?
22
XNA properties
r.Right r.Up r.Backward r.Translation
Matrix M M.Backward shipDirection M.Up
shipUp M.Right Vector3.Cross(shipUp,
shipDirection) M.Translation shipPosition
23
Intuition
r.Right r.Up r.Backward r.Translation
The matrix told us where the vectors rotated to.
If we know that we can build a matrix ourselves.
TT
24
Example
  • Lets make a ship fly in a spiral path
  • What is it going to take to do this?

r Radius of the spiral sr Rotational speed
around the spiral st Translation speed for z
only
25
Current XNA code
Member variables that describe the ships position
and orientation
private Matrix orientation private Vector3
position
In Update()
position new Vector3(Radius
(float)Math.Cos(time RadialSpeed),
Radius (float)Math.Sin(time RadialSpeed),
time LinearSpeed)
In Draw()
DrawModel(graphics, camera, xwing,
orientation Matrix.CreateTranslation(p
osition))
26
We need to know which way we are going
To get the orientation, just take the derivative
of each of the parts. The resulting vector is the
direction we are moving.
position new Vector3(Radius
(float)Math.Cos(time RadialSpeed),
Radius (float)Math.Sin(time RadialSpeed),
time LinearSpeed)
TT
27
We need to know which way we are going
To get the orientation, just take the derivative
of each of the parts. The resulting vector is the
direction we are moving.
TT
28
XNA
Vector3 shipZ new Vector3(-Radius RadialSpeed
(float)Math.Sin(time RadialSpeed),
Radius RadialSpeed (float)Math.Cos(time
RadialSpeed),
LinearSpeed) shipZ.Normalize()
Next question Which way is up?
29
Up direction
Vector3 up new Vector3(0, 0, position.Z) -
position
(0, 0, position.Z)
30
Now, we need the ship X and Y axis
Vector3 shipX Vector3.Cross(up,
shipZ) shipX.Normalize()
Vector3 shipY Vector3.Cross(shipZ, shipX)
orientation.Backward shipZ
orientation.Up shipY
orientation.Right shipX
31
public override void Update(GameTime
gameTime) time
(float)gameTime.ElapsedGameTime.TotalSeconds
position new Vector3(Radius
(float)Math.Cos(time RadialSpeed),
Radius (float)Math.Sin(time RadialSpeed),
time LinearSpeed)
Vector3 shipZ new Vector3(-Radius RadialSpeed
(float)Math.Sin(time RadialSpeed),
Radius RadialSpeed (float)Math.Cos(time
RadialSpeed), LinearSpeed)
shipZ.Normalize() Vector3 up new
Vector3(0, 0, position.Z) - position
Vector3 shipX Vector3.Cross(up, shipZ)
shipX.Normalize() Vector3 shipY
Vector3.Cross(shipZ, shipX)
orientation.Backward shipZ
orientation.Up shipY
orientation.Right shipX
32
Some Rotation Matrix Information
  • The inverse of any rotation matrix is the
    transpose of the matrix!
  • The rows are normalized vectors
  • The rows are all orthogonal to each other
  • The columns are normalized vectors
  • The columns are all orthogonal to each other
  • The determinant is 1 (serious trivia there)

This type of matrix is called orthonormal since
the rows and columns are all normalized and
orthogonal to each other.
TT
33
Using orientation matrix directly
You can use a matrix to keep track of the
orientation and simply update it as we change
orientation
orientation Matrix.CreateRotationY(yawRate
delta) orientation
The problem with this idea Each matrix multiply
does 64 float multiplies and 48 float additions.
Each has a small amount of roundoff error due to
numerical precision. This error accumulates and
the matrix ceases to be orthonormal!
Objects slowly start to change size, proportions
and gets skewed. See the demo for what can
happen.
34
Renormalizing
orientation.Backward Vector3.Normalize(orientati
on.Backward) orientation.Up Vector3.Normalize(V
ector3.Cross(orientation.Backward,
orientation.Right)) orientation.Right
Vector3.Cross(orientation.Up, orientation.Backward
)
You have to force the matrix to be orthonormal
again fairly regularly.
35
Rotation Matrix Advantages/Disadvantages
  • Advantages
  • Composes with simple multiplication
  • orientation rotation orientation
  • Composes with translation easily
  • Usually the final destination anyway
  • Easy to build a matrix for a desired orientation
  • Unique for a given orientation
  • Disadvantages
  • 9 numbers for 3 bits of information
    (over-specified)
  • Uses more memory
  • Do not interpolate easily
  • Can accumulate round-off errors easily

36
Two Vectors
  • A common way to keep track of orientation
  • Maintain two vectors
  • A direction we are going
  • Our current up direction
  • We can then change these to change our orientation

37
Example
38
Basic Operations
  • Pitch
  • Rotation around the ship X axis
  • Yaw
  • Rotation around the ship Y axis

39
How do we determine the axis?How do we make the
matrix?
Vector3 shipZ dir // Assumes
dir is normalized Vector3 shipX
Vector3.Cross(up, shipZ)
shipX.Normalize() Vector3 shipY
Vector3.Cross(shipZ, shipX)
orientation.Backward shipZ
orientation.Up shipY
orientation.Right shipX
Basically the same thing as before. But, where
would be put this? Whats local and whats a
member variable?
40
When and where
determines
dir/up Our orientation description
Our orientation matrix
UpdateOrientation()
Vector3 shipZ dir // Assumes dir is
normalized Vector3 shipX
Vector3.Cross(up, shipZ)
shipX.Normalize() Vector3 shipY
Vector3.Cross(shipZ, shipX)
orientation.Backward shipZ
orientation.Up shipY
orientation.Right shipX
Call this each time you change dir or up.
41
Cool C Syntax
public Vector3 Dir get return dir set
dir value UpdateOrientation()
Were actually keeping track of orientation two
ways two vectors and a matrix. But, the
vectors are the actual orientation description.
The matrix is computed from the vectors. We need
the matrix to draw. Its very common to have
some means of describing orientation other than a
matrix and then a matrix that is dependent on
that description.
42
Pitch
public void Pitch(float angle)
Matrix rotation Matrix.CreateFromAxis
Angle(orientation.Right, angle) up
Vector3.TransformNormal(up, rotation)
up.Normalize() dir
Vector3.TransformNormal(dir, rotation)
dir.Normalize() UpdateOrientation()

Why am I normalizing? Arent they already
normalized?
43
Yaw
public void Yaw(float angle)
Matrix rotation Matrix.CreateFromAxisAn
gle(orientation.Up, angle) up
Vector3.TransformNormal(up, rotation)
up.Normalize() dir
Vector3.TransformNormal(dir, rotation)
dir.Normalize() UpdateOrientation()

Why am I rotating up?
TT
44
Roll
public void Roll(float angle)
Matrix rotation Matrix.CreateFromAxisA
ngle(orientation.Backward, angle) up
Vector3.TransformNormal(up, rotation)
up.Normalize() UpdateOrientation()

Why am I not rotating dir?
45
Round-off Error Issues
  • Multiple rotations will make the vectors no
    longer normal
  • Just renormalize and they will be fine
  • Multiple rotations will make up not orthogonal to
    dir
  • You can replace up with orientation.Up which will
    definitely be orthogonal

46
Interpolation
  • The Two Vectors method gives us a new capability
  • We can interpolate between poses
  • Why would we want to do this?

47
Interpolating a position
48
Interpolating Vectors
0
t
I Initial direction
V Interpolated vector
t is the fraction of the total time. If you
start at time 7, end at time 11, and the current
time is 8, t0.25.
1
F Final direction
ladderDir Vector3.Normalize(originalDir t
(finalDir - originalDir)) ladderUp
Vector3.Normalize(originalUp t (finalUp -
originalUp)) UpdateOrientation()
TT
49
UpdateOrientation()
private void UpdateOrientation()
Vector3 ladderZ ladderDir
Vector3 ladderX Vector3.Normalize(Vector3.C
ross(ladderUp, ladderDir)) Vector3
ladderY Vector3.Cross(ladderZ, ladderX)
orientation.Backward ladderZ
orientation.Up ladderY
orientation.Right ladderX
50
Problems with interpolating vectors
a
b
c
d
e
f
g
h
I Initial direction
F Final direction
TT
51
Problems with interpolating vectors
I Initial direction
F Final direction
If you step like this, the steps are not uniform
in time. It will appear to move faster in the
middle of the rotation than near the ends.
52
Spherical Linear Interpolation - Slerp
I Initial direction
F Final direction
q
Spherical Linear Interpolation, or Slerp, changes
the rate of speed so the angle steps are
constant. Looks more realistic.
53
Slerp
private Vector3 Slerp(Vector3 I, Vector3
F, float t) float cosTheta
Vector3.Dot(I, F) if (cosTheta
1) return I
float theta
(float)Math.Acos(cosTheta) float
sinTheta (float)Math.Sin(theta)
return (float)Math.Sin((1 - t) theta) /
sinTheta I (float)Math.Sin(t
theta) / sinTheta F
54
Slerp Issues
  • Will always go the shortest way around
  • Watch out for no-change situations (q0)
  • Wont work if qp (why?)
  • Slerp on two vectors is not an exact solution for
    up

55
Two Vectors orientation Advantages/Disadvantages
  • Advantages
  • Can describe any orientation
  • Easy to do pitch, yaw, and roll
  • Can be interpolated, but is not exact
  • Disadvantages
  • 6 numbers for 3 bits of information (still
    over-specified)
  • Can accumulate round-off errors easily
  • Not natural for all applications
  • Do not compose easily
  • Have to convert to matrices, multiple, and
    convert back.

56
Euler Angles
  • Euler Angles specify an orientation with three
    rotations around coordinate axis

XYZ Euler Angle
  • Many combinations are possible
  • Common XYZ, YXZ, ZXY

Matrix orientation Matrix.CreateRotationX(rx)

Matrix.CreateRotationY(ry)
Matrix.CreateRotationZ(rz)
TT
57
Euler Angle Advantages/Disadvantages
  • Advantages
  • Natural Description
  • Easy to define limits
  • Can be interpolated, but only by component
  • Minimal specification (3 numbers)
  • Disadvantages
  • Requires use of trig functions and must know
    angles
  • Interpolation is not in a straight line and may
    do strange things
  • Lots of redundancy
  • Gimbal lock
  • Do not compose easily (in fact very hard to
    compose)

58
Keyframe Animation
A list of orientations and positions at given
points in time keyframes. For each keyframe
time, orientation, position. We are responsible
for filling in the gaps between times.
59
Quaternions
  • Quaterions are one of the most powerful ways to
    represent rotation
  • They will seem almost magical in what they do

60
The basic idea
Imaginary
(a,b)
  • Complex numbers can represent 2D rotation

q
Real
This is a completely made up mathematical
concept, but it turns out to be very useful. It
can represent angles with no trig functions.
TT
61
Complex Number advantages
Imaginary
  • A normalized complex number uniquely represents a
    rotation
  • Multiplying two complex numbers together add the
    rotation angles.

(a,b)
q
Real
  • So, make q into a complex number cq and w into a
    complex number cw
  • cqcw cqw

Normalizing a complex number. Just like
normalizing a vector.
62
Can we extend this to 3D?
  • Why do we care? Complex numbers represent
    rotation in a form that can be easily multiplied
    together with simple multiplication and division
    operations. Wed love to be able to do that in
    3D with something smaller than a matrix. You can
    compose a rotation and orientation with a single
    multiplication.
  • Well, we have real and imaginary. Could we have
    even more imaginary?
  • Yes, we can!
  • William Rowan Hamilton invented quaternions in
    1943 which provide a tool for rotation that
    easily composes and interpolates. Well use them
    a lot.

63
Quaternion Plaque on Broom Bridge in Dublin,
Ireland
Here as he walked by on the 16th of October 1843
Sir William Rowan Hamilton in a flash of genius
discovered the fundamental formula for quaternion
multiplication i2j2k2ijk -1 cut it on a
stone of this bridge
64
The main idea
A unit quaternion
Think of them as 4 element vectors. To make a
unit quaternion, you simply divide by the length
of the vector. This is Normalizing just as in 2D
and 3D vectors.
65
How much do we really need to know?
  • A quaternion represents a rotation.
  • q1 q2 represent rotation q2 followed by
    rotation q1.
  • q and q are the same rotation.
  • A multiplication rule exists (you can look it up)

Let (x, y, z) be a normalized vector and q an
angle
Is a rotation around (x,y,z) by q radians
Quaterion inverse
TT
66
Creating a Quaternion
Youre given a Vector v and an angle to rotate
around that vector of q. The quaternion to do
that will be
public static Quaternion CreateFromAxisAngle(Vecto
r3 axis, float angle) Quaternion
quaternion float num2 angle 0.5f
float num (float) Math.Sin((double) num2)
float num3 (float) Math.Cos((double) num2)
quaternion.X axis.X num quaternion.Y
axis.Y num quaternion.Z axis.Z num
quaternion.W num3 return quaternion
67
Composing Quaternions
Quaternion q Quaternion.CreateFromAxisAngle(new
Vector3(0, 0, 1), rx)
Quaternion.CreateFromAxisAngle(new Vector3(0, 1,
0), ry) Quaternion.CreateFromAxi
sAngle(new Vector3(1, 0, 0), rx)
Important Operations read right to left, not
left to right!
TT
68
Example Roll, Pitch, Yaw
private Vector3 position Vector3.Zero
private Quaternion orientation
Quaternion.Identity private Matrix
transform
private void Roll(float angle)
orientation orientation
Quaternion.CreateFromAxisAngle(new Vector3(0, 0,
1), -angle) orientation.Normalize()
UpdateTransform()
private void UpdateTransform()
transform Matrix.CreateFromQuaternion(orienta
tion) transform.Translation
position
Always normalize after you change the
orientation. This will prevent the buildup of
round-off errors.
69
The other two
private void Yaw(float angle)
orientation orientation
Quaternion.CreateFromAxisAngle(new Vector3(0, 1,
0), angle) orientation.Normalize()
UpdateTransform()
private void Pitch(float angle)
orientation orientation
Quaternion.CreateFromAxisAngle(new Vector3(1, 0,
0), -angle) orientation.Normalize()
UpdateTransform()
70
Of course
DrawModel(graphics, camera, Xwing, transform)
71
Quaterion Interpolation
One of the main reasons quaternions are so
popular is that they are easy to interpolate.
They are very commonly used for orientation
keyframes.
We use the same spherical linear interpolation
for quaternions that we used for vectors. Except
this time the interpolation will always be a
perfect linear rotation from A to B.
72
Perfect Rotations
Slerp with two quaternions will rotate from one
orientation to another using a uniform angular
velocity and a fixed rotation axis.
73
Slerp for Quaternions
Dot product is the cosine of ½ the angle between
orientations (important difference) q above is
½ of the angle If cosine is lt 0, the angle is
greater than 180 degrees
TT
74
Going the long way around
Theres always two possible rotations the short
way and the long way. If the cos(q) lt 0, where q
is the angle between the quaternions (not the
angle between the orientations), we are going the
long way around. If that happens, then instead
of doing Slerp(q1, q2, t), so Slerp(q1, -q2, t).
Negating one quaternion will force the other path.
Quaternion q Quaternion.Slerp(q1, q2, t)
XNA
XNA automatically takes the shortest route.
TT
75
Quaternion Advantages/Disadvantages
  • Advantages
  • Compact notation
  • Composes with simple multiplication
  • Very efficient and fast
  • Interpolates perfectly with slerp!
  • Perfect for keyframe animation
  • Disadvantages
  • Not unique q and q are the same rotation and
    there are other redundancies.
  • 4 values to represent 3 things some redundancy.

76
Scaled Axis rotations
Let v be a normalized vector we are rotating
around and q be the rotation angle. A scaled
axis rotation is expressed as qv. Each term is
multiplied by the rotation angle. This is a
compact notation, but only useful for special
applications in physics simulations. Well use
this later.
77
Common Applications
Combinations get used a lot. Very common one
method and we also keep a matrix around computed
from the other method. Skeletons often support
Euler Angles for inverse kinematics and
quaternions for keyframe animation.
Write a Comment
User Comments (0)
About PowerShow.com