-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSolidBody.js
72 lines (63 loc) · 2.62 KB
/
SolidBody.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
var SolidBody = (function () {
function SolidBody (data) {
this.x = data.x || 0;
this.y = data.y || 0;
this.width = data.width || 0;
this.height = data.height || 0;
this.surface = null;
};
SolidBody.prototype = new BasicObject();
SolidBody.prototype.getCollisionReaction = function (collided) {
var reaction = {
x: undefined,
y: undefined,
vx: undefined,
vy: undefined
};
var prevBottom = undefined;
var prevRight = undefined;
var elastic = false;
var vxSign = collided.direction != undefined ? collided.direction : collided.vx;
if(collided instanceof Box) {
elastic = true;
}
if(collided.prevy != undefined) {
prevBottom = collided.getPrevBottomY();
}
if(collided.prevx != undefined) {
prevRight = collided.getPrevRightX();
}
if(prevBottom <= this.y && collided.vy >= 0) {
reaction.y = this.y - collided.height;
reaction.vy = elastic ? collided.vy * -1 : 0;
} else if (collided.prevy >= this.getBottomY() && collided.vy <= 0) {
reaction.y = this.y + this.height;
reaction.vy = elastic ? collided.vy * -1 : 0;
} else if ( (collided.prevx >= this.getRightX() && vxSign <= 0) || (collided.getRightX() > this.getRightX()) ) {
reaction.x = this.x + this.width;
reaction.vx = elastic ? collided.vx * -1 : 0;
} else if ( (prevRight <= this.x && vxSign >= 0) || collided.x < this.x) {
reaction.x = this.x - collided.width;
reaction.vx = elastic ? collided.vx * -1 : 0;
}
// if(collided.y < this.y && prevBottom <= this.y) {
// reaction.y = this.y - collided.height;
// reaction.vy = elastic ? collided.vy * -1 : 0;
// } else if(collided.y + collided.height > this.y + this.height && collided.prevy >= this.y + this.height) {
// reaction.y = this.y + this.height + 1;
// reaction.vy = elastic ? collided.vy * -1 : 0;
// } else if(collided.x < this.x) {
// var newx = this.x - collided.width - 1;
// reaction.x = newx;
// reaction.vx = elastic ? collided.vx * -1 : 0;
// // collided.leftDirection();
// } else {
// var newx = this.x + this.width + 1;
// reaction.x = newx;
// reaction.vx = elastic ? collided.vx * -1 : 0;
// // collided.rightDirection();
// }
return reaction;
};
return SolidBody;
})();