-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.js
More file actions
136 lines (125 loc) · 3.38 KB
/
Vector.js
File metadata and controls
136 lines (125 loc) · 3.38 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
"use strict"
class Vector {
constructor(x = 0, y = 0) {
this.x = x
this.y = y
}
magnitude() {
return Math.sqrt(this.x * this.x + this.y * this.y)
}
angle() {
return (Math.atan(this.y / this.x) + (this.x < 0 ? Math.PI :
(this.y < 0 ? 2 * Math.PI : 0)))
}
clone() {
return new Vector(this.x, this.y)
}
normalize() {
if (this.x == 0 && this.y == 0) {
return new Vector()
}
var normal = this.clone()
var magnitude = this.magnitude()
normal.x /= magnitude
normal.y /= magnitude
return normal
}
/*
rotateAbout(ful, angle) {
var t = this.subtract(ful)
var c = Math.cos(angle)
var s = Math.sin(angle)
var r = new Vector(t.x * c - t.y * s,
t.x * s + t.y * c)
return r.add(ful)
}
*/
dot(v2) {
return this.x * v2.x + this.y * v2.y
}
add(v2) {
return new Vector(this.x + v2.x, this.y + v2.y)
}
subtract(v2) {
return new Vector(this.x - v2.x, this.y - v2.y)
}
perpendicular() {
return new Vector(-this.y, this.x)
}
}
function calculateDistance(v1, v2) {
return Math.sqrt(
Math.pow(v1.x - v2.x ,2) +
Math.pow(v1.y - v2.y, 2)
)
}
function collisionTest(lhs, rhs) {
var lhshb = hitBoxes[lhs.hitboxType]
var rhshb = hitBoxes[rhs.hitboxType]
var lhsleft = lhs.position.x
var lhstop = lhs.position.y
var lhsright = lhs.left + lhshb.width - 1
var lhsbottom = lhstop + lhshb.height - 1
var rhsleft = rhs.position.x
var rhstop = rhs.position.y
var rhsright = rhs.left + rhshb.width - 1
var rhsbottom = rhstop + rhshb.height - 1
if (rhsleft > lhsright
|| rhsright < lhsleft
|| rhstop > lhsbottom
|| rhsbottom < lhstop) {
return false
}
var lhsVertices = lhs.getVertices()
var rhsVertices = rhs.getVertices()
var lhsAxes = getAxes(lhsVertices)
var rhsAxes = getAxes(rhsVertices)
for (let i = 0; i < lhsAxes.length; i++) {
var lhsPro = project(lhsVertices, lhsAxes[i])
var rhsPro = project(rhsVertices, lhsAxes[i])
if (!lhsPro.overlap(rhsPro)) {
return false
}
}
for (let i = 0; i < rhsAxes.length; i++) {
var lhsPro = project(lhsVertices, rhsAxes[i])
var rhsPro = project(rhsVertices, rhsAxes[i])
if (!lhsPro.overlap(rhsPro)) {
return false
}
}
return true
}
function getAxes(vertices) {
var axes = []
for (let i = 0; i < vertices.length; i++) {
var v1 = vertices[i]
var v2 = vertices[(i + 1) % vertices.length]
var edge = v1.subtract(v2)
axes[i] = edge.perpendicular()
}
return axes
}
function project(vertices, axis) {
var normalized = axis.normalize()
var min = normalized.dot(vertices[0])
var max = min
for (let i = 1; i < vertices.length; i++) {
var p = normalized.dot(vertices[i])
if (p < min) {
min = p
} else if (p > max) {
max = p
}
}
return new Projection(min, max)
}
class Projection {
constructor(min, max) {
this.min = min
this.max = max
}
overlap(p2) { // returns true if touching or overlapping
return !(this.min > p2.max || p2.min > this.max)
}
}