Skip to content

Commit 8e4c09b

Browse files
author
monkstone
committed
revise physics
1 parent dd5138a commit 8e4c09b

26 files changed

+494
-397
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### Source of Sketches
2+
3+
These sketches have been translated from 'vanilla processing sketches' by Dan Shiffman. The original sketches were designed to run with the Box2D-for-processing library which has a subtlely different interface from the pbox2d gem. There is probably scope for further refactoring of most of the sketches to make them more ruby-like (the mouse_joint example has perhaps travelled furthest from the original version).
4+
15
### java_args.txt
26

37
The `data` folder contains java_args.txt, which currently silently enables the OpenGl-based pipeline using `-Dsun.java2d.opengl=true` add any other java args here, use `True` instead of `true` to get a message.

samples/external_library/ruby_gem/jbox2d/bumpy_surface_noise.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def setup
1919
# Create the empty list
2020
@particles = []
2121
# Create the surface
22-
@surface = Surface.new(box2d)
22+
@surface = Surface.new(self)
2323
end
2424

2525
def draw
@@ -29,7 +29,7 @@ def draw
2929
# Draw the surface
3030
surface.display
3131
# NB ? reqd to call mouse_pressed value, else method gets called.
32-
particles << Particle.new(box2d, mouse_x, mouse_y, rand(2.0..6)) if mouse_pressed?
32+
particles << Particle.new(self, mouse_x, mouse_y, rand(2.0..6)) if mouse_pressed?
3333
# Draw all particles
3434
particles.each(&:display)
3535
# Particles that leave the screen, we delete them
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'pbox2d'
2+
require_relative 'lib/custom_listener'
3+
require_relative 'lib/particle'
4+
require_relative 'lib/boundary'
5+
6+
attr_reader :box2d, :particles, :wall
7+
8+
def setup
9+
size 400, 400
10+
@box2d = Box2D.new(self)
11+
box2d.create_world
12+
box2d.add_listener(CustomListener.new)
13+
@particles = []
14+
@wall = Boundary.new(box2d, width / 2, height - 5, width, 10)
15+
end
16+
17+
def draw
18+
background(255)
19+
particles << Particle.new(box2d, rand(width), 20, rand(4..8)) if rand < 0.1
20+
particles.each{ |p| p.display(self) }
21+
particles.reject!(&:done)
22+
wall.display(self)
23+
end
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
-Dsun.java2d.opengl=true
1+
-XX:CompileCommand=dontinline,org.jruby.runtime.invokedynamic.InvokeDynamicSupport::invocationFallback
2+

samples/external_library/ruby_gem/jbox2d/distance_joint/boundary.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
# The Nature of Code
22
# Daniel Shiffman
33
# http://natureofcode.com
4-
require 'forwardable'
54

65
# A fixed boundary class
76
class Boundary
87
extend Forwardable
98
def_delegators(:@app, :box2d, :rect_mode, :rect, :fill, :stroke)
109
# A boundary is a simple rectangle with x, y, width, and height
1110
attr_reader :x, :y, :w, :h
12-
11+
1312
def initialize(x, y, w, h)
1413
@x, @y, @w, @h = x, y, w, h
1514
@app = $app
1615
# Define the polygon
1716
sd = PolygonShape.new
1817
# Figure out the box2d coordinates
19-
box2dW = box2d.scale_to_world(w / 2)
20-
box2dH = box2d.scale_to_world(h / 2)
18+
box2dw = box2d.scale_to_world(w / 2)
19+
box2dh = box2d.scale_to_world(h / 2)
2120
# We're just a box
22-
sd.setAsBox(box2dW, box2dH)
21+
sd.setAsBox(box2dw, box2dh)
2322
# Create the body
2423
bd = BodyDef.new
2524
bd.type = BodyType::STATIC
26-
bd.position.set(box2d.processing_to_world(x,y))
25+
bd.position.set(box2d.processing_to_world(x, y))
2726
b = box2d.createBody(bd)
2827
# Attached the shape to the body using a Fixture
29-
b.createFixture(sd,1)
28+
b.create_fixture(sd, 1)
3029
end
3130

3231
# Draw the boundary, if it were at an angle we'd have to do something fancier

samples/external_library/ruby_gem/jbox2d/distance_joint/distance_joint.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
# Daniel Shiffman
33
# http://natureofcode.com
44

5-
# Example demonstrating distance joints
5+
# Example demonstrating distance joints
66
# A bridge is formed by connected a series of particles with joints
77

88
require 'pbox2d'
9+
require 'forwardable'
910
require_relative 'boundary'
1011
require_relative 'pair'
1112
require_relative 'particle'
13+
require_relative 'particle_system'
1214

13-
attr_reader :box2d, :boundaries, :pairs
15+
attr_reader :box2d, :boundaries, :system
1416

1517
def setup
1618
size(640, 360)
1719
# Initialize box2d physics and create the world
1820
@box2d = Box2D.new(self)
1921
box2d.create_world
20-
@pairs = []
22+
@system = ParticleSystem.new
2123
@boundaries = []
2224
# Add a bunch of fixed boundaries
2325
boundaries << Boundary.new(width / 4, height - 5, width / 2 - 50, 10)
@@ -26,13 +28,13 @@ def setup
2628

2729
def draw
2830
background(255)
29-
pairs.each(&:display)
31+
system.run
3032
# Display all the boundaries
3133
boundaries.each(&:display)
3234
fill(0)
3335
text('Click mouse to add connected particles.', 10, 20)
3436
end
3537

3638
def mouse_pressed
37-
pairs << Pair.new(mouse_x, mouse_y)
39+
system.add_pair(mouse_x, mouse_y)
3840
end

