Updated readability of the code.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Fri, 3 Mar 2023 20:43:29 +0000 (22:43 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Fri, 3 Mar 2023 20:43:29 +0000 (22:43 +0200)
src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point3D.java
src/main/java/eu/svjatoslav/sixth/e3d/renderer/octree/raytracer/CameraView.java

index 78efbda..8d72345 100755 (executable)
@@ -172,6 +172,10 @@ public class Point3D implements Cloneable {
         return this;
     }
 
+    public Point3D rotate(final double angleXZ, final double angleYZ) {
+        return rotate(new Point3D(0, 0, 0), angleXZ, angleYZ);
+    }
+
     /**
      * Round current point coordinates to integer values.
      */
index ed0b037..b06f0c3 100644 (file)
@@ -7,6 +7,8 @@ package eu.svjatoslav.sixth.e3d.renderer.octree.raytracer;
 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
 import eu.svjatoslav.sixth.e3d.gui.Avatar;
 
+import static eu.svjatoslav.sixth.e3d.renderer.octree.raytracer.Camera.SIZE;
+
 public class CameraView {
 
     Point3D cameraCenter;
@@ -20,25 +22,25 @@ public class CameraView {
     }
 
     private void computeCameraCoordinates(final Avatar avatar, final double zoom) {
-        cameraCenter = new Point3D(avatar.getLocation()).scaleDown(zoom);
-
-        topLeft = new Point3D(cameraCenter.x, cameraCenter.y, cameraCenter.z
-                + Camera.SIZE);
-        topRight = new Point3D(topLeft);
-        bottomLeft = new Point3D(topLeft);
-        bottomRight = new Point3D(topLeft);
 
         final float viewAngle = (float) .6;
+        topLeft = new Point3D(0,0, SIZE).rotate(-viewAngle, -viewAngle);
+        topRight = new Point3D(0,0, SIZE).rotate(viewAngle, -viewAngle);
+        bottomLeft = new Point3D(0,0,SIZE).rotate(-viewAngle, viewAngle);
+        bottomRight = new Point3D(0,0,SIZE).rotate(viewAngle, viewAngle);
 
-        topLeft.rotate(cameraCenter, -viewAngle, -viewAngle);
-        topRight.rotate(cameraCenter, viewAngle, -viewAngle);
-        bottomLeft.rotate(cameraCenter, -viewAngle, viewAngle);
-        bottomRight.rotate(cameraCenter, viewAngle, viewAngle);
+        topLeft.rotate(-avatar.getAngleXZ(), -avatar.getAngleYZ());
+        topRight.rotate(-avatar.getAngleXZ(), -avatar.getAngleYZ());
+        bottomLeft.rotate(-avatar.getAngleXZ(), -avatar.getAngleYZ());
+        bottomRight.rotate(-avatar.getAngleXZ(), -avatar.getAngleYZ());
+
+        // compute camera coordinates as if camera is at avatar's location and look
+        cameraCenter = new Point3D(avatar.getLocation()).scaleDown(zoom);
+        topLeft.add(cameraCenter);
+        topRight.add(cameraCenter);
+        bottomLeft.add(cameraCenter);
+        bottomRight.add(cameraCenter);
 
-        topLeft.rotate(cameraCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
-        topRight.rotate(cameraCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
-        bottomLeft.rotate(cameraCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
-        bottomRight.rotate(cameraCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
     }
 
 }