-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPhysics.js
107 lines (86 loc) · 3.73 KB
/
Physics.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
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
// Just checks for collision between two objects
function collides(obj1, obj2) {
if (obj1.position.x < obj2.position.x + obj2.size.x &&
obj1.position.x + obj1.size.x > obj2.position.x &&
obj1.position.y < obj2.position.y + obj2.size.y &&
obj1.size.y + obj1.position.y > obj2.position.y) {
return true;
} else {
return false;
}
}
function phys(obj1, obj2, deltaTime) {
// obj1 is the moving object
// Save the position before moving it
var oldPos = obj1.position;
// The amount to move this frame
var vel = obj1.velocity.mul(deltaTime);
// Move the objects
obj1.position = obj1.position.add(vel);
var t1, t2;
// Check for collision this frame
if (collides(obj1, obj2)) {
if (oldPos.y >= obj2.position.y + obj2.size.y) {
// Check for intersection on lower horiz line
var bottom = obj2.position.y + obj2.size.y; // Bottom of obstacle
var dir = obj1.velocity.normalize(); // Direction of the movement
// The distance from oldPos to the collision point
var t = (bottom - oldPos.y) / dir.y;
// Collision point
var hit = oldPos.add(dir.mul(t));
// Make sure the collision happens on the x axis
if (hit.x > obj2.position.x - obj1.size.x &&
hit.x < obj2.position.x + obj2.size.x) {
if (t >= 0) t1 = t;
}
} else if (oldPos.y + obj1.size.y <= obj2.position.y) {
//if(obj1 == player) console.log("down");
// Check for intersection on upper horiz line
var top = obj2.position.y; // top of obstacle
var dir = obj1.velocity.normalize(); // Direction of the movement
// The distance from oldPos to the collision point
var t = (top - (oldPos.y + obj1.size.y)) / dir.y;
// Collision point
var hit = oldPos.add(dir.mul(t));
// Make sure the collision happens on the x axis
if (hit.x > obj2.position.x - obj1.size.x &&
hit.x < obj2.position.x + obj2.size.x) {
if (t >= 0) t1 = t;
}
}
if (oldPos.x >= obj2.position.x + obj2.size.x) {
// Check for intersection on right vert line
var right = obj2.position.x + obj2.size.x; // Right side of obstacle
var dir = obj1.velocity.normalize(); // Direction of the movement
// The distance from oldPos to the collision point
var t = (right - oldPos.x) / dir.x;
// Collision point
var hit = oldPos.add(dir.mul(t));
// Make sure the collision happens on the y axis
if (hit.y > obj2.position.y - obj1.size.y &&
hit.y < obj2.position.y + obj2.size.y) {
if (t >= 0) t2 = t;
}
} else if (oldPos.x + obj1.size.x <= obj2.position.x) {
// Check for intersection on left vert line
var left = obj2.position.x; // left side of obstacle
var dir = obj1.velocity.normalize(); // Direction of the movement
// The distance from oldPos to the collision point
var t = (left - (oldPos.x + obj1.size.x)) / dir.x;
// Collision point
var hit = oldPos.add(dir.mul(t));
// Make sure the collision happens on the y axis
if (hit.y > obj2.position.y - obj1.size.y &&
hit.y < obj2.position.y + obj2.size.y) {
if (t >= 0) t2 = t;
}
}
}
obj1.position = oldPos;
if((t1 < t2 || t2 == undefined) && t1 != undefined) {
return {t: t1, ax: true};
} else if (t2 != undefined) {
return {t: t2, ax: false};
}
return {t: -1, ax: false};
}