Skip to content

Commit 99e8b51

Browse files
author
monkstone
committed
refactor jbox2d examples
1 parent 692a891 commit 99e8b51

File tree

10 files changed

+295
-209
lines changed

10 files changed

+295
-209
lines changed

samples/external_library/ruby_gem/jbox2d/bumpy_surface_noise.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ def draw
3737
particles.reject!(&:done)
3838
# Just drawing the framerate to see how many particles it can handle
3939
fill(0)
40-
text("framerate: #{frame_rate.to_i}", 12, 16)
40+
text(format('framerate: %d', frame_rate), 12, 16)
4141
end
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
require 'pbox2d'
2+
require 'forwardable'
23
require_relative 'lib/custom_listener'
34
require_relative 'lib/particle'
45
require_relative 'lib/boundary'
56

67
attr_reader :box2d, :particles, :wall
78

9+
Vect = Struct.new(:x, :y)
10+
811
def setup
912
size 400, 400
1013
@box2d = Box2D.new(self)
1114
box2d.create_world
1215
box2d.add_listener(CustomListener.new)
1316
@particles = []
14-
@wall = Boundary.new(box2d, width / 2, height - 5, width, 10)
17+
@wall = Boundary.new(box2d, Vect.new(width / 2, height - 5), Vect.new(width, 10))
1518
end
1619

1720
def draw
1821
background(255)
19-
particles << Particle.new(box2d, rand(width), 20, rand(4..8)) if rand < 0.1
20-
particles.each{ |p| p.display(self) }
22+
particles << Particle.new(self, rand(width), 20, rand(4..8)) if rand < 0.1
23+
particles.each(&:display)
2124
particles.reject!(&:done)
22-
wall.display(self)
23-
end
25+
wall.display
26+
end
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# A Box class, note how to access class ParticleGroupDef in jruby
2-
# which is currently not on an included path for pbox2d
2+
# which is imported into PB module (not in global namespace)
33
class Box
44
attr_accessor :pg
55
def initialize(b2d, x, y)
@@ -8,8 +8,8 @@ def initialize(b2d, x, y)
88
shape = PolygonShape.new
99
pos = b2d.processing_to_world(x, y)
1010
shape.setAsBox(w, h, pos, 0)
11-
pd = Java::OrgJbox2dParticle::ParticleGroupDef.new
11+
pd = PB::ParticleGroupDef.new
1212
pd.shape = shape
1313
@pg = b2d.world.create_particle_group(pd)
1414
end
15-
end
15+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# A custom listener allows us to get the physics engine to
2+
# to call our code, on say contact (collisions)
3+
class CustomListener
4+
include ContactListener
5+
6+
def begin_contact(cp)
7+
# Get both fixtures
8+
f1 = cp.getFixtureA
9+
f2 = cp.getFixtureB
10+
# Get both bodies
11+
b1 = f1.getBody
12+
b2 = f2.getBody
13+
# Get our objects that reference these bodies
14+
o1 = b1.getUserData
15+
o2 = b2.getUserData
16+
return unless [o1, o2].all? { |obj| obj.respond_to?(:change) }
17+
o1.change
18+
o2.change
19+
end
20+
21+
def end_contact(_cp)
22+
end
23+
24+
def pre_solve(_cp, _m)
25+
end
26+
27+
def post_solve(_cp, _ci)
28+
end
29+
end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Note the particle class change method is use to change color to red
2+
# when two particles collide (no change just hitting boundary)
3+
class Particle
4+
extend Forwardable
5+
def_delegators(:@app, :box2d, :begin_shape, :end_shape, :line, :pop_matrix,
6+
:ellipse, :translate, :rotate, :stroke, :push_matrix,
7+
:fill, :no_fill, :stroke_weight)
8+
attr_accessor :body
9+
attr_reader :radius, :col
10+
11+
def initialize(app, x, y, r)
12+
@app, @x, @y, @radius = app, x, y, r
13+
# This function puts the particle in the Box2d world
14+
make_body(x, y, radius)
15+
@col = -5_263_441 # grey
16+
body.setUserData(self)
17+
end
18+
19+
# This function removes the particle from the box2d world
20+
def kill_body
21+
box2d.destroy_body(body)
22+
end
23+
24+
# Change color when hit
25+
def change
26+
@col = -65_536 # red
27+
end
28+
29+
# Is the particle ready for deletion?
30+
def done
31+
# Let's find the screen position of the particle
32+
pos = box2d.body_coord(body)
33+
# Is it off the bottom of the screen?
34+
return false unless pos.y > (box2d.height + radius * 2)
35+
kill_body
36+
true
37+
end
38+
39+
def display
40+
# We look at each body and get its screen position
41+
pos = box2d.body_coord(body)
42+
# Get its angle of rotation
43+
a = body.get_angle
44+
push_matrix
45+
translate(pos.x, pos.y)
46+
rotate(a)
47+
fill(col)
48+
stroke(0)
49+
stroke_weight(1)
50+
ellipse(0, 0, radius * 2, radius * 2)
51+
# Let's add a line so we can see the rotation
52+
line(0, 0, radius, 0)
53+
pop_matrix
54+
end
55+
56+
# Here's our function that adds the particle to the Box2D world
57+
def make_body(x, y, r)
58+
# Define a body
59+
bd = BodyDef.new
60+
# Set its position
61+
bd.position = box2d.processing_to_world(x, y)
62+
bd.type = BodyType::DYNAMIC
63+
@body = box2d.create_body(bd)
64+
# Make the body's shape a circle
65+
cs = CircleShape.new
66+
cs.m_radius = box2d.scale_to_world(r)
67+
fd = FixtureDef.new
68+
fd.shape = cs
69+
# Parameters that affect physics
70+
fd.density = 1
71+
fd.friction = 0.01
72+
fd.restitution = 0.3
73+
# Attach fixture to body
74+
body.create_fixture(fd)
75+
body.set_angular_velocity(rand(-10.0..10))
76+
end
77+
end

0 commit comments

Comments
 (0)