forked from quidmonkey/Box2D-Collision-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collision_v2.js
71 lines (56 loc) · 1.7 KB
/
collision_v2.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
ig.module(
'plugins.box2d.collision'
)
.requires(
'plugins.box2d.entity',
'plugins.box2d.game'
)
.defines(function(){
ig.Box2DEntity.inject({
init: function (x, y, settings) {
this.parent(x, y, settings);
if (!ig.global.wm) {
this.body.entity = this;
}
}
});
ig.Box2DGame.inject({
// remove impact's collision detection for performance
// comment out this line if you're using both
// ig.Entity and ig.Box2dEntity
checkEntities: function () {},
loadLevel: function (data) {
this.parent(data);
var handleContact = function(point){
var a = point.shape1.GetBody().entity,
b = point.shape2.GetBody().entity;
// is this an entity collision?
if (!a || !b) {
return;
}
// preserve impact's entity checks even
// though these are unnecessary
if (a.checkAgainst & b.type) {
a.check(b);
}
if (b.checkAgainst & a.type) {
b.check(a);
}
// call impact
// favor the axis of greater penetration
if (Math.abs(point.normal.y) > Math.abs(point.normal.x)) {
a.collideWith(b, 'y');
b.collideWith(a, 'y');
} else {
a.collideWith(b, 'x');
b.collideWith(a, 'x');
}
};
var listener = new b2.ContactListener();
listener.Add = handleContact; // on first contact
listener.Persist = handleContact; // on subsequent contacts
// attach to box2d world
ig.world.SetContactListener(listener);
}
});
});