2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.gui;
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.math.TransformsStack;
9 import eu.svjatoslav.sixth.e3d.math.Vertex;
12 * Tracks the position of the user in the 3D space.
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.
18 public class UserRelativityTracker {
20 private final static int minimalSliceFactor = 5;
23 * Points to 0, 0, 0 in object own relative space.
25 public Vertex center = new Vertex();
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.
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.
39 public UserRelativityTracker() {
43 public void analyze(final TransformsStack transformPipe,
44 final RenderingContext renderingContext) {
46 center.calculateLocationRelativeToViewer(transformPipe, renderingContext);
48 if (right != null) { // If orientation tracking is enabled.
49 right.calculateLocationRelativeToViewer(transformPipe, renderingContext);
50 down.calculateLocationRelativeToViewer(transformPipe, renderingContext);
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.
59 public void enableOrientationTracking() {
60 right = new Vertex(new Point3D(10, 0, 0));
61 down = new Vertex(new Point3D(0, 10, 0));
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.
68 public double getAngleXY() {
69 return center.transformedCoordinate
70 .getAngleXY(down.transformedCoordinate);
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.
77 public double getAngleXZ() {
78 return center.transformedCoordinate
79 .getAngleXZ(right.transformedCoordinate);
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.
86 public double getAngleYZ() {
87 return center.transformedCoordinate
88 .getAngleYZ(down.transformedCoordinate);
92 * Calculates the distance between the user and the object.
93 * Distance to the user can be used to determine object detail level.
95 public double getDistanceToUser() {
96 return center.transformedCoordinate.getVectorLength();
99 public double proposeSliceFactor() {
100 final double distanceToCamera = getDistanceToUser();
102 double proposedSliceFactor = distanceToCamera / 5;
104 if (proposedSliceFactor < minimalSliceFactor)
105 proposedSliceFactor = minimalSliceFactor;
107 return proposedSliceFactor;