So jMonkey engine does not allow rotation more than 180 or less than -180. I wrote this code to divide whatever angle not within the limits into smaller slerps smaller than 180 degrees and attach them sequentially. IT used to work fine with RotationTrack but not as well with AnimationTrack as the spatial animates but animation breaks up and is not consistent.
[java]
public void rotateSpatial(String id, float trigger_time, float theta,
float oldX, float oldY, float oldZ, float oldTheta, float duration ) {
final Spatial spatialToMove = rootNode.getChild(id);
float rotDuration = duration;
// get final angle to rotate to
Quaternion rotationAngle = Conversions.degreesToQuaternionOnZ(theta);
// if spatial is rotating around the angles not allowed
// in jMonkey divide them into smaller slerps
if (theta > 180 || theta < -180) {
int loopsNumber = (int) Math.ceil(theta / 180) + 1;
// rotate -2.5 to lower
if (theta < epsilon) {
loopsNumber = -(int) Math.floor(theta / 180);
}
for (int i = 1; i <= loopsNumber; i++) {
double slerp = theta / loopsNumber;
float slerpDuration = duration / loopsNumber;
Quaternion rotation = new Quaternion().fromAngleAxis(
(float) ( (slerp * i) * FastMath.DEG_TO_RAD),
Vector3f.UNIT_Z);
// create rotate animation
rotateAnimation(spatialToMove, slerpDuration, "rotate"+rotateCounter,
rotation, oldX, oldY, oldZ, oldTheta);
// add cinematic event to cinematic buffer
cinematic.addCinematicEvent(trigger_time + (slerpDuration * (i - 1)), new AnimationTrack(spatialToMove, "rotate"+rotateCounter));
// increment counter for next event
rotateCounter++;
oldTheta = (float) (slerp * i );
}
}
[/java]
you don’t have to bother to do this with AnimationTrack I integrated the code that handle rotation of more than 180°
Use addTimeRotationAngles(float,float float); you can use Euler angles with this one and it does all the complicated calculation for you.
It’as all explained in the javadoc please read it.
3 Likes
thats awesome!!!
thanks and great job!!! you just reduced my one PAGE function into 3 lines