Fixed git clone URL
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / UserRelativityTracker.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.gui;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.math.TransformsStack;
9 import eu.svjatoslav.sixth.e3d.math.Vertex;
10
11 /**
12  * Tracks the position of the user in the 3D space.
13  * <p>
14  * It can be used to determine the angle between the user and the object.
15  * Also, it can be used to determine the distance between the user and the object.
16  */
17
18 public class UserRelativityTracker {
19
20     private final static int minimalSliceFactor = 5;
21
22     /**
23      * Points to 0, 0, 0 in object own relative space.
24      */
25     public Vertex center = new Vertex();
26
27     /**
28      * Points to 10, 0, 0 in object own relative space if orientation tracking is enabled.
29      * It is used to determine the angle between the user and the object.
30      */
31     public Vertex right;
32
33     /**
34      * Points to 0, 10, 0 in object own relative space if orientation tracking is enabled.
35      * It is used to determine the angle between the user and the object.
36      */
37     public Vertex down;
38
39     public UserRelativityTracker() {
40
41     }
42
43     public void analyze(final TransformsStack transformPipe,
44                         final RenderingContext renderingContext) {
45
46         center.calculateLocationRelativeToViewer(transformPipe, renderingContext);
47
48         if (right != null) { // If orientation tracking is enabled.
49             right.calculateLocationRelativeToViewer(transformPipe, renderingContext);
50             down.calculateLocationRelativeToViewer(transformPipe, renderingContext);
51         }
52     }
53
54     /**
55      * Initializes the orientation tracking.
56      * Orientation tracking is used to determine the angle between the user and the object.
57      * Orientation tracking is disabled by default and it is optional.
58      */
59     public void enableOrientationTracking() {
60         right = new Vertex(new Point3D(10, 0, 0));
61         down = new Vertex(new Point3D(0, 10, 0));
62     }
63
64     /**
65      * Calculates the angle between the user and the object in the XY plane.
66      * @return the angle between the user and the object in the XY plane.
67      */
68     public double getAngleXY() {
69         return center.transformedCoordinate
70                 .getAngleXY(down.transformedCoordinate);
71     }
72
73     /**
74      * Calculates the angle between the user and the object in the XZ plane.
75      * @return the angle between the user and the object in the XZ plane.
76      */
77     public double getAngleXZ() {
78         return center.transformedCoordinate
79                 .getAngleXZ(right.transformedCoordinate);
80     }
81
82     /**
83      * Calculates the angle between the user and the object in the YZ plane.
84      * @return the angle between the user and the object in the YZ plane.
85      */
86     public double getAngleYZ() {
87         return center.transformedCoordinate
88                 .getAngleYZ(down.transformedCoordinate);
89     }
90
91     /**
92      * Calculates the distance between the user and the object.
93      * Distance to the user can be used to determine object detail level.
94      */
95     public double getDistanceToUser() {
96         return center.transformedCoordinate.getVectorLength();
97     }
98
99     public double proposeSliceFactor() {
100         final double distanceToCamera = getDistanceToUser();
101
102         double proposedSliceFactor = distanceToCamera / 5;
103
104         if (proposedSliceFactor < minimalSliceFactor)
105             proposedSliceFactor = minimalSliceFactor;
106
107         return proposedSliceFactor;
108     }
109
110 }