Permalink
Browse files

update to shiffmans box2d library

  • Loading branch information...
1 parent a4de999 commit 0141974568d8a75b265f0e1255d9a5d220785937 @monkstone monkstone committed Mar 2, 2014
View
@@ -1,7 +1,7 @@
syntax: glob
.DS_Store
*.sw*
-*.*~
+*~
*.pdf
*.gem
*.tgz
@@ -1,12 +1,11 @@
# The Nature of Code
-# <http:#www.shiffman.net/teaching/nature>
+# <http://www.shiffman.net/teaching/nature>
# Spring 2010
# PBox2D example
# An uneven surface
-load_library :pbox2d
-load_library :surface
+load_libraries :box2d_processing, :surface
include SB
@@ -17,7 +16,7 @@ def setup
smooth
# Initialize box2d physics and create the world
- @box2d = PBox2D.new(self)
+ @box2d = SB::Box2DProcessing.new(self)
box2d.create_world
# We are setting a custom gravity
box2d.set_gravity(0, -20)
@@ -46,11 +45,7 @@ def draw
end
# Particles that leave the screen, we delete them
# (note they have to be deleted from both the box2d world and our list
- particles.each_with_index do |p, i|
- if (p.done)
- particles.delete_at(i)
- end
- end
+ particles.delete_if {|p| p.done}
# Just drawing the framerate to see how many particles it can handle
fill(0)
text("framerate: #{frame_rate.to_i}", 12, 16)
@@ -3,7 +3,7 @@ module B2D
include_package 'org.jbox2d.collision.shapes'
include_package 'org.jbox2d.common'
include_package 'org.jbox2d.dynamics'
- java_import 'pbox2d.PBox2D'
+ include_package 'shiffman.box2d'
# The Nature of Code
@@ -2,22 +2,22 @@ module PS
include_package 'org.jbox2d.collision.shapes'
include_package 'org.jbox2d.common'
include_package 'org.jbox2d.dynamics'
- java_import 'pbox2d.PBox2D'
-
-
+ include_package 'shiffman.box2d'
+
# Box2D Particle System
# <http://www.shiffman.net/teaching/nature>
# Spring 2010
+ # translated to ruby-processing Martin Prout
# A class to describe a group of Particles
- # An ArrayList is used to manage the list of Particles
+ # An Array is used to manage the list of Particles
class ParticleSystem
attr_reader :particles, :x, :y
def initialize(bd, num, x, y)
- @particles = [] # Initialize the ArrayList
+ @particles = [] # Initialize the Array
@x, @y = x, y # Store the origin point
num.times do
particles << PS::Particle.new(bd, x, y)
@@ -32,11 +32,8 @@ def run
# Particles that leave the screen, we delete them
# (note they have to be deleted from both the box2d world and our list
- particles.each_with_index do |p, i|
- if (p.done)
- particles.delete_at(i)
- end
- end
+ particles.delete_if { |p| p.done}
+
end
def add_particles(bd, n)
@@ -1,5 +1,5 @@
# The Nature of Code
-# <http:#www.shiffman.net/teaching/nature>
+# <http://www.shiffman.net/teaching/nature>
# Spring 2010
# PBox2D example
@@ -9,7 +9,7 @@ module SB
include_package 'org.jbox2d.collision.shapes'
include_package 'org.jbox2d.common'
include_package 'org.jbox2d.dynamics'
- java_import 'pbox2d.PBox2D'
+ include_package 'shiffman.box2d'
@@ -1,14 +1,14 @@
# The Nature of Code
# <http:#www.shiffman.net/teaching/nature>
# Spring 2011
-# PBox2D example
+# Updated to use the updated library
+# translated to ruby-processing 2 March 2014 by Martin Prout
+# Box2DProcessing example
-# Box2D particle system example
+load_libraries :box2d_processing, :particle_system
-load_library :pbox2d
-load_library :particle_system
-# module PS is a wrapper for java imports, and Boundary and Particle classes
+# module PS is a wrapper for java packages, and Boundary and Particle classes
include PS
attr_reader :box2d, :boundaries, :systems
@@ -17,11 +17,11 @@ def setup
size(400,300)
smooth
# Initialize box2d physics and create the world
- @box2d = PBox2D.new(self)
+ @box2d = PS::Box2DProcessing.new(self)
box2d.create_world
# We are setting a custom gravity
box2d.set_gravity(0, -20)
- # Create ArrayLists
+ # Create Arrays
@systems = []
@boundaries = []
# Add a bunch of fixed boundaries
@@ -1,11 +1,10 @@
# The Nature of Code
# <http://www.shiffman.net/teaching/nature>
# Spring 2011
-# PBox2D example
+# Box2DProcessing example
# Basic example of falling rectangles
-load_library :pbox2d
-load_library :custom_shape
+load_libraries :box2d_processing, :custom_shape
# module B2D is a wrapper for java imports, and Boundary and CustomShape classes
include B2D
@@ -16,7 +15,7 @@ def setup
size(640,360)
smooth
# Initialize box2d physics and create the world
- @box2d = PBox2D.new(self)
+ @box2d = B2D::Box2DProcessing.new(self)
box2d.create_world
# We are setting a custom gravity
box2d.set_gravity(0, -20)
@@ -46,11 +45,7 @@ def draw
# polygons that leave the screen, we delete them
# (note they have to be deleted from both the box2d world and our list
- polygons.each_with_index do |polygon, i|
- if polygon.done
- polygons.delete_at(i)
- end
- end
+ polygons.delete_if { |p| p.done}
end
def mouse_pressed
@@ -1,32 +0,0 @@
-load_library 'pbox2d'
-
-class JContactListener
- include org.jbox2d.callbacks.ContactListener
- attr_reader :parent
- def initialize(applet)
- @parent = applet
- end
- java_signature("void beginContact(org.jbox2d.dynamics.contacts.Contact c)")
- def begin_contact(c)
- end
- java_signature("void endContact(org.jbox2d.dynamics.contacts.Contact c)")
- def end_contact(c)
- end
- java_signature("void beginContact(org.jbox2d.dynamics.contacts.Contact,org.jbox2d.callbacks.ContactImpulse )")
- def pre_solve(c, ci)
- end
- java_signature("void beginContact(org.jbox2d.dynamics.contacts.Contact,org.jbox2d.callbacks.ContactImpulse )")
- def post_solve(c, ci)
- end
-
-
-end
-
-
-def setup
- size 200, 200
- fred = JContactListener.new(self)
- fred.become_java
- puts fred.methods
-
-end
@@ -1,158 +0,0 @@
-module B2D
- include_package 'pbox2d'
- include_package 'org.jbox2d.collision.shapes'
- include_package 'org.jbox2d.common'
- include_package 'org.jbox2d.dynamics'
- java_import 'pbox2d.PBox2D'
-
-
- # The Nature of Code
- # <http:#www.shiffman.net/teaching/nature>
- # Spring 2011
- # PBox2D example
- # A rectangular box
-
- class CustomShape
-
- # We need to keep track of a Body and a width and height
- attr_reader :body, :box2d
-
- # Constructor
- def initialize(b2d, x, y)
- # Add the box to the box2d world
- @box2d = b2d
- make_body(B2D::Vec2.new(x, y))
- end
-
- # This function removes the particle from the box2d world
- def kill_body!
- box2d.destroy_body(body)
- end
-
- # Is the particle ready for deletion?
- def done
- # Let's find the screen position of the particle
- pos = box2d.get_body_pixel_coord(body)
- # Is it off the bottom of the screen?
- if (pos.y > $app.height)
- kill_body!
- return true
- end
- return false
- end
-
- # Drawing the box
- def display
- # We look at each body and get its screen position
- pos = box2d.get_body_pixel_coord(body)
- # Get its angle of rotation
- a = body.get_angle
-
- f = body.get_fixture_list
- ps = f.get_shape
-
-
- rect_mode(CENTER)
- push_matrix
- translate(pos.x, pos.y)
- rotate(-a)
- fill(175)
- stroke(0)
- begin_shape
- # For every vertex, convert to pixel vector
- ps.get_vertex_count.times do |i|
- v = box2d.vector_world_to_pixels(ps.get_vertex(i))
- vertex(v.x, v.y)
- end
- end_shape(CLOSE)
- pop_matrix
- end
-
- # This function adds the rectangle to the box2d world
- def make_body(center)
-
- # Define a polygon (this is what we use for a rectangle)
- sd = B2D::PolygonShape.new
-
- vertices = []
- vertices << box2d.vector_pixels_to_world(B2D::Vec2.new(-15, 25))
- vertices << box2d.vector_pixels_to_world(B2D::Vec2.new(15, 0))
- vertices << box2d.vector_pixels_to_world(B2D::Vec2.new(20, -15))
- vertices << box2d.vector_pixels_to_world(B2D::Vec2.new(-10, -10))
- sd.set(vertices.to_java(Java::OrgJbox2dCommon::Vec2), vertices.length)
-
- # Define the body and make it from the shape
- bd = B2D::BodyDef.new
- bd.type = B2D::BodyType::DYNAMIC
- bd.position.set(box2d.coord_pixels_to_world(center))
- @body = box2d.create_body(bd)
-
- body.create_fixture(sd, 1.0)
-
-
- # Give it some initial random velocity
- body.set_linear_velocity(Vec2.new(rand(-5 .. 5), rand(2 .. 5)))
- body.set_angular_velocity(rand(-5 .. 5))
- end
- end
-
- # The Nature of Code
- # <http://www.shiffman.net/teaching/nature>
- # Spring 2012
- # PBox2D example
-
- # A fixed boundary class (now incorporates angle)
-
-
-
- class Boundary
-
- attr_reader :box2d, :b, :x, :y, :w, :h #, :a
-
- def initialize(b2d, x, y, w, h, a)
- @box2d = b2d
- @x = x
- @y = y
- @w = w
- @h = h
-
- # Define the polygon
- sd = B2D::PolygonShape.new
-
- # Figure out the box2d coordinates
- box2dW = box2d.scalar_pixels_to_world(w/2)
- box2dH = box2d.scalar_pixels_to_world(h/2)
- # We're just a box
- sd.set_as_box(box2dW, box2dH)
-
-
- # Create the body
- bd = B2D::BodyDef.new
- bd.type = B2D::BodyType::STATIC
- bd.angle = a
- bd.position.set(box2d.coord_pixels_to_world(x,y))
- @b = box2d.create_body(bd)
-
- # Attached the shape to the body using a Fixture
- b.create_fixture(sd,1)
- end
-
- # Draw the boundary, it doesn't move so we don't have to ask the Body for location
- def display
- fill(0)
- stroke(0)
- stroke_weight(1)
- rect_mode(CENTER)
- a = b.get_angle
- push_matrix
- translate(x,y)
- rotate(-a)
- rect(0,0,w,h)
- pop_matrix
- end
-
- end
-end
-
-
-
Oops, something went wrong.

0 comments on commit 0141974

Please sign in to comment.