forked from quidmonkey/Box2D-Collision-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collision_v1.js
78 lines (63 loc) · 2.34 KB
/
collision_v1.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
// Written by Abraham Walters
// Box2D Collision Plugin for Impact
// November 2011
ig.module(
'plugins.box2d.collision'
)
.requires(
'plugins.box2d.entity'
)
.defines(function () {
ig.Box2DEntity.inject({
init: function (x, y, settings) {
this.parent(x, y, settings);
this.body.entity = this;
this.shape = this.body.m_shapeList;
},
collision: function () {
this.standing = false;
//for each contact edge
for (var edge = this.body.m_contactList; edge; edge = edge.next) {
//grab normal force, which is the vector
//perpendicular to the surface of contact
var normal = edge.contact.m_manifold.normal;
//calculate point of contact
var x = this.pos.x + normal.x.map(1, -1, 0, 1) * this.size.x;
var y = this.pos.y + normal.y.map(1, -1, 0, 1) * this.size.y;
var point = {
x: x,
y: y
};
//if contact is an entity
var ent = edge.other.entity;
if (ent) {
//test for groupIndex & mask bits
var f1 = this.shape.m_filter,
f2 = ent.shape.m_filter;
if (!f1.groupIndex || f1.groupIndex != f2.groupIndex || f1.groupIndex > 0 || f1.categoryBits == f2.maskBits) {
this.collideEntity(ent, point, normal);
}
}
//else contact is a collision tile
else {
//calculate tile
var tile = ig.game.collisionMap.getTile(x, y);
this.collideTile(tile, point, normal);
//if normal force is straight up
//entity is standing
if (normal.y < 0) {
this.standing = true;
}
}
}
},
//passes collision entity, point & normal force
collideEntity: function (other, point, normal) {},
//passes the collision tile, point & normal force
collideTile: function (tile, point, normal) {},
update: function () {
this.parent();
this.collision();
}
});
});