-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClosestItem.java
More file actions
78 lines (65 loc) · 1.88 KB
/
Copy pathClosestItem.java
File metadata and controls
78 lines (65 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
George Zhang
Class to help find closest intersecting cube.
*/
package geetransit.minecraft05.engine;
import java.util.List;
import org.joml.Vector2f;
import org.joml.Vector3f;
import org.joml.Intersectionf;
public class ClosestItem {
public float distance; // distance from camera
public Item closest;
public Vector3f hit; // position of intersection
public Vector3f direction; // points away from camera
private final Vector3f max;
private final Vector3f min;
private final Vector2f nearFar;
public ClosestItem() {
this.hit = new Vector3f();
this.direction = new Vector3f();
this.min = new Vector3f();
this.max = new Vector3f();
this.nearFar = new Vector2f();
}
public ClosestItem(List<Item> items, Camera camera) {
this();
this.update(items, camera);
}
public ClosestItem reset() {
this.distance = Float.POSITIVE_INFINITY;
this.closest = null;
return this;
}
public ClosestItem update(List<Item> items, Camera camera) {
this.reset().extend(items, camera);
return this;
}
public ClosestItem extend(List<Item> items, Camera camera) {
// get camera direction
camera.getViewMatrix().positiveZ(this.direction);
this.direction.negate().normalize();
// loop through all items
for (Item item : items) {
this.min.set(item.getPosition());
this.max.set(item.getPosition());
this.min.add(-item.getScale(), -item.getScale(), -item.getScale());
this.max.add(item.getScale(), item.getScale(), item.getScale());
// check if intersects and is closer
if (Intersectionf.intersectRayAab(
camera.getPosition(), this.direction,
this.min, this.max, this.nearFar
)) {
if (this.nearFar.x < this.distance) {
this.distance = nearFar.x;
this.closest = item;
this.hit.set(this.direction);
this.hit.mul(this.distance);
this.hit.add(camera.getPosition());
}
}
}
// allow method chaining
return this;
}
}