Skip to content

Commit 96c700b

Browse files
committed
bubble factory
1 parent b1e3095 commit 96c700b

3 files changed

Lines changed: 30 additions & 16 deletions

File tree

examples/blob/bubbles.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
require 'pbox2d'
1111
require 'forwardable'
1212
require_relative 'lib/boundary'
13+
require_relative 'lib/bubble_factory'
1314
require_relative 'lib/bubble'
1415

15-
Vect = Struct.new(:x, :y)
16-
attr_reader :boundaries, :box2d, :bubble, :bubble1, :bubble2
16+
17+
attr_reader :boundaries, :box2d, :bubble, :bubble1, :bubble2, :bubble_factory
1718

1819
def settings
1920
size(400, 400)
@@ -25,26 +26,27 @@ def setup
2526
@box2d = WorldBuilder.build(app: self, gravity: [0, 30])
2627
box2d.create_world
2728
@boundaries = []
28-
boundaries << Boundary.new(box2d, Vect.new(160, 135), Vect.new(20, height * 0.2))
29-
boundaries << Boundary.new(box2d, Vect.new(240, 135), Vect.new(20, height * 0.2))
29+
boundaries << Boundary.new(box2d, Vect.new(160, 135), Vect.new(20, height * 0.21))
30+
boundaries << Boundary.new(box2d, Vect.new(240, 135), Vect.new(20, height * 0.21))
3031
boundaries << Boundary.new(box2d, Vect.new(130, 190), Vect.new(80, 20), 0.5)
3132
boundaries << Boundary.new(box2d, Vect.new(270, 190), Vect.new(80, 20), -0.5)
3233
boundaries << Boundary.new(box2d, Vect.new(100, height * 0.9), Vect.new(20, height * 0.8))
3334
boundaries << Boundary.new(box2d, Vect.new(300, height * 0.9), Vect.new(20, height * 0.8))
34-
@bubble = Bubble.new(pos: Vect.new(rand(130..270), height - 20), hue: 255)
35-
@bubble1 = Bubble.new(pos: Vect.new(rand(130..270), height + 20), hue: 255)
36-
@bubble2 = Bubble.new(pos: Vect.new(rand(130..270), height + 60), hue: 255)
35+
@bubble_factory = BubbleFactory.new(xrange: (145..255))
36+
@bubble = bubble_factory.create_bubble(height: height - 20, hue: 255)
37+
@bubble1 = bubble_factory.create_bubble(height: height + 20, hue: 255)
38+
@bubble2 = bubble_factory.create_bubble(height: height + 60, hue: 255)
3739
end
3840

3941
def draw
4042
background(0, 0, 255)
41-
# We must always step through time!
43+
4244
boundaries.each(&:display)
4345
# Show the blobs!
44-
@bubble = Bubble.new(pos: Vect.new(rand(120..280), height - 20), hue: 255) if ((frame_count % 340) == 0)
46+
@bubble = bubble_factory.create_bubble(height: height - 20, hue: 255) if ((frame_count % 240) == 0)
4547
bubble.display
46-
@bubble1 = Bubble.new(pos: Vect.new(rand(120..280), height - 20), hue: 255) if ((frame_count % 440) == 0)
48+
@bubble1 = bubble_factory.create_bubble(height: height + 20, hue: 255) if ((frame_count % 280) == 0)
4749
bubble1.display
48-
@bubble2 = Bubble.new(pos: Vect.new(rand(120..280), height - 20), hue: 255) if ((frame_count % 540) == 0)
50+
@bubble2 = bubble_factory.create_bubble(height: height + 60, hue: 255) if ((frame_count % 320) == 0)
4951
bubble2.display
5052
end

examples/blob/lib/bubble.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ def initialize(pos:, hue:)
4545
# For filtering out collisions
4646
#fd.filter.groupIndex = -2
4747
# Parameters that affect physics
48-
fd.set_density(0.1)
49-
fd.set_restitution(0.05)
50-
fd.set_friction(1.0)
48+
fd.set_density(1.0)
49+
fd.set_restitution(0.5)
50+
fd.set_friction(0.5)
5151
# Finalize the body
5252
body.create_fixture(fd)
5353
# Add it to the volume
@@ -56,8 +56,8 @@ def initialize(pos:, hue:)
5656
outline << body
5757
end
5858
# These parameters control how stiff vs. jiggly the blob is
59-
cvjd.frequencyHz = 10.0
60-
cvjd.dampingRatio = 3.0
59+
cvjd.frequencyHz = 0.0
60+
cvjd.dampingRatio = 0.0
6161
# Put the joint thing in our world!
6262
box2d.world.create_joint(cvjd)
6363
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Vect = Struct.new(:x, :y)
2+
3+
class BubbleFactory
4+
attr_reader :xrange
5+
def initialize(xrange:)
6+
@xrange = xrange
7+
end
8+
9+
def create_bubble(hue:, height:)
10+
Bubble.new(pos: Vect.new(rand(xrange), height), hue: hue)
11+
end
12+
end

0 commit comments

Comments
 (0)