Skip to content

Commit b1e3095

Browse files
committed
bubbles
1 parent 47a4832 commit b1e3095

5 files changed

Lines changed: 138 additions & 6 deletions

File tree

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
require 'pbox2d'
1313
require 'forwardable'
14-
require_relative 'boundary'
15-
require_relative 'blob'
14+
require_relative 'lib/boundary'
15+
require_relative 'lib/blob'
1616

1717
attr_reader :boundaries, :blob, :box2d
1818

19+
Vect = Struct.new(:x, :y)
20+
1921
def settings
2022
size(400, 300)
2123
end
@@ -26,10 +28,10 @@ def setup
2628
@box2d = WorldBuilder.build(app: self, gravity: [0, -20])
2729
box2d.create_world
2830
@boundaries = []
29-
boundaries << Boundary.new(box2d, Vec2D.new(width / 2, height - 5), Vec2D.new(width, 10))
30-
boundaries << Boundary.new(box2d, Vec2D.new(width / 2, 5), Vec2D.new(width, 10))
31-
boundaries << Boundary.new(box2d, Vec2D.new(width - 5, height / 2), Vec2D.new(10, height))
32-
boundaries << Boundary.new(box2d, Vec2D.new(5, height / 2), Vec2D.new(10, height))
31+
boundaries << Boundary.new(box2d, Vect.new(width / 2, height - 5), Vect.new(width, 10))
32+
boundaries << Boundary.new(box2d, Vect.new(width / 2, 5), Vect.new(width, 10))
33+
boundaries << Boundary.new(box2d, Vect.new(width - 5, height / 2), Vect.new(10, height))
34+
boundaries << Boundary.new(box2d, Vect.new(5, height / 2), Vect.new(10, height))
3335
# Make a new blob
3436
@blob = Blob.new
3537
end

examples/blob/bubbles.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# The Nature of Code
2+
# <http://www.shiffman.net/teaching/nature>
3+
# Spring 2012
4+
# Box2DProcessing example
5+
6+
# A blob skeleton
7+
# Could be used to create blobbly characters a la Nokia Friends
8+
# http://postspectacular.com/work/nokia/friends/start
9+
10+
require 'pbox2d'
11+
require 'forwardable'
12+
require_relative 'lib/boundary'
13+
require_relative 'lib/bubble'
14+
15+
Vect = Struct.new(:x, :y)
16+
attr_reader :boundaries, :box2d, :bubble, :bubble1, :bubble2
17+
18+
def settings
19+
size(400, 400)
20+
end
21+
22+
def setup
23+
sketch_title 'Bubbles'
24+
# Initialize box2d physics and create the world
25+
@box2d = WorldBuilder.build(app: self, gravity: [0, 30])
26+
box2d.create_world
27+
@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))
30+
boundaries << Boundary.new(box2d, Vect.new(130, 190), Vect.new(80, 20), 0.5)
31+
boundaries << Boundary.new(box2d, Vect.new(270, 190), Vect.new(80, 20), -0.5)
32+
boundaries << Boundary.new(box2d, Vect.new(100, height * 0.9), Vect.new(20, height * 0.8))
33+
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)
37+
end
38+
39+
def draw
40+
background(0, 0, 255)
41+
# We must always step through time!
42+
boundaries.each(&:display)
43+
# Show the blobs!
44+
@bubble = Bubble.new(pos: Vect.new(rand(120..280), height - 20), hue: 255) if ((frame_count % 340) == 0)
45+
bubble.display
46+
@bubble1 = Bubble.new(pos: Vect.new(rand(120..280), height - 20), hue: 255) if ((frame_count % 440) == 0)
47+
bubble1.display
48+
@bubble2 = Bubble.new(pos: Vect.new(rand(120..280), height - 20), hue: 255) if ((frame_count % 540) == 0)
49+
bubble2.display
50+
end

examples/blob/lib/bubble.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# The Nature of Code
2+
# <http://www.shiffman.net/teaching/nature>
3+
# Spring 2012
4+
# Box2DProcessing example
5+
6+
# A blob skeleton
7+
# Could be used to create blobbly characters a la Nokia Friends
8+
# http://postspectacular.com/work/nokia/friends/start
9+
10+
class Bubble
11+
include Processing::Proxy
12+
# A list to keep track of all the points in our blob
13+
attr_reader :outline, :radius, :total_points, :hue
14+
# We should modify this constructor to receive arguments
15+
# So that we can make many different types of blobs
16+
17+
def initialize(pos:, hue:)
18+
# Create the empty outline
19+
@outline = []
20+
@hue = hue
21+
# Let's make a volume of joints!
22+
cvjd = ConstantVolumeJointDef.new
23+
# Where and how big is the blob
24+
center = Vec2.new(pos.x, pos.y)
25+
@total_points = 150
26+
@radius = 25
27+
# Initialize all the points
28+
total_points.times do |i|
29+
# Look polar to cartesian coordinate transformation!
30+
theta = map1d(i, 0..total_points, 0..TWO_PI)
31+
x = center.x + radius * sin(theta)
32+
y = center.y + radius * cos(theta)
33+
# Make each individual body
34+
bd = BodyDef.new
35+
bd.type = BodyType::DYNAMIC
36+
bd.fixedRotation = true # no rotation!
37+
bd.position.set(box2d.processing_to_world(Vec2.new(x, y)))
38+
body = box2d.createBody(bd)
39+
# The body is a circle
40+
cs = CircleShape.new
41+
cs.m_radius = box2d.scale_to_world(0)
42+
# Define a fixture
43+
fd = FixtureDef.new
44+
fd.shape = cs
45+
# For filtering out collisions
46+
#fd.filter.groupIndex = -2
47+
# Parameters that affect physics
48+
fd.set_density(0.1)
49+
fd.set_restitution(0.05)
50+
fd.set_friction(1.0)
51+
# Finalize the body
52+
body.create_fixture(fd)
53+
# Add it to the volume
54+
cvjd.add_body(body)
55+
# Store our own copy for later rendering
56+
outline << body
57+
end
58+
# These parameters control how stiff vs. jiggly the blob is
59+
cvjd.frequencyHz = 10.0
60+
cvjd.dampingRatio = 3.0
61+
# Put the joint thing in our world!
62+
box2d.world.create_joint(cvjd)
63+
end
64+
65+
# Time to draw the blob!
66+
# Can you make it a cute character, a la
67+
# http://postspectacular.com/work/nokia/friends/start
68+
def display
69+
# Draw the outline
70+
begin_shape
71+
fill(hue)
72+
stroke(0, 0, 255)
73+
stroke_weight(0)
74+
outline.each do |b|
75+
pos = box2d.body_coord(b)
76+
vertex(pos.x, pos.y)
77+
end
78+
end_shape(CLOSE)
79+
end
80+
end

0 commit comments

Comments
 (0)