-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVector2.java
More file actions
125 lines (107 loc) · 3.25 KB
/
Copy pathVector2.java
File metadata and controls
125 lines (107 loc) · 3.25 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
package JavaGameEngine.msc;
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 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(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 devide(float a){
return new Vector2(x/a,y/a);
}
public Vector2 devide(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 getDirection(double angle) {
float x = (float) Math.cos(Math.toRadians(angle));
float y = (float) Math.sin(Math.toRadians(angle));
return new Vector2(x,y);
}
public Vector2 getNormalized()
{
return devide(getHighest());
}
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;
}
/**
Returns the angle between object position and vector given
@param toLookAt the vector to look at
**/
public double LookAt(Vector2 toLookAt)
{
float b = getX()-toLookAt.getX();
float a = getY()-toLookAt.getY();
//a/b=tan v
//System.out.println("a; "+a+"b: "+b);
return(Math.toDegrees(Math.atan(a/b)));
}
@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);
}
}