-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVector2.java
More file actions
161 lines (140 loc) · 4.47 KB
/
Copy pathVector2.java
File metadata and controls
161 lines (140 loc) · 4.47 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package javagameengine.msc;
import testing.spel1.Main;
import java.util.Objects;
public class Vector2 {
public static Vector2 up = new Vector2(0,-1);
public static Vector2 down = new Vector2(0,1);
public static Vector2 right = new Vector2(1,0);
public static Vector2 left = new Vector2(-1,0);
public static Vector2 zero = new Vector2(0,0);
private float x,y;
public Vector2(float x, float y) {
this.x = x;
this.y = y;
}
public Vector2(Vector2 vector2) {
this.x = vector2.x;
this.y = vector2.y;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public String toString()
{
return ("{x:{"+x+"} y:{"+y+"}}");
}
public Vector2 multiply(float multiple) {
return new Vector2(x*multiple,y*multiple);
}
public Vector2 multiply(double multiple) {
return new Vector2((float) (x*multiple), (float) (y*multiple));
}
public Vector2 multiply(Vector2 vector2) {
return new Vector2(x*vector2.x,y*vector2.y);
}
public float getMagnitude (){
return (float) Math.sqrt((double) this.x * (double) this.x + (double) this.y * (double) this.y);
}
public Vector2 getNegative(){
return new Vector2(y,x);
}
public Vector2 divide(float a){
return new Vector2(x/a,y/a);
}
public Vector2 divide(Vector2 a){
return new Vector2(x/a.getX(),y/a.getY());
}
public Vector2 add(Vector2 vector2) {
return new Vector2(x+ vector2.x,y+ vector2.y);
}
public Vector2 add(float a) {
return new Vector2(x+ a,y+ a);
}
public Vector2 subtract(Vector2 vector2) {
return new Vector2(x - vector2.x,y - vector2.y);
}
public Vector2 subtract(float value) {
return new Vector2( x- value,y - value);
}
public Vector2 getNormalized()
{
if(getHighest()!=0)
return divide(getHighest());
return Vector2.zero;
}
private float getHighest() {
return Math.max(Math.abs(x), Math.abs(y));
}
public double getDistance(Vector2 vector2)
{
return Math.sqrt(Math.pow(Math.abs(vector2.getY()-y),2)+Math.pow(Math.abs(vector2.getX()-x),2));
}
public float getAngle()
{
//A = atan2(V.y, V.x)
return (float)(Math.toDegrees(Math.atan2(-y,-x)));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vector2 vector2 = (Vector2) o;
return Float.compare(vector2.x, x) == 0 && Float.compare(vector2.y, y) == 0;
}
public static Vector2 getDirection(double angle) {
float x = (float) Math.cos(Math.toRadians(angle));
float y = (float) Math.sin(Math.toRadians(angle));
return new Vector2(x,y);
}
/**
Returns the angle between object position and vector given
@param toLookAt the vector to look at
@return the degrees from this to the toLookAt vector
**/
public float lookAt(Vector2 toLookAt)
{
float angle;
if((toLookAt.getX()>getX())){
float b = getX()-toLookAt.getX();
float a = getY()-toLookAt.getY();
//a/b=tan v
//System.out.println("a; "+a+"b: "+b);
angle = (float) Math.toDegrees(Math.atan(a/b));
}else{
float b = getX()-toLookAt.getX();
float a = getY()-toLookAt.getY();
//a/b=tan v
//System.out.println("a; "+a+"b: "+b);
angle = 180+(float) Math.toDegrees(Math.atan(a/b));
}
return angle;
}
public static double angleFromOriginCounterClockwise(Vector2 a) {
double degrees = Math.toDegrees(Math.atan(a.getY()/a.getX()));
if(a.getX() < 0.0) return degrees+180.0;
else if(a.getY() < 0.0) return degrees+360.0;
else return degrees;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
public Vector2 removeY(){
return new Vector2(x,0);
}
public Vector2 removeX(){
return new Vector2(0,y);
}
public Vector2 divide(int i) {
return this.divide(new Vector2(i,i));
}
}