-
Notifications
You must be signed in to change notification settings - Fork 0
/
bTest.js
executable file
·231 lines (204 loc) · 8.06 KB
/
bTest.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var b2Vec2 = Box2D.Common.Math.b2Vec2
, b2AABB = Box2D.Collision.b2AABB
, b2BodyDef = Box2D.Dynamics.b2BodyDef
, b2Body = Box2D.Dynamics.b2Body
, b2FixtureDef = Box2D.Dynamics.b2FixtureDef
, b2Fixture = Box2D.Dynamics.b2Fixture
, b2World = Box2D.Dynamics.b2World
, b2MassData = Box2D.Collision.Shapes.b2MassData
, b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
, b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
, b2DebugDraw = Box2D.Dynamics.b2DebugDraw
, b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef
, b2DistanceJointDef = Box2D.Dynamics.Joints.b2DistanceJointDef
, b2WeldJointDef = Box2D.Dynamics.Joints.b2WeldJointDef
, b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef
;
var last_clicked = null;
function bTest(intervalRate, adaptive, width, height, scale) {
this.intervalRate = parseInt(intervalRate);
this.adaptive = adaptive;
this.width = width;
this.height = height;
this.scale = scale;
this.bodiesMap = {};
this.world = new b2World(
new b2Vec2(0, 10) //gravity
, true //allow sleep
);
this.fixDef = new b2FixtureDef;
this.fixDef.density = 1.0;
this.fixDef.friction = 0.5;
this.fixDef.restitution = 0.2;
}
// bTest.prototype.buildGround = function() {
// //create ground
// var bodyDef = new b2BodyDef;
// bodyDef.type = b2Body.b2_staticBody;
//
// // positions the center of the object (not upper left!)
// bodyDef.position.x = this.width / 2 / this.scale;
// bodyDef.position.y = this.height / 2 / this.scale;
// bodyDef.angle = (45*Math.PI) / 180; // radians
// bodyDef.userData = '__BODY__';
//
// this.fixDef.shape = new b2PolygonShape;
//
// // half width, half height. eg actual height here is 1 unit
// this.fixDef.shape.SetAsBox((this.width-(this.width*0.1) / this.scale) / 2, (10/this.scale) / 2);
// this.registerBody(bodyDef).CreateFixture(this.fixDef);
// }
bTest.prototype.getBodyAtMouse = function() {
mousePVec = new b2Vec2(mouseX, mouseY);
var aabb = new b2AABB();
aabb.lowerBound.Set(mouseX - 0.001, mouseY - 0.001);
aabb.upperBound.Set(mouseX + 0.001, mouseY + 0.001);
// Query the world for overlapping shapes.
selectedBody = null;
this.world.QueryAABB(this.getBodyCB, aabb);
return selectedBody;
}
bTest.prototype.getBodyCB = function(fixture) {
if(fixture.GetBody().GetType() != b2Body.b2_staticBody) {
if(fixture.GetShape().TestPoint(fixture.GetBody().GetTransform(), mousePVec)) {
selectedBody = fixture.GetBody();
return false;
}
}
return true;
}
bTest.prototype.update = function() {
if (!connecting) {
if(isMouseDown && (!mouseJoint) ) {
var body = this.getBodyAtMouse();
last_clicked = body;
if(body) {
if (freebees <= 2 && (body.m_userData == "hyper" || body.m_userData == "ist") ) {
connections_made++;
freebees++;
updateStars();
}
body.SetAngle(0);
fixedY = body.GetPosition().y;
var md = new b2MouseJointDef();
md.bodyA = this.world.GetGroundBody();
md.bodyB = body;
md.target.Set(mouseX, fixedY);
md.collideConnected = true;
md.maxForce = 300.0 * body.GetMass();
mouseJoint = this.world.CreateJoint(md);
body.SetAwake(true);
}
}
if(mouseJoint) {
if(isMouseDown && fixedY ) {
mouseJoint.SetTarget(new b2Vec2(mouseX, fixedY));
} else {
this.world.DestroyJoint(mouseJoint);
mouseJoint = null;
}
}
}
var start = Date.now();
var stepRate = (this.adaptive) ? (now - this.lastTimestamp) / 1000 : (1 / this.intervalRate);
this.world.Step(
stepRate //frame-rate
, 10 //velocity iterations
, 10 //position iterations
);
this.world.ClearForces();
return (Date.now() - start);
}
bTest.prototype.getState = function() {
var state = {};
for (var b = this.world.GetBodyList(); b; b = b.m_next) {
if (b.IsActive() && typeof b.GetUserData() !== 'undefined' && b.GetUserData() != null) {
state[b.GetUserData()] = this.getBodySpec(b);
}
}
return state;
}
bTest.prototype.getBodySpec = function(b) {
return {x: b.GetPosition().x, y: b.GetPosition().y, a: b.GetAngle(), c: {x: b.GetWorldCenter().x, y: b.GetWorldCenter().y}};
}
bTest.prototype.setBodies = function(bodyEntities) {
var bodyDef = new b2BodyDef;
for(var id in bodyEntities) {
var entity = bodyEntities[id];
if (entity.character_type == 'platform' || entity.character_type == 'wall') {
bodyDef.type = b2Body.b2_staticBody;
} else {
bodyDef.type = b2Body.b2_dynamicBody;
}
bodyDef.position.x = entity.x;
bodyDef.position.y = entity.y;
bodyDef.userData = entity.id;
bodyDef.angle = entity.angle;
var body = this.registerBody(bodyDef);
if (entity.radius) {
this.fixDef.shape = new b2CircleShape(entity.radius);
body.CreateFixture(this.fixDef);
} else if (entity.polys) {
for (var j = 0; j < entity.polys.length; j++) {
var points = entity.polys[j];
var vecs = [];
for (var i = 0; i < points.length; i++) {
var vec = new b2Vec2();
vec.Set(points[i].x, points[i].y);
vecs[i] = vec;
}
this.fixDef.shape = new b2PolygonShape;
this.fixDef.shape.SetAsArray(vecs, vecs.length);
body.CreateFixture(this.fixDef);
}
} else {
this.fixDef.shape = new b2PolygonShape;
this.fixDef.shape.SetAsBox(entity.halfWidth, entity.halfHeight);
body.CreateFixture(this.fixDef);
}
}
this.ready = true;
}
bTest.prototype.registerBody = function(bodyDef) {
var body = this.world.CreateBody(bodyDef);
this.bodiesMap[body.GetUserData()] = body;
return body;
}
bTest.prototype.addRevoluteJoint = function(body1Id, body2Id, params) {
var body1 = this.bodiesMap[body1Id];
var body2 = this.bodiesMap[body2Id];
var joint = new b2RevoluteJointDef();
joint.Initialize(body1, body2, body1.GetWorldCenter());
if (params && params.motorSpeed) {
joint.motorSpeed = params.motorSpeed;
joint.maxMotorTorque = params.maxMotorTorque;
joint.enableMotor = true;
}
this.world.CreateJoint(joint);
}
bTest.prototype.applyImpulse = function(bodyId, degrees, power) {
var body = this.bodiesMap[bodyId];
body.ApplyImpulse(new b2Vec2(Math.cos(degrees * (Math.PI / 180)) * power,
Math.sin(degrees * (Math.PI / 180)) * power),
body.GetWorldCenter());
}
bTest.prototype.addContactListener = function(callbacks) {
var listener = new Box2D.Dynamics.b2ContactListener;
if (callbacks.BeginContact) listener.BeginContact = function(contact) {
callbacks.BeginContact(contact.GetFixtureA().GetBody().GetUserData(),
contact.GetFixtureB().GetBody().GetUserData());
}
if (callbacks.EndContact) listener.EndContact = function(contact) {
callbacks.EndContact(contact.GetFixtureA().GetBody().GetUserData(),
contact.GetFixtureB().GetBody().GetUserData());
}
if (callbacks.PostSolve) listener.PostSolve = function(contact, impulse) {
callbacks.PostSolve(contact.GetFixtureA().GetBody().GetUserData(),
contact.GetFixtureB().GetBody().GetUserData(),
impulse.normalImpulses[0]);
}
this.world.SetContactListener(listener);
}
bTest.prototype.removeBody = function(id) {
this.world.DestroyBody(this.bodiesMap[id]);
}