Sha256: 538ec85705c89dc727009f4ac69bbde018d74d23390bfd703eb42da1b0fa30f2

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

module Joybox
  module Physics

    class World < B2DWorld

      def self.defaults
        {
          gravity: [0, 0],
          allows_sleeping: true,
          continuous_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.continuousPhysics = options[:continuous_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) if 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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
joybox-1.0.0 motion/joybox/physics/world.rb