Improved code readability
[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     public Vertex center = new Vertex();
22     public Vertex right;
23     public Vertex down;
24
25     public UserRelativityTracker() {
26
27     }
28
29     public void analyze(final TransformsStack transformPipe,
30                         final RenderingContext renderingContext) {
31
32         center.calculateLocationRelativeToViewer(transformPipe, renderingContext);
33
34         if (right != null) {
35             right.calculateLocationRelativeToViewer(transformPipe, renderingContext);
36             down.calculateLocationRelativeToViewer(transformPipe, renderingContext);
37         }
38     }
39
40     public void enableOrientationTracking() {
41         right = new Vertex(new Point3D(10, 0, 0));
42         down = new Vertex(new Point3D(0, 10, 0));
43     }
44
45     public double getAngleXY() {
46         return center.transformedCoordinate
47                 .getAngleXY(down.transformedCoordinate);
48     }
49
50     public double getAngleXZ() {
51         return center.transformedCoordinate
52                 .getAngleXZ(right.transformedCoordinate);
53     }
54
55     public double getAngleYZ() {
56         return center.transformedCoordinate
57                 .getAngleYZ(down.transformedCoordinate);
58     }
59
60     public double getDistanceToUser() {
61         return center.transformedCoordinate.getVectorLength();
62     }
63
64     public double proposeSliceFactor() {
65         final double distanceToCamera = getDistanceToUser();
66
67         double proposedSliceFactor = distanceToCamera / 5;
68
69         if (proposedSliceFactor < minimalSliceFactor)
70             proposedSliceFactor = minimalSliceFactor;
71
72         return proposedSliceFactor;
73     }
74
75 }