X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fmath%2FOrientation.java;h=f4b285134fa392883ba0b8ab335dc8ed397e6754;hb=HEAD;hp=714b781ffc780eb0b36a748f68fd3b7372e9f6a5;hpb=316a696bf9db6e8eddf90ef3df5e1119481c0192;p=sixth-3d.git diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/math/Orientation.java b/src/main/java/eu/svjatoslav/sixth/e3d/math/Orientation.java index 714b781..f4b2851 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/math/Orientation.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/math/Orientation.java @@ -1,22 +1,48 @@ /* - * Sixth 3D engine. Author: Svjatoslav Agejenko. + * Sixth 3D engine. Author: Svjatoslav Agejenko. * This project is released under Creative Commons Zero (CC0) license. */ package eu.svjatoslav.sixth.e3d.math; import eu.svjatoslav.sixth.e3d.geometry.Point3D; +import static java.lang.Math.cos; +import static java.lang.Math.sin; + +/** + * Used to represent orientation in a 3D space. + * + * Orientations are represented as two angles of rotation around the XZ and YZ axes. + * The angles are stored as sines and cosines to avoid unnecessary trigonometric calculations. + * + * Orientations are used for rotating object coordinates in a 3D space. + */ public class Orientation implements Cloneable { + /** + * The sine and cosine of the angles. + */ private double s1, c1, s2, c2; + /** + * The angle of rotation around the XZ axis. + */ private double angleXZ = 0; + + /** + * The angle of rotation around the YZ axis. + */ private double angleYZ = 0; public Orientation() { computeMultipliers(); } + /** + * Creates a new orientation with the specified angles. + * @param angleXZ The angle of rotation around the XZ axis. + * @param angleYZ The angle of rotation around the YZ axis. + */ public Orientation(final double angleXZ, final double angleYZ) { this.angleXZ = angleXZ; this.angleYZ = angleYZ; @@ -28,22 +54,34 @@ public class Orientation implements Cloneable { return new Orientation(angleXZ, angleYZ); } + /** + * Computes the sine and cosine of the angles. + */ private void computeMultipliers() { - s1 = Math.sin(angleXZ); - c1 = Math.cos(angleXZ); + s1 = sin(angleXZ); + c1 = cos(angleXZ); - s2 = Math.sin(angleYZ); - c2 = Math.cos(angleYZ); + s2 = sin(angleYZ); + c2 = cos(angleYZ); } + /** + * Rotates the specified point around the XZ and YZ axes relative to the origin. + * @param point3d The point to rotate. + */ public void rotate(final Point3D point3d) { + // Rotate around the XZ axis. final double z1 = (point3d.z * c1) - (point3d.x * s1); point3d.x = (point3d.z * s1) + (point3d.x * c1); + // Rotate around the YZ axis. point3d.z = (z1 * c2) - (point3d.y * s2); point3d.y = (z1 * s2) + (point3d.y * c2); } + /** + * Rotates current orientation around the XZ and YZ axes. + */ public void rotate(final double angleXZ, final double angleYZ) { this.angleXZ += angleXZ; this.angleYZ += angleYZ;