-
Notifications
You must be signed in to change notification settings - Fork 2
/
embox2d-helpers.js
166 lines (151 loc) · 5.39 KB
/
embox2d-helpers.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//Having to type 'Box2D.' in front of everything makes porting
//existing C++ code a pain in the butt. This function can be used
//to make everything in the Box2D namespace available without
//needing to do that.
function using(ns, pattern) {
if (pattern == undefined) {
// import all
for (var name in ns) {
this[name] = ns[name];
}
} else {
if (typeof(pattern) == 'string') {
pattern = new RegExp(pattern);
}
// import only stuff matching given pattern
for (var name in ns) {
if (name.match(pattern)) {
this[name] = ns[name];
}
}
}
}
var e_shapeBit = 0x0001;
var e_jointBit = 0x0002;
var e_aabbBit = 0x0004;
var e_pairBit = 0x0008;
var e_centerOfMassBit = 0x0010;
//to replace original C++ operator =
function copyVec2(vec) {
return new b2Vec2(vec.get_x(), vec.get_y());
}
//to replace original C++ operator * (float)
function scaleVec2(vec, scale) {
vec.set_x( scale * vec.get_x() );
vec.set_y( scale * vec.get_y() );
}
//to replace original C++ operator *= (float)
function scaledVec2(vec, scale) {
return new b2Vec2(scale * vec.get_x(), scale * vec.get_y());
}
// http://stackoverflow.com/questions/12792486/emscripten-bindings-how-to-create-an-accessible-c-c-array-from-javascript
function createChainShape(vertices, closedLoop) {
var shape = new b2ChainShape();
var buffer = Box2D.allocate(vertices.length * 8, 'float', Box2D.ALLOC_STACK);
var offset = 0;
for (var i=0;i<vertices.length;i++) {
Box2D.setValue(buffer+(offset), vertices[i].get_x(), 'float'); // x
Box2D.setValue(buffer+(offset+4), vertices[i].get_y(), 'float'); // y
offset += 8;
}
var ptr_wrapped = Box2D.wrapPointer(buffer, Box2D.b2Vec2);
if ( closedLoop )
shape.CreateLoop(ptr_wrapped, vertices.length);
else
shape.CreateChain(ptr_wrapped, vertices.length);
return shape;
}
function createPolygonShape(vertices) {
var shape = new b2PolygonShape();
var buffer = Box2D.allocate(vertices.length * 8, 'float', Box2D.ALLOC_STACK);
var offset = 0;
for (var i=0;i<vertices.length;i++) {
Box2D.setValue(buffer+(offset), vertices[i].get_x(), 'float'); // x
Box2D.setValue(buffer+(offset+4), vertices[i].get_y(), 'float'); // y
offset += 8;
}
var ptr_wrapped = Box2D.wrapPointer(buffer, Box2D.b2Vec2);
shape.Set(ptr_wrapped, vertices.length);
for (var i in vertices){
Box2D.destroy(vertices[i]);
}
return shape;
}
function createEdgeShape(vertices){
var shape=new b2EdgeShape();
var buffer=Box2D.allocate(vertices.length * 8,'float',Box2D.ALLOC_STACK);
var offset =0;
for (var i=0;i<vertices.legth;i++){
Box2D.setValue(buffer+(offset),vertices[i].get_x(),'float');
Box2D.setValue(buffer+(offset+4),vertices[i].get_y(),'float');
offset+=8;
}
var ptr_wrapped =Box2D.wrapPointer(buffer,Box2D.b2Vec2);
shape.Set(ptr_wrapped,vertices.length);
for(var i in vertices){
//Box2D.destroy(vertices[i]);
}
return shape;
}
function createRandomPolygonShape(radius) {
var numVerts = 3.5 + Math.random() * 5;
numVerts = numVerts | 0;
var verts = [];
for (var i = 0; i < numVerts; i++) {
var angle = i / numVerts * 360.0 * 0.0174532925199432957;
verts.push( new b2Vec2( radius * Math.sin(angle), radius * -Math.cos(angle) ) );
}
return createPolygonShape(verts);
}
function getPosition(vert1,vert2) {
var position = new b2Vec2(0,0);
position.set_x(0.5 *(vert1.get_x()+vert2.get_x()));
position.set_y(0.5 *(vert1.get_y()+vert2.get_y()));
//Box2D.destroy(position);
//console.log('x : '+position.get_x() + 'y :' +position.get_y());
return position;
}
function getDistance(vert1,vert2){
//console.log(vert2);
var resultantVector= new b2Vec2(vert2.get_x()-vert1.get_x(),vert2.get_y()-vert1.get_y());
var distance = resultantVector.Length();
Box2D.destroy(resultantVector);
return distance;
}
function getAngle(vert1,vert2){
var resultantVector = new b2Vec2(vert2.get_x()-vert1.get_x(),vert2.get_y()-vert1.get_y());
var angle = Math.atan2(resultantVector.get_y(),resultantVector.get_x());
Box2D.destroy(resultantVector);
return angle;
}
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
var setContactListener = function(){
console.log('entered');
myContactListener = new b2ContactListener();
Box2D.customizeVTable(myContactListener,[{
original : Box2D.b2ContactListener.prototype.BeginContact,
replacement : function(thsPtr,contactPtr){
var contact = Box2D.wrapPointer(contactPtr,b2Contact);
var body1 = contact.GetFixtureA().GetBody();
var body2 = contact.GetFixtureB().GetBody();
// console.log(body1.a +' ' + body2.a);
//console.log('listener');
//console.log(' body1 ' + body1.userData.id +' body2 ' +body2.userData.id);
if(body1.userData.id == 'ball' && body2.userData.id == 'star'){
to_be_destroyed = body2.a;
//console.log(to_be_destroyed);
}
else if(body1.userData.id == 'star' && body2.userData.id == 'ball'){
to_be_destroyed = body1.a;
}
}
}])
world.SetContactListener(myContactListener);
}