Skip to content

Commit 4bbf2e8

Browse files
CardillosCreationsCardillosCreations
authored andcommitted
A few small tweaks to the JBox2D physics simulation parameters (removed experimental features).
1 parent 6b7e899 commit 4bbf2e8

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/org/cocos2d/tests/JBox2DTest.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void onDestroy() {
100100

101101
static class JBox2DTestLayer extends Layer {
102102
public static final int kTagSpriteManager = 1;
103-
103+
104104
// Pixel to meters ratio. Box2D uses meters as the unit for measurement.
105105
// This ratio defines how many pixels correspond to 1 Box2D "meter"
106106
// Box2D is optimized for objects of 1x1 meter therefore it makes sense
@@ -125,8 +125,6 @@ public JBox2DTestLayer() {
125125
Vec2 gravity = new Vec2(0.0f, -10.0f);
126126

127127
bxWorld = new World(new AABB(lower, upper), gravity, true);
128-
bxWorld.setContinuousPhysics(true);
129-
bxWorld.setPositionCorrection(true);
130128

131129
// TODO: JBox2D debug drawing is a bit different now. We could add debug drawing the old way, but looks like that is going away soon.
132130
/*
@@ -164,12 +162,25 @@ public JBox2DTestLayer() {
164162
label.setPosition(s.width / 2f, s.height - 50f);
165163
label.setColor(new CCColor3B(0, 0, 255));
166164
addChild(label);
167-
168-
// schedule the physics update processor
169-
this.schedule("tick");
170165
}
171166

172-
private void addNewSpriteWithCoords(CCPoint pos) {
167+
@Override
168+
public void onEnter() {
169+
super.onEnter();
170+
171+
// start ticking (for physics simulation)
172+
schedule("tick");
173+
}
174+
175+
@Override
176+
public void onExit() {
177+
super.onExit();
178+
179+
// stop ticking (for physics simulation)
180+
unschedule("tick");
181+
}
182+
183+
private void addNewSpriteWithCoords(CCPoint pos) {
173184
AtlasSpriteManager mgr = (AtlasSpriteManager) getChild(kTagSpriteManager);
174185

175186
// We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
@@ -194,24 +205,23 @@ private void addNewSpriteWithCoords(CCPoint pos) {
194205
bxSpritePolygonDef.density = 1.0f;
195206
bxSpritePolygonDef.friction = 0.3f;
196207

197-
synchronized(bxWorld) {
198-
Body bxSpriteBody = bxWorld.createBody(bxSpriteBodyDef);
199-
208+
synchronized (bxWorld) {
200209
// Define the dynamic body fixture and set mass so it's dynamic.
201-
bxSpriteBody.createShape(bxSpritePolygonDef);
202-
bxSpriteBody.setMassFromShapes();
210+
Body bxSpriteBody = bxWorld.createBody(bxSpriteBodyDef);
211+
bxSpriteBody.createShape(bxSpritePolygonDef);
212+
bxSpriteBody.setMassFromShapes();
203213
}
204214
}
205215

206-
public void tick(float delta) {
216+
public synchronized void tick(float delta) {
207217
// It is recommended that a fixed time step is used with Box2D for stability
208218
// of the simulation, however, we are using a variable time step here.
209219
// You need to make an informed choice, the following URL is useful
210220
// http://gafferongames.com/game-physics/fix-your-timestep/
211221

212222
// Instruct the world to perform a simulation step. It is
213223
// generally best to keep the time step and iterations fixed.
214-
synchronized(bxWorld) {
224+
synchronized (bxWorld) {
215225
bxWorld.step(delta, 10);
216226
}
217227

@@ -233,7 +243,7 @@ public boolean ccTouchesBegan(MotionEvent event) {
233243
CCPoint location = Director.sharedDirector().convertCoordinate(event.getX(), event.getY());
234244

235245
addNewSpriteWithCoords(location);
236-
246+
237247
return TouchDispatcher.kEventHandled;
238248
}
239249

0 commit comments

Comments
 (0)