242-515 agd: 8. rotations11 objective o explain the main types of object rotation, with the help of...

43
242-515 AGD: 8. Rotations 1 1 • Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515, Semester 1, 2014-2015 8. Rotations

Upload: hugo-hardy

Post on 22-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 11

• Objectiveo explain the main types of object

rotation, with the help of jME examples

Animation and Games

Development242-515, Semester 1, 2014-2015

8. Rotations

Page 2: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 22

1. Rotation Forms2. Matricies3. Euler Angles4. Multiple Rotations5. Axis-angle Rotation6. Quaternions7. Why use Quaternions?

Overview

Page 3: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 33

• There are four main ways of rotating a 3D object:o matricieso euler angleso axis-angleso quaternions

• jME supports all of these, which I'll demonstrate with variations of the Sinbad.java application, which displays a 3D monster model.

1. Rotations Forms

Page 4: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 44

• The model is facing along the +z axis, towards the camera. It is centered at the origin.

Sinbad.java

+z+x

+y

Page 5: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 55

public void simpleInitApp() { DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal()); sun.setColor(ColorRGBA.White); rootNode.addLight(sun); Spatial player = assetManager.loadModel( "Models/Sinbad/Sinbad.mesh.xml"); player.setLocalScale(0.5f); rootNode.attachChild(player); } // end of simpleInitApp()

Partial Code

I'll be explaining directionallight and mesh models later.

Page 6: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 66

• I described the 4x4 homogenous matrices for rotation about the x-, y-, and z- axes in part 7.

2. Matricies

-rotate

cos sin 0 0

sin cos 0 0

0 0 1 0

0 0 0 1

z

M

Rotates around the z-axis through the angle θ

Page 7: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 77

• Similar matrices for rotations about x-, y- axes

-rotate

1 0 0 0

0 cos sin 0

0 sin cos 0

0 0 0 1

x

M

-rotate

cos 0 sin 0

0 1 0 0

sin 0 cos 0

0 0 0 1

y

M

Page 8: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 88

• The Spatial class has numerous rotation methods.

• The class includes:o Spatial.setLocalRotation(Matrix3f rotation) o It sets the local rotation of this node using a Matrix3f

object.

o The contents of the Matrix3f object should be the top left 3x3 part of the homogenous 4x4 matrix.

Page 9: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 99

• The rotation uses the cosine and sine of 45 degrees (in radians):

Rotating 45 degs around the z-

axis

-rotate

cos sin 0 0

sin cos 0 0

0 0 1 0

0 0 0 1

z

M

45

45 45

45

Page 10: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 1010

RotMatrix.java Result

45 degrees aroundthe z-axis

Page 11: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 1111

public void simpleInitApp() { DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal()); sun.setColor(ColorRGBA.White); rootNode.addLight(sun); Spatial player = assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml"); player.setLocalScale(0.5f); rootNode.attachChild(player);

// create rotation matrix for 45 degs around z-axis float cos45 = (float) Math.cos(Math.toRadians(45)); float sin45 = (float) Math.sin(Math.toRadians(45)); Matrix3f mat = new Matrix3f(Matrix3f.IDENTITY); mat.setColumn(0, new Vector3f(cos45, sin45, 0)); mat.setColumn(1, new Vector3f(-sin45, cos45, 0));

player.setLocalRotation(mat); } // end of simpleInitApp()

RotMatrix.java

(partial)

the new part

Page 12: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 1212

• jME has a FastMath class with faster (but less accurate) trigonometry constants and methods than those in Java's Math class.

• The matrix code can be rewritten using FastMath:

// create rotation matrix for 45 degs around z-axis float cos45 = FastMath.cos(FastMath.QUARTER_PI); float sin45 = FastMath.sin(FastMath.QUARTER_PI);

Matrix3f mat = new Matrix3f(Matrix3f.IDENTITY); mat.setColumn(0, new Vector3f(cos45, sin45, 0)); mat.setColumn(1, new Vector3f(-sin45, cos45, 0)); :

the new part

Page 13: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 1313

• Euler angles express rotations in terms of angles around the object's local x-, y-, and z- axes.

• There's no need to build a matrix, just specify the three angles.

• Traditionally, these angles are called:o yaw – a rotation around the y-axiso pitch – a rotation around x-axiso roll – a rotation around the z-axis

3. Euler Angles

+z

+x

+y

Page 14: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 1414

• The relevant Spatial method for Euler angle rotations is:o Spatial.rotate(float x, float y, float z) o it rotates the spatial by the x-, y- and z- axis angles (in

radians), in the local coordinate spaceo the arguments are not (yaw, roll, pitch) even though

that's what the documentation says

• The RotRoll.java application applies a 45 degree roll (rotation around the z-axis) using euler angles instead of a matrix.

Rotating 45 degs around the z-axis

(again)

Page 15: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 1515

• The same result as RotMatrix.java

RotRoll.java Result

Page 16: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 1616

public void simpleInitApp() { DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal()); sun.setColor(ColorRGBA.White); rootNode.addLight(sun); Spatial player = assetManager.loadModel( "Models/Sinbad/Sinbad.mesh.xml"); player.setLocalScale(0.5f); rootNode.attachChild(player);

