Skip to content
monkstone edited this page Nov 9, 2014 · 16 revisions

Using pbox2d in some more detail

You should have ruby-processing and the pbox2d gem installed, from your processing sketch require pbox2d

require 'pbox2d'

# A list we'll use to track fixed objects
attr_reader :box2d, :boundaries, :boxes

In your before setup you need to include ContactListener (the java interface as a module), by the magic that is jruby this has the effect of implementing the java interface in the Sketch class (that wraps the bare sketch at runtime).

include ContactListener

You then need to create an instance of Box2D in the setup

def setup
  size(400,300)
  @box2d = Box2D.new(self)
  box2d.init_options(gravity: [0, -20])
  box2d.create_world  
  ...

Since version 0.2.0 the design of the pbox2d is somewhat different from the Dan Shiffman version, to give a cleaner more ruby like experience. Most options can now be set using either init_options (gravity etc) or step_options (time_step). Like all good ruby the code says it all, see code below from the Box2D class, where warm stands for warm_starting and continuous stands for continuous_physics for the sake of brevity:-

  def init_options(args = {})
    scale = args[:scale] || 10.0
    gravity = args[:gravity] || [0, -10.0]
    warm = args[:warm] || true
    continuous = args[:continuous] || true
    set_options(scale, gravity.to_java(Java::float), warm, continuous) # java method  
  end

  def step_options(args = {})
    time_step = args[:time_step] || 1.0 / 60
    velocity = args[:velocity_iter] || 8
    position = args[:position_iter] || 10
    set_step(time_step, velocity, position) # java method
  end

Translating from sketch to physics world and vice versa

Clone this wiki locally