Sha256: 5e8d4e0c8ef364dc7c840e1fcba4002e030de8cf88cd3e22dc5b625c61e382f6

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# Basic example of falling rectangles
require 'pbox2d'
require_relative 'lib/custom_shape'
require_relative 'lib/boundary'

attr_reader :box2d, :boundaries, :polygons

def setup
  size(640, 360)
  smooth
  # Initialize box2d physics and create the world
  @box2d = Box2D.new(self)
  box2d.init_options(gravity: [0, -20])
  box2d.create_world
  # To later set a custom gravity
  # box2d.gravity([0, -20]
  # Create Arrays
  @polygons = []
  @boundaries = [
    Boundary.new(self, width / 4, height - 5, width / 2 - 50, 10),
    Boundary.new(self, 3 * width / 4, height - 50, width / 2 - 50, 10),
    Boundary.new(self, width - 5, height / 2, 10, height),
    Boundary.new(self, 5, height / 2, 10, height)
  ]
end

def draw
  background(255)
  # Display all the boundaries
  boundaries.each(&:display)
  # Display all the polygons
  polygons.each(&:display)
  # polygons that leave the screen, we delete them
  # (note they have to be deleted from both the box2d world and our list
  polygons.reject!(&:done)
end

def mouse_pressed
  polygons << CustomShape.new(self, mouse_x, mouse_y)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pbox2d-0.4.2-java examples/polygons.rb