player.rotate(0, 0, (float)Math.toRadians(45)); // player.rotate(0, 0, FastMath.QUARTER_PI); // roll: rotate around z-axis } // end of simpleInitApp()

Partial Code

No matrixcode required.

RotRoll.java

Page 17: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 1717

• An euler angle rotation rotates the local coordinate space (and everything inside it).

• This means that the x-, y-, and z- axes of the local space will no longer match up with the x-, y-, and z- axes of the global space.

Rotating the Local Coord. Space

xz

y

xz

y

local coord. space

global coord. space

spatial.rotate()

x

z

y

xz

y

local coord. space

global coord. space

Page 18: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 1818

• Make the model turn constantly around its y-axis.

Turning Around the y-axis

RotLoop.java (very similar to part 6's HelloLoop.java)

Page 19: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 1919

• The constant change requires the call to rotate() to be moved to the simpleUpdate() method inherited from Simple Application.

private Spatial player; // now global public void simpleInitApp() { DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal()); sun.setColor(ColorRGBA.White); rootNode.addLight(sun); player = assetManager.loadModel( "Models/Sinbad/Sinbad.mesh.xml"); player.setLocalScale(0.5f); rootNode.attachChild(player); } // end of simpleInitApp()

Partial CodeRotLoop.java

Page 20: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 2020

public void simpleUpdate(float tpf) // update action, called from game loop automatically // the player is rotated around the y-axis { player.rotate(0, 2*tpf, 0); }

Page 21: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 2121

• An euler angle rotation rotates the local coordinate space.

• This can become confusing when multiple euler rotations are applied to a model.

• For example: player.rotate(0, FastMath.HALF_PI, 0); // y-axis rotation player.rotate(0, 0, FastMath.HALF_PI); // z player.rotate(FastMath.HALF_PI, 0, 0); // x

• It's confusing since the x-, y-, and z-axes of the local coordinate space do not match the global space axes after the first rotation.

4. Multiple Rotations

Page 22: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 2222

zy

x

yz

x

rotate(0, FastMath.HALF_PI, 0);90 degree y-axis rotation

xz

yx

z

y

rotate(0, 0, FastMath.HALF_PI);90 degree z-axis rotation

rotate(FastMath.HALF_PI,0,0);90 degree x-axis rotation

Page 23: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 2323

RotationTest1 Result

Page 24: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 2424

public void simpleInitApp() { DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal()); sun.setColor(ColorRGBA.White); rootNode.addLight(sun); Spatial player = assetManager.loadModel( "Models/Sinbad/Sinbad.mesh.xml"); player.setLocalScale(0.5f); rootNode.attachChild(player);

player.rotate(0, FastMath.HALF_PI, 0); // y-axis rotation player.rotate(0, 0, FastMath.HALF_PI); // z player.rotate(FastMath.HALF_PI, 0, 0); // x } // end of simpleInitApp()

Partial CodeRotationTest1.java

Page 25: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 2525

• The complexity of multiple rotate() calls is made worse, if several euler angle rotations are combined into a single rotate() call.

• For example:player.rotate(0, FastMath.HALF_PI, 0); // y-axis rotation

player.rotate(0, 0, FastMath.HALF_PI); // z player.rotate(FastMath.HALF_PI, 0, 0); // x

• is the same asplayer.rotate(FastMath.HALF_PI, FastMath.HALF_PI,

FastMath.HALF_PI);

• You must remember the hidden order: y, z, x.o this is not the same as (x, y, z) or (z, y, x) or others

since rotation is non-commutative.

More Complex rotate() Calls

Page 26: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 2626

• e.g: reorder the rotate() lines in RotationsTest1.java: player.rotate(FastMath.HALF_PI, 0, 0); // x was last, now first player.rotate(0, FastMath.HALF_PI, 0); // y player.rotate(0, 0, FastMath.HALF_PI); // z

• Result:

z

y

x

Page 27: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 2727

• An axis-angle rotation is a rotation around a user-specified vector (a rotation axis)o this is more flexible than an euler angle rotation which

fixes the vector to be the x-, y-, or z- axis

5. Axis-angle Rotation

Page 28: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 2828

• Rotate the model 90 degrees around the y=x line

RotAxis.java

xz

y x

z

y

Page 29: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 2929

public void simpleInitApp() { DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal()); sun.setColor(ColorRGBA.White); rootNode.addLight(sun); Spatial player = assetManager.loadModel( "Models/Sinbad/Sinbad.mesh.xml"); player.setLocalScale(0.5f); rootNode.attachChild(player);

// rotate 90 degrees around y=x line Vector3f xyVec = new Vector3f(1.0f, 1.0f, 0).normalize(); Quaternion q1 = new Quaternion().fromAngleAxis(FastMath.HALF_PI, xyVec);

player.rotate(q1); }

Partial Code

In jME an axis-angle mustbe executed as a quaternion

axisangle

RotAxis.java

Page 30: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 3030

• Euler's theorem: any rotation can be written as a single rotation about an axis.

• This means that we can rotate a model to any position with one axis-angle rotationo but working out the necessary axis and angle can be

