Skip to content

Commit a65fd65

Browse files
author
monkstone
committed
re-factor ext
1 parent 5132dc1 commit a65fd65

File tree

4 files changed

+30
-41
lines changed

4 files changed

+30
-41
lines changed

ext/processing/box2d/Box2DProcessing.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public abstract class Box2DProcessing {
3838
private final float yFlip;// = -1.0f; //flip y coordinate
3939

4040
/**
41-
* Controls access to processing draw loop (via reflection)
41+
* Controls access to processing pre loop (via reflection)
4242
*/
4343
private boolean isActive = false;
4444

@@ -58,13 +58,13 @@ public Box2DProcessing(PApplet p) {
5858

5959
/**
6060
* Abstract method implement on ruby side
61+
*
6162
* @param listener Custom Listener, Sketch?
6263
*/
63-
6464
public abstract void addListener(org.jbox2d.callbacks.ContactListener listener);
6565

6666
/**
67-
*
67+
*
6868
* @param scale
6969
* @param gravity
7070
* @param warmStart
@@ -84,7 +84,6 @@ protected void setStep(float timeStep, int velocity, int position) {
8484
stepO = new Step(timeStep, velocity, position);
8585
}
8686

87-
8887
/**
8988
* This is the all important physics "step" function Says to move ahead one
9089
* unit in time Default
@@ -100,10 +99,9 @@ protected void step() {
10099
/**
101100
* Create a world
102101
*/
103-
104102
public void createWorld() {
105-
if (options == null){
106-
options = new Options();
103+
if (options == null) {
104+
options = new Options();
107105
}
108106
Vec2 gravity = new Vec2(options.gravity[0], options.gravity[1]);
109107
scaleFactor = options.scaleFactor;
@@ -145,17 +143,14 @@ public Vec2 worldToProcessing(Vec2 world) {
145143
/**
146144
* Box2d has its own coordinate system and we have to move back and forth
147145
* between them to convert from Box2d world to processing pixel space
148-
*
146+
* Note reverse Y mapping (processing poxy coord system again)
149147
* @param worldX
150148
* @param worldY
151149
* @return
152150
*/
153151
public Vec2 worldToProcessing(float worldX, float worldY) {
154-
float pixelX = map(worldX, 0f, 1f, parent.width / 2, parent.width / 2 + scaleFactor);
155-
float pixelY = map(worldY, 0f, 1f, parent.height / 2, parent.height / 2 + scaleFactor);
156-
if (yFlip == -1.0f) {
157-
pixelY = map(pixelY, 0f, height, height, 0f);
158-
}
152+
float pixelX = map(worldX, 0f, 1f, parent.width / 2, parent.width / 2 + scaleFactor);
153+
float pixelY = map(worldY, 1f, 0f, parent.height / 2, parent.height / 2 + scaleFactor);
159154
return new Vec2(pixelX, pixelY);
160155
}
161156

@@ -170,18 +165,14 @@ public Vec2 processingToWorld(Vec2 screen) {
170165
}
171166

172167
/**
173-
*
168+
* Note reverse Y mapping (processing poxy coord system again)
174169
* @param pixelX
175170
* @param pixelY
176171
* @return
177172
*/
178173
public Vec2 processingToWorld(float pixelX, float pixelY) {
179-
float worldX = map(pixelX, parent.width / 2, parent.width / 2 + scaleFactor, 0f, 1f);
180-
float worldY = pixelY;
181-
if (yFlip == -1.0f) {
182-
worldY = map(pixelY, height, 0f, 0f, height);
183-
}
184-
worldY = map(worldY, parent.height / 2, parent.height / 2 + scaleFactor, 0f, 1f);
174+
float worldX = map(pixelX, parent.width / 2, parent.width / 2 + scaleFactor, 0f, 1f);
175+
float worldY = map(pixelY, parent.height / 2, parent.height / 2 + scaleFactor, 1f, 0f);
185176
return new Vec2(worldX, worldY);
186177
}
187178

@@ -268,8 +259,7 @@ public Joint createJoint(JointDef jd) {
268259
* @return body coord as Vec2
269260
*/
270261
public Vec2 bodyCoord(Body b) {
271-
Transform xf = b.getTransform();//b.getXForm();
272-
//return coordWorldToPixels(xf.position);
262+
Transform xf = b.getTransform();
273263
return worldToProcessing(xf.p);
274264
}
275265

@@ -282,9 +272,9 @@ public void destroyBody(Body b) {
282272
}
283273

284274
/**
285-
* Access the processing draw loop by java reflection
275+
* Access the processing pre loop by java reflection
286276
*/
287-
public void draw() {
277+
public void pre() {
288278
step();
289279
}
290280

@@ -320,9 +310,9 @@ private void setActive(boolean active) {
320310
isActive = active;
321311
if (active) {
322312
parent.registerMethod("dispose", this);
323-
parent.registerMethod("draw", this);
313+
parent.registerMethod("pre", this);
324314
} else {
325-
parent.unregisterMethod("draw", this);
315+
parent.unregisterMethod("pre", this);
326316
}
327317
}
328318
}

ext/processing/box2d/Options.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public Options(float scaleFactor, float[] gravity, boolean warmStart, boolean co
3737
this.scaleFactor = scaleFactor;
3838
this.gravity = gravity;
3939
this.warm = warmStart;
40-
this.continuous = continuousPhysics;
41-
40+
this.continuous = continuousPhysics;
4241
}
4342

4443
/**

ext/processing/box2d/Step.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ public class Step {
2020
* No of position iterations
2121
*/
2222
public int posIters;
23-
23+
2424
/**
2525
*
2626
* @param timeStep
2727
* @param velIterations
2828
* @param posIterations
2929
*/
30-
public Step(float timeStep, int velIterations, int posIterations){
31-
this.timeStep = timeStep;
32-
this.velIters = velIterations;
33-
this.posIters = posIterations;
34-
}
35-
30+
public Step(float timeStep, int velIterations, int posIterations) {
31+
this.timeStep = timeStep;
32+
this.velIters = velIterations;
33+
this.posIters = posIterations;
34+
}
35+
3636
/**
3737
* Step constructor with defaults
3838
*/
39-
public Step(){
40-
this.timeStep = 1.0f / 60;
41-
this.velIters = 8;
42-
this.posIters = 10;
43-
}
39+
public Step() {
40+
this.timeStep = 1.0f / 60;
41+
this.velIters = 8;
42+
this.posIters = 10;
43+
}
4444
}

lib/pbox2d/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Module here just to keep namespace tidy
22
module Pbox2D
3-
VERSION = '0.4.1'
3+
VERSION = '0.4.2'
44
end

0 commit comments

Comments
 (0)