Sha256: 440cb612299450525cb0df94bdd9a32cd8b276fc8787aba7607dc7a1b2757f0c

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

module Joybox

  class World < B2DWorld

    def self.defaults
      {
        gravity: [0, 0],
        allows_sleeping: true,
        continuos_physics: true
      }
    end


    def self.new(options)

      options = options.nil? ? defaults : defaults.merge!(options)

      world = World.alloc.init 

      world.gravity = options[:gravity]
      world.allowsSleeping = options[:allows_sleeping]
      world.continuosPhysics = options[:continuos_physics]

      world
    end


    def step_defaults
      {
        velocity_interactions: 8,
        position_interactions: 1
      }
    end


    def step(options = {})

      options = options.nil? ? step_defaults : step_defaults.merge!(options)

      stepWithDelta(options[:delta], 
        velocityInteractions: options[:velocity_interactions],
        positionInteractions: options[:position_interactions])
    end


    def new_body(options = {}, &block)

      body = Body.new(self, options)
      body.instance_eval(&block)

      body 
    end


    def setup_collision_listener

      @contact_listener = B2DContactListener.new
      addContactListener(@contact_listener)

      @listening_bodies = Hash.new

      @contact_listener.beginContact = lambda { | first_body, second_body, is_touching |

        @listening_bodies[first_body].call(second_body, is_touching) if @listening_bodies.include? first_body
        @listening_bodies[second_body].call(first_body, is_touching) if @listening_bodies.include? second_body
      }
    end


    def when_collide(body, &block)

      setup_collision_listener unless @contact_listener

      @listening_bodies[body] = block
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
joybox-0.0.5 lib/joybox/box2d/world.rb
joybox-0.0.4 lib/joybox/box2d/world.rb
joybox-0.0.3 lib/joybox/box2d/world.rb