tricky!

Euler's Rotation Theorem

Page 31: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 3131

• Quaternions are an extension to complex numbers. A quaternion is made up of 4 values:o one is a scalar (called w), and the other three are a

vector in 3D complex number space (i, j, k)

• A quaternion (i, j, k, w) can be viewed as a point on the surface of a 4D sphere o which I'll draw as a 3D sphere

6. Quaternions

(i, j, k, w)

Page 32: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 3232

• There's a simple mapping between an axis-angle and a quaternion.

• If the axis-angle is [x, y, z, θ] then the corresponding quaternion (i, j, k, w) is equal to:o ( sin(θ/2)*x, sin(θ/2)*y, sin(θ/2)*z, cos(θ/2) )

• Example: a rotation of 90 degrees around the y-axiso axis-angle = [0, 1, 0, π/2]o quaternion = (0, sqrt(2)/2, 0, sqrt(2)/2 )

• since cos(45) = sin(45) = sqrt(2)/2

Quaternions and Axis-Angles

Page 33: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 3333

public void simpleInitApp() { DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal()); sun.setColor(ColorRGBA.White); rootNode.addLight(sun); Spatial player = assetManager.loadModel( "Models/Sinbad/Sinbad.mesh.xml"); player.setLocalScale(0.5f); rootNode.attachChild(player);

// rotate 90 degs around y-axis float trig = FastMath.sqrt(2)/2; Quaternion q1 = new Quaternion(0, trig, 0, trig); // x, y, z, w // carry out y-axis rotation of 90 degrees player.rotate(q1); } // end of simpleInitApp()

Quaternions in jME

new part

Page 34: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 3434

• Two quaternion rotations are combined ('added together') by multiplicationo e.g. carry out two 90 degree rotations around the y-

axis:

// rotate 90 degs around y-axis float trig = FastMath.sqrt(2)/2; Quaternion q1 = new Quaternion(0, trig, 0, trig); // x, y, z, w

// carry out two rotations of 90 degrees Quaternion q2 = q1.mult(q1); player.rotate(q2);

Combining Rotations

RotQuats.java

Page 35: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 3535

• We often want to smoothly rotate a model from one position to anothero called rotational interpolation

• Calculating a smooth rotational interpolation between two matrix rotations, two euler angles, or two axis-angles is difficulto but rotational interpolation is easy between

quaternions

7. Why Use Quaternions?

Page 36: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 3636

• Rotating between two quaternions is equivalent to following the shortest path of the great circle arc between themo a great circle arc is the distance over the sphere's

surface between two pointso called slerping (spherical linear interpolatation)

Slerping

Page 37: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 3737

• We want to smoothly rotate the model between the following two positions:

Slerping in jME

rotation of -90 degreesaround the z-axis

rotation of 180 degreesaround the y = -x line

Page 38: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 3838

y

zx

rotate -90 degree around z-axis

xz

y

xz

y

rotate 180 degrees aroundthe y= -x line

xz

y

Page 39: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 3939

• The simpleInitApp() method creates the two positions as quaternion rotations.

• The simpleUpdate() method uses jME's Quaternion.slerp() to smoothly interpolate between the two quaternionso this will cause the model to slowly rotate between the

two positions, updated with each call to simpleUpdate()

ImplementationRotSlerp.java

Page 40: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 4040

Execution RotSlerp.java

Page 41: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 4141

// globals private static final float SLERP_STEP = 0.025f;

private Spatial player; private Quaternion rightFrontQ, leftBackQ, slerpQ;

private float currSlerp = 0f; private boolean isForward = true;

public void simpleInitApp() { DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal()); sun.setColor(ColorRGBA.White); rootNode.addLight(sun); player = assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml"); player.setLocalScale(0.5f); rootNode.attachChild(player); :

RotSlerp Code

Page 42: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 4242

: // rotated -90 degs around z-axis Vector3f zVec = new Vector3f(0, 0, 1.0f); rightFrontQ = new Quaternion().fromAngleAxis(-FastMath.HALF_PI, zVec);

// rotated 180 degs around y=-x line Vector3f negxyVec = new Vector3f(-1.0f, 1.0f, 0).normalize(); leftBackQ = new Quaternion().fromAngleAxis(FastMath.PI, negxyVec);

slerpQ = new Quaternion(); player.rotate(rightFrontQ); // player starts at rightFront } // end of simpleInitApp()

Page 43: 242-515 AGD: 8. Rotations11 Objective o explain the main types of object rotation, with the help of jME examples Animation and Games Development 242-515,

242-515 AGD: 8. Rotations 4343

public void simpleUpdate(float tpf) // update action, called from game loop automatically // the player is slerped between the two quaternions { if (isForward) currSlerp += SLERP_STEP; else currSlerp -= SLERP_STEP;

slerpQ.slerp(rightFrontQ, leftBackQ, currSlerp); player.setLocalRotation(slerpQ);

if ((currSlerp >= 1f) || (currSlerp <= 0f)) isForward = !isForward; } // end of simpleUpdate()

slerpQ is a rotationbetween the other two

varies from 0 to 1and back to 0