|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "PBox2D" |
| 4 | +permalink: /classes/pbox2d/ |
| 5 | +--- |
| 6 | + |
| 7 | +The PBox2D class is a simple wrapper to help integrate the JBox2D world with ruby implementations of processing (_JRubyArt and propane_). Use the [WorldBuilder][world_builder] in your sketch setup to create an instance of this class. |
| 8 | + |
| 9 | +```ruby |
| 10 | +# Ruby version of java wrapper allows us to have more |
| 11 | +# rubified interface, also needed for add_listener |
| 12 | +class Box2D < Java::ProcessingBox2d::Box2DProcessing |
| 13 | + field_accessor :world # allow access to protected variable |
| 14 | + |
| 15 | + def init_options(args = {}) |
| 16 | + args = defaults.merge(args) |
| 17 | + set_options(args[:scale], |
| 18 | + args[:gravity].to_java(Java::float), |
| 19 | + args[:warm], |
| 20 | + args[:continuous] |
| 21 | + ) |
| 22 | + end |
| 23 | + |
| 24 | + def step_options(args = {}) |
| 25 | + default_step.merge(args) |
| 26 | + set_step(args[:time_step], args[:velocity_iter], args[:position_iter]) |
| 27 | + end |
| 28 | + |
| 29 | + def defaults |
| 30 | + { scale: 10.0, gravity: [0, -10], warm: true, continuous: true } |
| 31 | + end |
| 32 | + |
| 33 | + def default_step |
| 34 | + { time_step: 1.0 / 60, velocity_iter: 8, position_iter: 10 } |
| 35 | + end |
| 36 | + |
| 37 | + def gravity(args) |
| 38 | + change_gravity(args.to_java(Java::float)) |
| 39 | + end |
| 40 | + |
| 41 | + def add_listener(listener) |
| 42 | + # in combination with field accessor we can access protected world |
| 43 | + world.setContactListener(listener) |
| 44 | + end |
| 45 | + |
| 46 | + def version |
| 47 | + format('pbox2d version %s', Pbox2d::VERSION) |
| 48 | + end |
| 49 | +end |
| 50 | +``` |
| 51 | + |
| 52 | +The java class |
| 53 | +```java |
| 54 | + |
| 55 | +package processing.box2d; |
| 56 | + |
| 57 | +import org.jbox2d.common.Transform; |
| 58 | +import org.jbox2d.common.Vec2; |
| 59 | +import org.jbox2d.dynamics.Body; |
| 60 | +import org.jbox2d.dynamics.BodyDef; |
| 61 | +import org.jbox2d.dynamics.World; |
| 62 | +import org.jbox2d.dynamics.joints.Joint; |
| 63 | +import org.jbox2d.dynamics.joints.JointDef; |
| 64 | + |
| 65 | +import processing.core.PApplet; |
| 66 | + |
| 67 | +/** |
| 68 | + * Loosely based on Box2D-for-processing by Dan Shiffman |
| 69 | + * |
| 70 | + * @author Martin Prout |
| 71 | + */ |
| 72 | +public abstract class Box2DProcessing { |
| 73 | + |
| 74 | + private final PApplet parent; |
| 75 | + private Options options; |
| 76 | + private Step stepO; |
| 77 | + private final float height; |
| 78 | + private final float width; |
| 79 | + /** |
| 80 | + * The Box2D world (we might need public access for our ContactListener) |
| 81 | + */ |
| 82 | + protected World world; |
| 83 | + |
| 84 | + /** |
| 85 | + * Scale between processing sketch and physics world |
| 86 | + */ |
| 87 | + private float scaleFactor;// = 10.0f; |
| 88 | + |
| 89 | + /** |
| 90 | + * Adjust for processing.org unfathomable choice of y-axis direction |
| 91 | + */ |
| 92 | + private final float yFlip;// = -1.0f; //flip y coordinate |
| 93 | + |
| 94 | + /** |
| 95 | + * Controls access to processing pre loop (via reflection) |
| 96 | + */ |
| 97 | + private boolean isActive = false; |
| 98 | + |
| 99 | + private Body groundBody; |
| 100 | + |
| 101 | + /** |
| 102 | + * |
| 103 | + * @param p PApplet |
| 104 | + */ |
| 105 | + public Box2DProcessing(PApplet p) { |
| 106 | + parent = p; |
| 107 | + height = p.height; |
| 108 | + width = p.width; |
| 109 | + yFlip = -1; |
| 110 | + setActive(true); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Abstract method implement on ruby side |
| 115 | + * |
| 116 | + * @param listener Custom Listener, Sketch? |
| 117 | + */ |
| 118 | + public abstract void addListener(org.jbox2d.callbacks.ContactListener listener); |
| 119 | + |
| 120 | + /** |
| 121 | + * |
| 122 | + * @param scale float |
| 123 | + * @param gravity float[] |
| 124 | + * @param warmStart boolean |
| 125 | + * @param continuous boolean |
| 126 | + */ |
| 127 | + protected void setOptions(float scale, float[] gravity, boolean warmStart, boolean continuous) { |
| 128 | + options = new Options(scale, gravity, warmStart, continuous); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * |
| 133 | + * @param timeStep float |
| 134 | + * @param velocity int |
| 135 | + * @param position int |
| 136 | + */ |
| 137 | + protected void setStep(float timeStep, int velocity, int position) { |
| 138 | + stepO = new Step(timeStep, velocity, position); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * This is the all important physics "step" function Says to move ahead one |
| 143 | + * unit in time Default |
| 144 | + */ |
| 145 | + protected void step() { |
| 146 | + if (stepO == null) { |
| 147 | + stepO = new Step(); |
| 148 | + } |
| 149 | + world.step(stepO.timeStep, stepO.velIters, stepO.posIters); |
| 150 | + world.clearForces(); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Create a world |
| 155 | + */ |
| 156 | + public void createWorld() { |
| 157 | + if (options == null) { |
| 158 | + options = new Options(); |
| 159 | + } |
| 160 | + Vec2 gravity = new Vec2(options.gravity[0], options.gravity[1]); |
| 161 | + scaleFactor = options.scaleFactor; |
| 162 | + world = new World(gravity); |
| 163 | + world.setWarmStarting(options.warm); |
| 164 | + world.setContinuousPhysics(options.continuous); |
| 165 | + BodyDef bodyDef = new BodyDef(); |
| 166 | + groundBody = world.createBody(bodyDef); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * |
| 171 | + * @return Body |
| 172 | + */ |
| 173 | + public Body groundBody() { |
| 174 | + return groundBody; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Set the gravity (this can change in real-time) |
| 179 | + * |
| 180 | + * @param gravity float[] |
| 181 | + */ |
| 182 | + protected void changeGravity(float[] gravity) { |
| 183 | + world.setGravity(new Vec2(gravity[0], gravity[1])); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Box2d has its own coordinate system and we have to move back and forth |
| 188 | + * between them to convert from Box2d world to processing pixel space |
| 189 | + * |
| 190 | + * @param world Vec2 |
| 191 | + * @return Vec2 |
| 192 | + */ |
| 193 | + public Vec2 worldToProcessing(Vec2 world) { |
| 194 | + return worldToProcessing(world.x, world.y); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Box2d has its own coordinate system and we have to move back and forth |
| 199 | + * between them to convert from Box2d world to processing pixel space |
| 200 | + * Note reverse Y mapping (processing poxy coord system again) |
| 201 | + * @param worldX float |
| 202 | + * @param worldY float |
| 203 | + * @return Vec2 |
| 204 | + */ |
| 205 | + public Vec2 worldToProcessing(float worldX, float worldY) { |
| 206 | + float pixelX = map(worldX, 0f, 1f, parent.width / 2, parent.width / 2 + scaleFactor); |
| 207 | + float pixelY = map(worldY, 1f, 0f, parent.height / 2, parent.height / 2 + scaleFactor); |
| 208 | + return new Vec2(pixelX, pixelY); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * convert Coordinate from pixel space to box2d world |
| 213 | + * |
| 214 | + * @param screen Vec2 |
| 215 | + * @return Vec2 |
| 216 | + */ |
| 217 | + public Vec2 processingToWorld(Vec2 screen) { |
| 218 | + return processingToWorld(screen.x, screen.y); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Note reverse Y mapping (processing poxy coord system again) |
| 223 | + * @param pixelX float |
| 224 | + * @param pixelY float |
| 225 | + * @return Vec2 |
| 226 | + */ |
| 227 | + public Vec2 processingToWorld(float pixelX, float pixelY) { |
| 228 | + float worldX = map(pixelX, parent.width / 2, parent.width / 2 + scaleFactor, 0f, 1f); |
| 229 | + float worldY = map(pixelY, parent.height / 2, parent.height / 2 + scaleFactor, 1f, 0f); |
| 230 | + return new Vec2(worldX, worldY); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Scale from processing to world |
| 235 | + * |
| 236 | + * @param val float |
| 237 | + * @return float |
| 238 | + */ |
| 239 | + public float scaleToWorld(float val) { |
| 240 | + return val / scaleFactor; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Scale from world to processing |
| 245 | + * |
| 246 | + * @param val float |
| 247 | + * @return float |
| 248 | + */ |
| 249 | + public float scaleToProcessing(float val) { |
| 250 | + return val * scaleFactor; |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Vector scale between two worlds |
| 255 | + * |
| 256 | + * @param v Vec2 |
| 257 | + * @return Vec2 |
| 258 | + */ |
| 259 | + public Vec2 vectorToWorld(Vec2 v) { |
| 260 | + Vec2 u = new Vec2(v.x / scaleFactor, v.y / scaleFactor); |
| 261 | + u.y *= yFlip; |
| 262 | + return u; |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * Translate from world coords to processing as a Vec2 |
| 267 | + * |
| 268 | + * @param x float |
| 269 | + * @param y float |
| 270 | + * @return Vec |
| 271 | + */ |
| 272 | + public Vec2 vectorToWorld(float x, float y) { |
| 273 | + Vec2 u = new Vec2(x / scaleFactor, y / scaleFactor); |
| 274 | + u.y *= yFlip; |
| 275 | + return u; |
| 276 | + } |
| 277 | + |
| 278 | + /** |
| 279 | + * Translate from world to processing as a Vec2 |
| 280 | + * |
| 281 | + * @param v Vec |
| 282 | + * @return Vec |
| 283 | + */ |
| 284 | + public Vec2 vectorToProcessing(Vec2 v) { |
| 285 | + Vec2 u = new Vec2(v.x * scaleFactor, v.y * scaleFactor); |
| 286 | + u.y *= yFlip; |
| 287 | + return u; |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * A common task we have to do a lot |
| 292 | + * |
| 293 | + * @param bd BodyDef |
| 294 | + * @return Body |
| 295 | + */ |
| 296 | + public Body createBody(BodyDef bd) { |
| 297 | + return world.createBody(bd); |
| 298 | + } |
| 299 | + |
| 300 | + /** |
| 301 | + * A common task we have to do a lot |
| 302 | + * |
| 303 | + * @param jd JointDef |
| 304 | + * @return World |
| 305 | + */ |
| 306 | + public Joint createJoint(JointDef jd) { |
| 307 | + return world.createJoint(jd); |
| 308 | + } |
| 309 | + |
| 310 | + /** |
| 311 | + * |
| 312 | + * @param b Body |
| 313 | + * @return body coord as Vec2 |
| 314 | + */ |
| 315 | + public Vec2 bodyCoord(Body b) { |
| 316 | + Transform xf = b.getTransform(); |
| 317 | + return worldToProcessing(xf.p); |
| 318 | + } |
| 319 | + |
| 320 | + /** |
| 321 | + * |
| 322 | + * @param b Body |
| 323 | + */ |
| 324 | + public void destroyBody(Body b) { |
| 325 | + world.destroyBody(b); |
| 326 | + } |
| 327 | + |
| 328 | + /** |
| 329 | + * Access the processing pre loop by java reflection |
| 330 | + */ |
| 331 | + public void pre() { |
| 332 | + step(); |
| 333 | + } |
| 334 | + |
| 335 | + /** |
| 336 | + * Recommended inclusion in a processing library |
| 337 | + */ |
| 338 | + public void dispose() { |
| 339 | + setActive(false); |
| 340 | + } |
| 341 | + |
| 342 | + /** |
| 343 | + * |
| 344 | + * @return height float |
| 345 | + */ |
| 346 | + public float height() { |
| 347 | + return height; |
| 348 | + } |
| 349 | + |
| 350 | + /** |
| 351 | + * |
| 352 | + * @return width float |
| 353 | + */ |
| 354 | + public float width() { |
| 355 | + return width; |
| 356 | + } |
| 357 | + |
| 358 | + private float map(float val, float startIn, float endIn, float startOut, float endOut) { |
| 359 | + return startOut + (endOut - startOut) * ((val - startIn) / (endIn - startIn)); |
| 360 | + } |
| 361 | + |
| 362 | + private void setActive(boolean active) { |
| 363 | + if (active != isActive) { |
| 364 | + isActive = active; |
| 365 | + if (active) { |
| 366 | + parent.registerMethod("dispose", this); |
| 367 | + parent.registerMethod("pre", this); |
| 368 | + } else { |
| 369 | + parent.unregisterMethod("pre", this); |
| 370 | + } |
| 371 | + } |
| 372 | + } |
| 373 | +} |
| 374 | + |
| 375 | +``` |
0 commit comments