Sha256: fa79b13734a9d060052d8a2c4731f16e851ce45f4ed0fafb599be4463fde2834

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com

# Basic example of controlling an object with the mouse (by attaching a spring)

require 'pbox2d'
require_relative 'box'
require_relative 'boundary'
require_relative 'spring'
require_relative 'dummy_spring'
require 'forwardable'

# A reference to our box2d world
attr_reader :box2d, :boundaries, :box, :spring

def setup
  size(640, 360)
  # Initialize box2d physics and create the world
  @box2d = Box2D.new self
  box2d.create_world
  # Make the box
  @box = Box.new(width / 2, height / 2)
  # Make a dummy spring, that returns a real spring on :bind
  @spring = DummySpring.new
  # Create a bunch of fixed boundaries
  @boundaries = [
    Boundary.new(width / 2, height - 5, width, 10, 0),
    Boundary.new(width / 2, 5, width, 10, 0),
    Boundary.new(width - 5, height / 2, 10, height, 0),
    Boundary.new(5, height / 2, 10, height, 0)
  ]
end

# When the mouse is released we're done with the spring
def mouse_released
  spring.destroy
  @spring = DummySpring.new
end

# When the mouse is pressed we. . .
def mouse_pressed
  # Check to see if the mouse was clicked on the box and if so create
  # a real spring and bind the mouse location to the box with a spring
  @spring = spring.bind(mouse_x, mouse_y, box) if box.contains(mouse_x, mouse_y)
end

def draw
  background(255)
  # Always alert the spring to the new mouse location
  spring.update(mouse_x, mouse_y)
  # Draw the boundaries
  boundaries.each(&:display)
  # Draw the box
  box.display
  # Draw the spring (it only appears when active)
  spring.display
end

Version data entries

1 entries across 1 versions & 1 rubygems

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