-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.js
More file actions
92 lines (91 loc) · 1.46 KB
/
vector.js
File metadata and controls
92 lines (91 loc) · 1.46 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
var Vec = function(x,y,z)
{
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
Vec.prototype =
{
has_velocity: function()
{
return (this.dot() > 0.001);
},
quat: function()
{
return new Quat(this.x,this.y,this.z,0);
},
to_a: function() { return [this.x,this.y,this.z]; },
to_s: function() {
return [
Math.floor(this.x),
Math.floor(this.y),
Math.floor(this.z)
].join(',');
},
plus: function(p2)
{
return new Vec(
this.x + p2.x,
this.y + p2.y,
this.z + p2.z
);
},
minus: function(p2)
{
return new Vec(
this.x - p2.x,
this.y - p2.y,
this.z - p2.z
);
},
dot: function(q)
{
if(!q){ q = this; }
return this.x * q.x + this.y * q.y + this.z * q.z;
},
length: function()
{
return Math.sqrt(this.dot());
},
length2: function()
{
return this.dot();
},
multiply: function(v2)
{
return new Vec(
this.x * v2.x,
this.y * v2.y,
this.z * v2.z
);
},
multiply_scalar: function(x)
{
return new Vec(
this.x * x,
this.y * x,
this.z * x
);
},
divide_scalar: function(x)
{
return new Vec(
this.x / x,
this.y / x,
this.z / x
);
},
cross: function(v2)
{
return new Vec(
this.y*v2.z - this.z*v2.y,
this.z*v2.x - this.x*v2.z,
this.x*v2.y - this.y*v2.x
);
},
normalize: function()
{
var l = this.length();
return new Vec( this.x / l, this.y / l, this.z / l );
}
}