Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
monkstone committed Apr 15, 2015
1 parent fb00100 commit b03571c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
13 changes: 10 additions & 3 deletions chp5_physicslibraries/box2d/distance_joint/distance_joint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ def setup
end

def draw
background(255)
pairs.each(&:display)
background(255)
# Look at all pairs, in reverse order
pairs.reverse_each do |p|
p.display
# pairs that leave the screen, we delete them
# (note they have to be deleted from both the box2d world and our list
next unless p.done?
pairs.shift
end
# Display all the boundaries
boundaries.each(&:display)
boundaries.each(&:display)
fill(0)
text('Click mouse to add connected particles.', 10, 20)
end
Expand Down
22 changes: 21 additions & 1 deletion chp5_physicslibraries/box2d/distance_joint/pair.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Pair
extend Forwardable
def_delegators(:@app, :box2d, :stroke, :line, :stroke_weight)
attr_reader :p1, :p2, :len
attr_reader :p1, :p2, :len, :w, :h
# Chain constructor
def initialize(x, y)
@app = $app
Expand All @@ -27,6 +27,26 @@ def initialize(x, y)
# We might need to someday, but for now it's ok
box2d.world.create_joint(djd)
end

def kill_bodies
box2d.destroyBody(p1.body)
box2d.destroyBody(p2.body)
end

# Is the pair ready for deletion?
def done?
# Let's find the screen position of the particle
pos1 = box2d.body_coord(p1.body)
pos2 = box2d.body_coord(p2.body)
# Is it off the screen?
if (0..@app.width).include?(pos1.x) || (0..@app.width).include?(pos2.x)
if (0..@app.height).include?(pos1.y) || (0..@app.width).include?(pos2.y)
return false
end
end
kill_bodies
return true
end

def display
pos1 = box2d.body_coord(p1.body)
Expand Down

0 comments on commit b03571c

Please sign in to comment.