samples/external_library/ruby_gem/jbox2d/distance_joint/pair.rb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# The Nature of Code
22
# Daniel Shiffman
33
# http://natureofcode.com
4-
require 'forwardable'
54

65
# Series of Particles connected with distance joints
76
class Pair
87
extend Forwardable
98
def_delegators(:@app, :box2d, :stroke, :line, :stroke_weight)
10-
attr_reader :p1, :p2, :len
9+
attr_reader :p1, :p2, :len, :joint
1110
# Chain constructor
1211
def initialize(x, y)
1312
@app = $app
@@ -19,13 +18,34 @@ def initialize(x, y)
1918
djd.bodyA = p1.body
2019
djd.bodyB = p2.body
2120
# Equilibrium length
22-
djd.length = box2d.scale_to_world(len)
23-
# These properties affect how springy the joint is
21+
djd.length = box2d.scale_to_world(len)
22+
# These properties affect how springy the joint is
2423
djd.frequencyHz = 3 # Try a value less than 5 (0 for no elasticity)
2524
djd.dampingRatio = 0.1 # Ranges between 0 and 1 (1 for no springiness)
26-
# Make the joint. Note we aren't storing a reference to the joint ourselves anywhere!
27-
# We might need to someday, but for now it's ok
28-
box2d.world.create_joint(djd)
25+
# Make the joint.
26+
@joint = box2d.world.create_joint(djd)
27+
end
28+
29+
def kill_bodies
30+
box2d.world.destroy_joint(joint)
31+
@joint = nil
32+
box2d.destroy_body(p1.body)
33+
box2d.destroy_body(p2.body)
34+
end
35+
36+
# Is the pair ready for deletion?
37+
def done?
38+
# Let's find the screen position of the particle
39+
pos1 = box2d.body_coord(p1.body)
40+
pos2 = box2d.body_coord(p2.body)
41+
# Is it off the screen?
42+
if (0..@app.width).include?(pos1.x) || (0..@app.width).include?(pos2.x)
43+
if (0..@app.height).include?(pos1.y) || (0..@app.height).include?(pos2.y)
44+
return false
45+
end
46+
end
47+
kill_bodies
48+
true
2949
end
3050

3151
def display
@@ -38,4 +58,3 @@ def display
3858
p2.display
3959
end
4060
end
41-

samples/external_library/ruby_gem/jbox2d/distance_joint/particle.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# The Nature of Code
22
# Daniel Shiffman
33
# http://natureofcode.com
4-
require 'forwardable'
54

65
# A circular particle
76
class Particle
@@ -10,28 +9,28 @@ class Particle
109
:push_matrix, :pop_matrix, :ellipse, :rotate, :translate)
1110
# We need to keep track of a Body and a radius
1211
attr_reader :body, :r
13-
12+
1413
def initialize(x, y)
1514
@r = 8
1615
@app = $app
1716
# Define a body
1817
bd = BodyDef.new
1918
# Set its position
20-
bd.position = box2d.processing_to_world(x,y)
19+
bd.position = box2d.processing_to_world(x, y)
2120
bd.type = BodyType::DYNAMIC
2221
@body = box2d.world.createBody(bd)
2322

2423
# Make the body's shape a circle
2524
cs = CircleShape.new
2625
cs.m_radius = box2d.scale_to_world(r)
27-
26+
2827
fd = FixtureDef.new
2928
fd.shape = cs
3029
# Parameters that affect physics
3130
fd.density = 1
3231
fd.friction = 0.01
3332
fd.restitution = 0.3
34-
33+
3534
# Attach fixture to body
3635
body.createFixture(fd)
3736
body.setLinearVelocity(Vec2.new(rand(-5..5), rand(2..5)))
@@ -41,7 +40,7 @@ def initialize(x, y)
4140
def kill_body
4241
box2d.destroy_body(body)
4342
end
44-
43+
4544
# Is the particle ready for deletion?
4645
def done
4746
# Let's find the screen position of the particle
@@ -71,5 +70,3 @@ def display
7170
pop_matrix
7271
end
7372
end
74-
75-
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# run system with a single command
2+
module Runnable
3+
def run
4+
reject!(&:done?)
5+
each(&:display)
6+
end
7+
end
8+
9+
# A custom enumerable class, it is so easy in ruby
10+
class ParticleSystem
11+
include Enumerable, Runnable
12+
extend Forwardable
13+
def_delegators(:@pairs, :each, :reject!, :<<)
14+
15+
def initialize
16+
@pairs = []
17+
end
18+
19+
def add_pair(x, y)
20+
self << Pair.new(x, y)
21+
end
22+
end

samples/external_library/ruby_gem/jbox2d/lib/boundary.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
require 'forwardable'
2+
13
# The boundary class is used to create fixtures
24
class Boundary
3-
include Processing::Proxy
4-
attr_reader :box2d, :x, :y, :w, :h, :b
5-
def initialize(b2d, x, y, w, h)
6-
@box2d, @x, @y, @w, @h = b2d, x, y, w, h
5+
extend Forwardable
6+
def_delegators(:@app, :fill, :stroke, :rect, :rect_mode, :box2d)
7+
attr_reader :x, :y, :w, :h, :b
8+
def initialize(app, x, y, w, h)
9+
@app, @x, @y, @w, @h = app, x, y, w, h
710
sd = PolygonShape.new
811
box2d_w = box2d.scale_to_world(w / 2)
912
box2d_h = box2d.scale_to_world(h / 2)
@@ -21,7 +24,7 @@ def initialize(b2d, x, y, w, h)
2124
def display
2225
fill(0)
2326
stroke(0)
24-
rect_mode(CENTER)
27+
rect_mode(Java::ProcessingCore::PConstants::CENTER)
2528
rect(x, y, w, h)
2629
end
2730
end

0 commit comments

Comments
 (0)