Assignment 5: Using SLERP
This assignment is
relatively simple, since all tools are already provided by JMonkey. The assignment
will give you an idea how wonderful quaternions are. As we already learned in
class, the advantage of quaternions, compared to Euler angles, is:
· In many situations, they are easier to
handle, since they represent the rotation axis directly (as opposed to split a
rotation into 3 partial rotations)
· They are faster, when it comes to multiple,
subsequent rotations (they are SLOWER when the actual rotation on a point is
performed)
· There’s no gimbal lock problem (because the
rotation is not split into three parts)
But there is
another huge advantage:
· Quaternions can easily be interpolated,
i.e. there is a natural, smooth way to get from one rotation to another. The
technique to do so is called SLERP (which stands for Spherical linear
interpolation. If you ask me, it’s quite a stretch of language to connect those
to terms).
What does SLERP do?
First we have to remember, that rotations, represented by quaternions, are
points on a Hypersphere (a 4D Sphere). To make this easier, imagine a 3D sphere
for now (as we did in class). To interpolate between two rotations means to
interpolate between two points on the sphere, while staying on the sphere (!).
Please watch this youtube video to get the idea: http://www.youtube.com/watch?v=uNHIPVOnt-Y .
In this video, the
WHITE trace shows the SPHERICAL linear interpolation, while the BLUE trace is a
direct linear interpolation, which does not stay on the sphere (which leads to
a non-smooth, unnatural interpolation. For the mathematicians among us: the
blue trace is located on multiple spheres of smaller radius, i.e. it represents
an unnecessary change of rotation amount between the two endpoints. Visually,
this leads to funny effects. Think about it!)
So: given two
rotations (i.e. a start and an end rotational state of an object, represented
by two quaternions Q_start, Q_end), SLERP, taking parameters (Q_start, Q_end, t),
t in [0..1], returns an interpolated rotation Q_interpol, which lies on the
direct path on the sphere between Q_start (t=0) and Q_end (t=1). Changing the
parameter t between 0 and 1, and applying the quaternion Q_interpol to an
object, rotates the object beautifully between Q_start and Q_end.
Usage of SLERP in
JMonkey is explained here: http://jmonkeyengine.org/wiki/doku.php/jme3:math
. There is a small section dedicated to SLERP. Reading the rest of the website
might also be useful --- it’s a math summary. By now, you should be able to
understand everything on that page.
The math of SLERP
can be found on …. Yes, Wikipedia J : http://en.wikipedia.org/wiki/Slerp
Here is your
assignment (finally!):
Create a Shape S in
JMonkey, e.g. a box. The more complicated the object, the better (best: Oto, the
robot). Use directional lighting, to see rotation effects better.
Create 3 geometries
G_start, G_end, G_interpol, using the same shape S, yet with different materials
(e.g. color, transparency), to make them distinguishable. They represent the
start, end, and interpolated state. At the beginning, assign a 0 rotation to
all of them, i.e. they are all shown in the same pose (you therefore only see
one object on the screen).
Assign keys to
change the rotation of G_start and G_end.
Let G_interpol
constantly (=all the time) move smoothly between G_start and G_end. You achieve
this, by calling SLERP in simpleUpdate with a parameter that changes back and
forth between 0 and 1.
Hence the output on
the screen is: using one key-set (e.g. the arrow keys) you change the rotation
of G_start (represented by a quaternion Q_start). Another key set (e.g. arrow
keys plus shift) does the same with G_end. As you change the poses of G_start
and G_end, G_inerpol wanders between them constantly.
Hint: if you apply
the rotation to a parent node of all geometries, and you translate the
geometries, the effect is even nicer (since then the objects rotate around a
center outside of them).
That’s it!
Good luck.
Deadline: Wednesday
(!) , 3/27