-
Notifications
You must be signed in to change notification settings - Fork 1
/
boxworld.js
75 lines (58 loc) · 2.01 KB
/
boxworld.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
"use strict";
define(['boxregistry', 'lib/box2d'], function(boxregistry) {
function setBeginContactListener( listener, onContact ) {
Box2D.customizeVTable(listener, [{
original: Box2D.b2ContactListener.prototype.BeginContact,
replacement: onContact
}]
);
}
function create() {
var gravity = new Box2D.b2Vec2( 0.0, -10.0 );
var b2world = new Box2D.b2World( gravity );
var objects = boxregistry.create();
objects.addDeletionListener( function( object ) {
if( object.body ) {
b2world.DestroyBody( object.body );
}
});
function update() {
b2world.Step( 1/30, 20, 20 );
objects.processDeletions();
}
function add( bodyFunc, params ) {
var body = bodyFunc( b2world, params );
var object = objects.registerBody( body );
return object;
}
var listener = new Box2D.b2ContactListener();
b2world.SetContactListener( listener );
function onBeginContact( thsPtr, contactPtr ) {
console.log('contact');
var contacts = Box2D.wrapPointer( contactPtr, Box2D.b2Contact );
var fixtureA = contacts.GetFixtureA();
var fixtureB = contacts.GetFixtureB();
var objectA = objects.getByFixture( fixtureA );
var objectB = objects.getByFixture( fixtureB );
if( objectA.isMarkedForDeletion ||
objectB.isMarkedForDeletion ) {
return;
}
if( objectA.onContact ) {
objectA.onContact( objectB );
}
if( objectB.onContact ) {
objectB.onContact( objectA );
}
}
setBeginContactListener( listener, onBeginContact );
return {
getb2world: function() { return b2world; },
update:update,
add:add,
}
}
return {
create:create
}
});