forked from RLBot/RLBotJavaExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2.java
More file actions
106 lines (87 loc) · 2.73 KB
/
Vector2.java
File metadata and controls
106 lines (87 loc) · 2.73 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
package rlbotexample.vector;
/**
* A vector that only knows about x and y components.
*
* 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 Vector2 {
public final double x;
public final double y;
public Vector2(double x, double y) {
this.x = x;
this.y = y;
}
public Vector2 plus(Vector2 other) {
return new Vector2(x + other.x, y + other.y);
}
public Vector2 minus(Vector2 other) {
return new Vector2(x - other.x, y - other.y);
}
public Vector2 scaled(double scale) {
return new Vector2(x * scale, y * scale);
}
/**
* If magnitude is negative, we will return a vector facing the opposite direction.
*/
public Vector2 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(Vector2 other) {
double xDiff = x - other.x;
double yDiff = y - other.y;
return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
}
/**
* This is the length of the vector.
*/
public double magnitude() {
return Math.sqrt(magnitudeSquared());
}
public double magnitudeSquared() {
return x * x + y * y;
}
public Vector2 normalized() {
if (isZero()) {
throw new IllegalStateException("Cannot normalize a vector with length zero!");
}
return this.scaled(1 / magnitude());
}
public double dotProduct(Vector2 other) {
return x * other.x + y * other.y;
}
public boolean isZero() {
return x == 0 && y == 0;
}
/**
* The correction angle is how many radians you need to rotate this vector to make it line up with the "ideal"
* vector. This is very useful for deciding which direction to steer.
*/
public double correctionAngle(Vector2 ideal) {
double currentRad = Math.atan2(y, x);
double idealRad = Math.atan2(ideal.y, ideal.x);
if (Math.abs(currentRad - idealRad) > Math.PI) {
if (currentRad < 0) {
currentRad += Math.PI * 2;
}
if (idealRad < 0) {
idealRad += Math.PI * 2;
}
}
return idealRad - currentRad;
}
/**
* Will always return a positive value <= Math.PI
*/
public static double angle(Vector2 a, Vector2 b) {
return Math.abs(a.correctionAngle(b));
}
@Override
public String toString() {
return String.format("(%s, %s)", x, y);
}
}