-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector2.js
55 lines (39 loc) · 798 Bytes
/
vector2.js
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
var Vector2 = function() {
this.x = 0;
this.y = 0;
}
Vector2.prototype.copy = function() {
var newVector = new Vector2();
newVector.x = this.x;
newVector.y = this.y;
return newVector;
}
Vector2.prototype.set = function(x, y) {
this.x = x;
this.y = y;
}
Vector2.prototype.zero = function() {
this.x = 0;
this.y = 0;
}
Vector2.prototype.normalize = function() {
var magnitude = (this.x * this.x) + (this.y * this.y);
if (magnitude != 0)
{
var oneOverMag = 1 / Math.sqrt(magnitude);
this.x *= oneOverMag;
this.y *= oneOverMag;
}
}
Vector2.prototype.add = function(v2) {
this.x += v2.x;
this.y += v2.y;
}
Vector2.prototype.subtract = function(v2) {
this.x -= v2.x;
this.y -= v2.y;
}
Vector2.prototype.multiplyScalar = function(f) {
this.x *= f;
this.y *= f;
}