forked from RLBot/RLBotJavaExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3.java
More file actions
107 lines (86 loc) · 2.94 KB
/
Vector3.java
File metadata and controls
107 lines (86 loc) · 2.94 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package rlbotexample.vector;
import com.google.flatbuffers.FlatBufferBuilder;
/**
* A simple 3d vector class with the most essential operations.
*
* This class is here for your convenience, it is NOT part of the framework. You can add to it as much
* as you want, or delete it.
*/
public class Vector3 extends rlbot.vector.Vector3 {
public Vector3(double x, double y, double z) {
super((float) x, (float) y, (float) z);
}
public Vector3() {
this(0, 0, 0);
}
public Vector3(rlbot.flat.Vector3 vec) {
// Invert the X value so that the axes make more sense.
this(-vec.x(), vec.y(), vec.z());
}
public int toFlatbuffer(FlatBufferBuilder builder) {
// Invert the X value again so that rlbot sees the format it expects.
return rlbot.flat.Vector3.createVector3(builder, -x, y, z);
}
public Vector3 plus(Vector3 other) {
return new Vector3(x + other.x, y + other.y, z + other.z);
}
public Vector3 minus(Vector3 other) {
return new Vector3(x - other.x, y - other.y, z - other.z);
}
public Vector3 scaled(double scale) {
return new Vector3(x * scale, y * scale, z * scale);
}
/**
* If magnitude is negative, we will return a vector facing the opposite direction.
*/
public Vector3 scaledToMagnitude(double magnitude) {
if (isZero()) {
throw new IllegalStateException("Cannot scale up a vector with length zero!");
}
double scaleRequired = magnitude / magnitude();
return scaled(scaleRequired);
}
public double distance(Vector3 other) {
double xDiff = x - other.x;
double yDiff = y - other.y;
double zDiff = z - other.z;
return Math.sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff);
}
public double magnitude() {
return Math.sqrt(magnitudeSquared());
}
public double magnitudeSquared() {
return x * x + y * y + z * z;
}
public Vector3 normalized() {
if (isZero()) {
throw new IllegalStateException("Cannot normalize a vector with length zero!");
}
return this.scaled(1 / magnitude());
}
public double dotProduct(Vector3 other) {
return x * other.x + y * other.y + z * other.z;
}
public boolean isZero() {
return x == 0 && y == 0 && z == 0;
}
public Vector2 flatten() {
return new Vector2(x, y);
}
public double angle(Vector3 v) {
double mag2 = magnitudeSquared();
double vmag2 = v.magnitudeSquared();
double dot = dotProduct(v);
return Math.acos(dot / Math.sqrt(mag2 * vmag2));
}
public Vector3 crossProduct(Vector3 v) {
double tx = y * v.z - z * v.y;
double ty = z * v.x - x * v.z;
double tz = x * v.y - y * v.x;
return new Vector3(tx, ty, tz);
}
@Override
public String toString() {
return String.format("(%s, %s, %s)", x, y, z);
}
}