Fixed git clone URL
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / Avatar.java
index 6e4c5ec..f2d58c9 100755 (executable)
@@ -1,12 +1,7 @@
 /*
- * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- *
+ * Sixth 3D engine. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
  */
-
 package eu.svjatoslav.sixth.e3d.gui;
 
 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
@@ -14,13 +9,28 @@ import eu.svjatoslav.sixth.e3d.geometry.Point3D;
 import static java.lang.Math.cos;
 import static java.lang.Math.sin;
 
-public class Avatar implements ViewUpdateListener {
+/**
+ * The avatar is a user-controlled object in the 3D world.
+ *
+ * It is a point in space that can be moved around by the user.
+ * It has orientation in space, and it can also be rotated around.
+ */
+public class Avatar implements ViewRenderListener {
 
+    /**
+     * Avatar movement speed, relative to the world. When avatar coordinates are
+     * updated within the world, avatar orientation relative to the world is
+     * taken into account.
+     */
     public static final double SPEED_LIMIT = 30;
     /**
      * Just in case we want to adjust global speed for some reason.
      */
     private static final double SPEED_MULTIPLIER = .02d;
+    /**
+     * Determines amount of friction user experiences every millisecond while moving around in space.
+     */
+    private static final double MILLISECOND_FRICTION = 1.005;
     /**
      * Avatar movement speed, relative to avatar itself. When avatar coordinates
      * are updated within the world, avatar orientation relative to the world is
@@ -32,23 +42,16 @@ public class Avatar implements ViewUpdateListener {
      * Avatar location within the 3D world.
      */
     private Point3D location = new Point3D();
-
     /**
      * Avatar orientation on the X-Z plane. It changes when turning left or
      * right.
      */
     private double orientationXZ;
-
     /**
      * Avatar orientation on the Y-Z plane. It changes when looking up or down.
      */
     private double orientationYZ;
 
-    /**
-     * Determines amount of friction user experiences every millisecond while moving around in space.
-     */
-    private static final double MILLISECOND_FRICTION = 1.005;
-
     public Avatar() {
     }
 
@@ -69,11 +72,12 @@ public class Avatar implements ViewUpdateListener {
         setAngleYZ(angleYZ);
     }
 
+
     @Override
-    public boolean beforeViewUpdate(final ViewContext viewContext, final int millisecondsSinceLastFrame) {
+    public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) {
 
         final Point3D locationBeforeUpdate = new Point3D(location);
-        translateAvatarLocation(millisecondsSinceLastFrame);
+        translateAvatarLocationBasedOnMovementVector(millisecondsSinceLastFrame);
         applyFrictionToUserMovement(millisecondsSinceLastFrame);
         return isFrameRepaintNeeded(locationBeforeUpdate);
     }
@@ -84,8 +88,7 @@ public class Avatar implements ViewUpdateListener {
     }
 
     public void enforceSpeedLimit() {
-        final double currentSpeed = movementVector
-                .getDistanceTo(Point3D.ZERO);
+        final double currentSpeed = movementVector.getVectorLength();
 
         if (currentSpeed <= SPEED_LIMIT)
             return;
@@ -122,14 +125,24 @@ public class Avatar implements ViewUpdateListener {
     }
 
     public double getMovementSpeed() {
-        return movementVector.getDistanceTo(Point3D.ZERO);
+        return movementVector.getVectorLength();
     }
 
+    /**
+     * Apply friction to avatar movement vector.
+     *
+     * @param millisecondsPassedSinceLastFrame We want avatar movement to be independent of framerate.
+     *                                         Therefore, we take frame rendering time into account when translating
+     *                                         avatar between consecutive frames.
+     */
     private void applyFrictionToUserMovement(int millisecondsPassedSinceLastFrame) {
         for (int i = 0; i < millisecondsPassedSinceLastFrame; i++)
             applyMillisecondFrictionToUserMovementVector();
     }
 
+    /**
+     * Apply friction to avatar movement vector.
+     */
     private void applyMillisecondFrictionToUserMovementVector() {
         getMovementVector().x /= MILLISECOND_FRICTION;
         getMovementVector().y /= MILLISECOND_FRICTION;
@@ -140,10 +153,10 @@ public class Avatar implements ViewUpdateListener {
      * Translate coordinates based on avatar movement vector and avatar orientation in the world.
      *
      * @param millisecondsPassedSinceLastFrame We want avatar movement to be independent of framerate.
-     *                                         Therefore we take frame rendering time into account when translating
+     *                                         Therefore, we take frame rendering time into account when translating
      *                                         avatar between consecutive frames.
      */
-    private void translateAvatarLocation(int millisecondsPassedSinceLastFrame) {
+    private void translateAvatarLocationBasedOnMovementVector(int millisecondsPassedSinceLastFrame) {
         location.x -= (float) sin(getAngleXZ())
                 * getMovementVector().z * SPEED_MULTIPLIER
                 * millisecondsPassedSinceLastFrame;