Anonymous avatar Anonymous committed 742b558

minor optimizations to rotation matrix use

Comments (0)

Files changed (1)

src/main/java/sandbox/common/ActorFrame.java

 import static org.newdawn.slick.util.FastTrig.sin;
 
 
+/** A quaternion-based camera (that uses a rotation matrix way too much, because I still don't get
+ * quaternion multiplication as much as I should.
+ */
 public class ActorFrame {
     public final Vector3f position = new Vector3f();
     public final Quat4f orientation = new Quat4f(0, 0, 0, 1);
-    private Matrix4f rotationMatrix;
 
+    private final Matrix4f rot = new Matrix4f();
+    
     public void setPosition(float x, float y, float z) {
         position.set(x, y, z);
     }
 
-    private Matrix4f getRotationMatrix() {
-        rotationMatrix = new Matrix4f();
-        rotationMatrix.set(orientation);
-        return rotationMatrix;
-    }
-
     public void moveRight(float delta) {
-        Matrix4f m = getRotationMatrix();
-        Vector3f v = new Vector3f(m.m00, m.m01, m.m02);
+        Vector3f v = new Vector3f(rot.m00, rot.m01, rot.m02);
         v.scale(delta);
         position.add(v);
     }
 
     public void moveUp(float delta) {
-        Matrix4f m = getRotationMatrix();
-        Vector3f v = new Vector3f(m.m10, m.m11, m.m12);
+        Vector3f v = new Vector3f(rot.m10, rot.m11, rot.m12);
         v.scale(delta);
         position.add(v);
     }
 
     public void moveForward(float delta) {
-        Matrix4f m = getRotationMatrix();
-        Vector3f v = new Vector3f(m.m20, m.m21, m.m22);
+        Vector3f v = new Vector3f(rot.m20, rot.m21, rot.m22);
         // "forward" is actually -z
         v.scale(-delta);
         position.add(v);
     public void rotate(float pitch, float heading, float roll) {
         // Note the different order of args below.
         Matrix4f m = fromHeadingPitchRoll(heading, pitch, roll);
+
         Quat4f q = new Quat4f();
         q.set(m);
         orientation.mul(q, orientation);
-        rotationMatrix = null;
+        rot.set(orientation);
+    }
+
+    private Matrix4f getRotationMatrix() {
+        Matrix4f m = new Matrix4f();
+        m.set(orientation);
+        return m;
     }
 
     public Matrix4f getActorTransform() {
         return m;
     }
 
-    public static Matrix4f fromHeadingPitchRoll(float heading, float pitch, float roll) {
+    private static Matrix4f fromHeadingPitchRoll(float heading, float pitch, float roll) {
         heading = (float) toRadians(heading);
         pitch = (float) toRadians(pitch);
         roll = (float) toRadians(roll);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.