Sha256: 6396cfcfb71dcb4c2054c2c6f6a301d22b4442b486f67d76ec474e8bf4ebb25b
Contents?: true
Size: 1.56 KB
Versions: 2
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 @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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
pbox2d-0.5.0-java | examples/mouse_joint/mouse_joint.rb |
pbox2d-0.4.1-java | examples/mouse_joint/mouse_joint.rb |