From 298c052d24fb90dc912913ba463d4016cb598503 Mon Sep 17 00:00:00 2001 From: "rem..om" Date: Mon, 17 Oct 2011 20:55:38 +0000 Subject: [PATCH] - Use quaternions and slerp for rotation track git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8432 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../jme3/cinematic/events/RotationTrack.java | 39 ++++++++++--------- .../jme3test/animation/TestCinematic.java | 8 ++-- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/engine/src/core/com/jme3/cinematic/events/RotationTrack.java b/engine/src/core/com/jme3/cinematic/events/RotationTrack.java index 065add4a2..99606729b 100644 --- a/engine/src/core/com/jme3/cinematic/events/RotationTrack.java +++ b/engine/src/core/com/jme3/cinematic/events/RotationTrack.java @@ -11,9 +11,9 @@ import com.jme3.export.InputCapsule; import com.jme3.export.JmeExporter; import com.jme3.export.JmeImporter; import com.jme3.export.OutputCapsule; -import com.jme3.math.FastMath; import com.jme3.math.Quaternion; import com.jme3.scene.Spatial; +import com.jme3.util.TempVars; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; @@ -25,8 +25,8 @@ import java.util.logging.Logger; public class RotationTrack extends AbstractCinematicEvent { private static final Logger log = Logger.getLogger(RotationTrack.class.getName()); - private float[] startRotation; - private float[] endRotation; + private Quaternion startRotation = new Quaternion(); + private Quaternion endRotation = new Quaternion(); private Spatial spatial; private String spatialName = ""; private float value = 0; @@ -46,29 +46,29 @@ public class RotationTrack extends AbstractCinematicEvent { public RotationTrack() { } - public RotationTrack(Spatial spatial, float[] endRotation) { - this.endRotation = endRotation; + public RotationTrack(Spatial spatial, Quaternion endRotation) { + this.endRotation.set(endRotation); this.spatial = spatial; spatialName = spatial.getName(); } - public RotationTrack(Spatial spatial, float[] endRotation, float initialDuration, LoopMode loopMode) { + public RotationTrack(Spatial spatial, Quaternion endRotation, float initialDuration, LoopMode loopMode) { super(initialDuration, loopMode); - this.endRotation = endRotation; + this.endRotation.set(endRotation); this.spatial = spatial; spatialName = spatial.getName(); } - public RotationTrack(Spatial spatial, float[] endRotation, LoopMode loopMode) { + public RotationTrack(Spatial spatial, Quaternion endRotation, LoopMode loopMode) { super(loopMode); - this.endRotation = endRotation; + this.endRotation.set(endRotation); this.spatial = spatial; spatialName = spatial.getName(); } - public RotationTrack(Spatial spatial, float[] endRotation, float initialDuration) { + public RotationTrack(Spatial spatial, Quaternion endRotation, float initialDuration) { super(initialDuration); - this.endRotation = endRotation; + this.endRotation.set(endRotation); this.spatial = spatial; spatialName = spatial.getName(); } @@ -76,10 +76,10 @@ public class RotationTrack extends AbstractCinematicEvent { @Override public void onPlay() { if (playState != playState.Paused) { - startRotation = spatial.getWorldRotation().toAngles(null); + startRotation.set(spatial.getWorldRotation()); } if (duration == 0 && spatial != null) { - spatial.setLocalRotation(new Quaternion().fromAngles(endRotation)); + spatial.setLocalRotation(endRotation); stop(); } } @@ -88,11 +88,12 @@ public class RotationTrack extends AbstractCinematicEvent { public void onUpdate(float tpf) { if (spatial != null) { value += Math.min(tpf * speed / duration, 1.0f); - float[] rot = new float[3]; - rot[0] = FastMath.interpolateLinear(value, startRotation[0], endRotation[0]); - rot[1] = FastMath.interpolateLinear(value, startRotation[1], endRotation[1]); - rot[2] = FastMath.interpolateLinear(value, startRotation[2], endRotation[2]); - spatial.setLocalRotation(new Quaternion().fromAngles(rot)); + TempVars vars = TempVars.get(); + Quaternion q = vars.quat1; + q.set(startRotation).slerp(endRotation, value); + + spatial.setLocalRotation(q); + vars.release(); } } @@ -118,6 +119,6 @@ public class RotationTrack extends AbstractCinematicEvent { super.read(im); InputCapsule ic = im.getCapsule(this); spatialName = ic.readString("spatialName", ""); - endRotation = ic.readFloatArray("endRotation", null); + endRotation = (Quaternion) ic.readSavable("endRotation", null); } } diff --git a/engine/src/test/jme3test/animation/TestCinematic.java b/engine/src/test/jme3test/animation/TestCinematic.java index efc33fd88..3b14d6e0e 100644 --- a/engine/src/test/jme3test/animation/TestCinematic.java +++ b/engine/src/test/jme3test/animation/TestCinematic.java @@ -53,6 +53,7 @@ import com.jme3.light.DirectionalLight; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.FastMath; +import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; import com.jme3.post.FilterPostProcessor; import com.jme3.post.filters.FadeFilter; @@ -123,13 +124,14 @@ public class TestCinematic extends SimpleApplication { }); cinematic.addCinematicEvent(0, new PositionTrack(teapot, new Vector3f(10, 0, 10), 0)); cinematic.addCinematicEvent(0, new ScaleTrack(teapot, new Vector3f(1, 1, 1), 0)); - float[] rotation = {0, 0, 0}; - cinematic.addCinematicEvent(0, new RotationTrack(teapot, rotation, 0)); + Quaternion q = new Quaternion(); + q.loadIdentity(); + cinematic.addCinematicEvent(0, new RotationTrack(teapot, q, 0)); cinematic.addCinematicEvent(0, new PositionTrack(teapot, new Vector3f(10, 0, -10), 20)); cinematic.addCinematicEvent(0, new ScaleTrack(teapot, new Vector3f(4, 4, 4), 10)); cinematic.addCinematicEvent(10, new ScaleTrack(teapot, new Vector3f(1, 1, 1), 10)); - float[] rotation2 = {0, FastMath.TWO_PI, 0}; + Quaternion rotation2 = new Quaternion().fromAngleAxis(FastMath.PI, Vector3f.UNIT_Y); cinematic.addCinematicEvent(0, new RotationTrack(teapot, rotation2, 20)); cinematic.activateCamera(0, "aroundCam");