Sha256: 4222f5e7f29648cdaf12479ce250c0ce606e31b9bf27de9780de9db1425f8a52

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 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 'forwardable'
require_relative 'box'
require_relative 'boundary'
require_relative 'spring'
require_relative 'dummy_spring'


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

def settings
  size(640, 360)
end

def setup
  sketch_title 'Mouse Joint'
  # Initialize box2d physics and create the world
  @box2d = WorldBuilder.build(app: self)
  # Make the box
  @box = Box.new(width / 2, height / 2)
  # Make a dummy spring
  @spring = DummySpring.new
  # Add a bunch of fixed boundaries
  @boundaries = []
  boundaries << Boundary.new(width / 2, height - 5, width, 10, 0)
  boundaries << Boundary.new(width / 2, 5, width, 10, 0)
  boundaries << Boundary.new(width - 5, height / 2, 10, height, 0)
  boundaries << 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-1.0.3-java examples/mouse_joint/mouse_joint.rb