pbox2d 0.4.1-java → 0.4.2-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/examples/bumpy_surface_noise.rb +1 -1
- data/examples/distance_joint/boundary.rb +6 -7
- data/examples/distance_joint/distance_joint.rb +11 -9
- data/examples/distance_joint/pair.rb +27 -8
- data/examples/distance_joint/particle.rb +5 -8
- data/examples/distance_joint/particle_system.rb +22 -0
- data/examples/lib/boundary.rb +1 -3
- data/examples/lib/box.rb +4 -3
- data/examples/lib/custom_shape.rb +72 -0
- data/examples/lib/particle_system.rb +3 -7
- data/examples/lib/shape_system.rb +23 -0
- data/examples/liquid_fun_test.rb +4 -3
- data/examples/liquidy.rb +11 -11
- data/examples/mouse_joint/dummy_spring.rb +10 -10
- data/examples/mouse_joint/mouse_joint.rb +8 -7
- data/examples/mouse_joint/spring.rb +1 -0
- data/examples/polygons.rb +7 -6
- data/examples/quick_test.rb +4 -4
- data/examples/revolute_joint/particle_system.rb +24 -0
- data/examples/revolute_joint/revolute_joint.rb +6 -15
- data/examples/test_contact/lib/particle.rb +3 -3
- data/examples/test_contact/test_contact.rb +1 -2
- data/lib/box2d.jar +0 -0
- data/lib/pbox2d.rb +8 -1
- data/lib/pbox2d/version.rb +1 -1
- metadata +19 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ff8899781cd0308635cb95b3f78b9e3789d345d
|
4
|
+
data.tar.gz: 37b9df883bb1730e4f7129aebe65acd7c8b6a86e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c6218dacd978b3c530fea1d4df318d6281c4e265a4140c9bc0a376487dbd9f76e1e8aedd28d06736b40d59c00da33421030e1ebe765431c102eeaeccccc26f1
|
7
|
+
data.tar.gz: 6ed60e48763528a00bdad9f0d6a93e49dfa8b5802d3acac0293bf49c13dd84161930f5bc37fad65efa4786acc5c536db3ce8ee5930131c89eaa6657fc822cb2d
|
data/README.md
CHANGED
@@ -30,7 +30,7 @@ Copyright (c) 2014 Daniel Shiffman
|
|
30
30
|
|
31
31
|
This gem was created by Martin Prout
|
32
32
|
|
33
|
-
Copyright (c) 2014 Martin Prout
|
33
|
+
Copyright (c) 2014-2015 Martin Prout
|
34
34
|
|
35
35
|
### To compile
|
36
36
|
|
@@ -60,6 +60,6 @@ The other thing you should know is there is a mismatch between the physics world
|
|
60
60
|
[Nature of Code book]:http://natureofcode.com/
|
61
61
|
[Sandi Metz]:http://www.poodr.com/
|
62
62
|
[this book]:http://www.crcpress.com/product/isbn/9781466565760
|
63
|
-
[zip]:https://github.com/ruby-processing/jbox2d/archive/0.
|
64
|
-
[tar]:https://github.com/ruby-processing/jbox2d/archive/0.
|
63
|
+
[zip]:https://github.com/ruby-processing/jbox2d/archive/0.4.2.zip
|
64
|
+
[tar]:https://github.com/ruby-processing/jbox2d/archive/0.4.2.tar.gz
|
65
65
|
[Wiki]:https://github.com/ruby-processing/jbox2d/wiki
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# The Nature of Code
|
2
2
|
# Daniel Shiffman
|
3
3
|
# http://natureofcode.com
|
4
|
-
require 'forwardable'
|
5
4
|
|
6
5
|
# A fixed boundary class
|
7
6
|
class Boundary
|
@@ -9,24 +8,24 @@ class Boundary
|
|
9
8
|
def_delegators(:@app, :box2d, :rect_mode, :rect, :fill, :stroke)
|
10
9
|
# A boundary is a simple rectangle with x, y, width, and height
|
11
10
|
attr_reader :x, :y, :w, :h
|
12
|
-
|
11
|
+
|
13
12
|
def initialize(x, y, w, h)
|
14
13
|
@x, @y, @w, @h = x, y, w, h
|
15
14
|
@app = $app
|
16
15
|
# Define the polygon
|
17
16
|
sd = PolygonShape.new
|
18
17
|
# Figure out the box2d coordinates
|
19
|
-
|
20
|
-
|
18
|
+
box2dw = box2d.scale_to_world(w / 2)
|
19
|
+
box2dh = box2d.scale_to_world(h / 2)
|
21
20
|
# We're just a box
|
22
|
-
sd.setAsBox(
|
21
|
+
sd.setAsBox(box2dw, box2dh)
|
23
22
|
# Create the body
|
24
23
|
bd = BodyDef.new
|
25
24
|
bd.type = BodyType::STATIC
|
26
|
-
bd.position.set(box2d.processing_to_world(x,y))
|
25
|
+
bd.position.set(box2d.processing_to_world(x, y))
|
27
26
|
b = box2d.createBody(bd)
|
28
27
|
# Attached the shape to the body using a Fixture
|
29
|
-
b.
|
28
|
+
b.create_fixture(sd, 1)
|
30
29
|
end
|
31
30
|
|
32
31
|
# Draw the boundary, if it were at an angle we'd have to do something fancier
|
@@ -2,31 +2,33 @@
|
|
2
2
|
# Daniel Shiffman
|
3
3
|
# http://natureofcode.com
|
4
4
|
|
5
|
-
# Example demonstrating distance joints
|
5
|
+
# Example demonstrating distance joints
|
6
6
|
# A bridge is formed by connected a series of particles with joints
|
7
7
|
|
8
8
|
require 'pbox2d'
|
9
|
+
require 'forwardable'
|
9
10
|
require_relative 'boundary'
|
10
11
|
require_relative 'pair'
|
11
12
|
require_relative 'particle'
|
13
|
+
require_relative 'particle_system'
|
12
14
|
|
13
|
-
attr_reader :box2d, :boundaries, :
|
15
|
+
attr_reader :box2d, :boundaries, :system
|
14
16
|
|
15
17
|
def setup
|
16
18
|
size(640, 360)
|
17
19
|
# Initialize box2d physics and create the world
|
18
20
|
@box2d = Box2D.new(self)
|
19
21
|
box2d.create_world
|
20
|
-
@
|
21
|
-
@boundaries = [
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
@system = ParticleSystem.new
|
23
|
+
@boundaries = [
|
24
|
+
Boundary.new(width / 4, height - 5, width / 2 - 50, 10),
|
25
|
+
Boundary.new(3 * width / 4, height - 50, width / 2 - 50, 10)
|
26
|
+
]
|
25
27
|
end
|
26
28
|
|
27
29
|
def draw
|
28
30
|
background(255)
|
29
|
-
|
31
|
+
system.run
|
30
32
|
# Display all the boundaries
|
31
33
|
boundaries.each(&:display)
|
32
34
|
fill(0)
|
@@ -34,5 +36,5 @@ def draw
|
|
34
36
|
end
|
35
37
|
|
36
38
|
def mouse_pressed
|
37
|
-
|
39
|
+
system.add_pair(mouse_x, mouse_y)
|
38
40
|
end
|
@@ -1,13 +1,12 @@
|
|
1
1
|
# The Nature of Code
|
2
2
|
# Daniel Shiffman
|
3
3
|
# http://natureofcode.com
|
4
|
-
require 'forwardable'
|
5
4
|
|
6
5
|
# Series of Particles connected with distance joints
|
7
6
|
class Pair
|
8
7
|
extend Forwardable
|
9
8
|
def_delegators(:@app, :box2d, :stroke, :line, :stroke_weight)
|
10
|
-
attr_reader :p1, :p2, :len
|
9
|
+
attr_reader :p1, :p2, :len, :joint
|
11
10
|
# Chain constructor
|
12
11
|
def initialize(x, y)
|
13
12
|
@app = $app
|
@@ -19,13 +18,34 @@ class Pair
|
|
19
18
|
djd.bodyA = p1.body
|
20
19
|
djd.bodyB = p2.body
|
21
20
|
# 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
|
24
23
|
djd.frequencyHz = 3 # Try a value less than 5 (0 for no elasticity)
|
25
24
|
djd.dampingRatio = 0.1 # Ranges between 0 and 1 (1 for no springiness)
|
26
|
-
# Make the joint.
|
27
|
-
|
28
|
-
|
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
|
29
49
|
end
|
30
50
|
|
31
51
|
def display
|
@@ -38,4 +58,3 @@ class Pair
|
|
38
58
|
p2.display
|
39
59
|
end
|
40
60
|
end
|
41
|
-
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# The Nature of Code
|
2
2
|
# Daniel Shiffman
|
3
3
|
# http://natureofcode.com
|
4
|
-
require 'forwardable'
|
5
4
|
|
6
5
|
# A circular particle
|
7
6
|
class Particle
|
@@ -10,28 +9,28 @@ class Particle
|
|
10
9
|
:push_matrix, :pop_matrix, :ellipse, :rotate, :translate)
|
11
10
|
# We need to keep track of a Body and a radius
|
12
11
|
attr_reader :body, :r
|
13
|
-
|
12
|
+
|
14
13
|
def initialize(x, y)
|
15
14
|
@r = 8
|
16
15
|
@app = $app
|
17
16
|
# Define a body
|
18
17
|
bd = BodyDef.new
|
19
18
|
# Set its position
|
20
|
-
bd.position = box2d.processing_to_world(x,y)
|
19
|
+
bd.position = box2d.processing_to_world(x, y)
|
21
20
|
bd.type = BodyType::DYNAMIC
|
22
21
|
@body = box2d.world.createBody(bd)
|
23
22
|
|
24
23
|
# Make the body's shape a circle
|
25
24
|
cs = CircleShape.new
|
26
25
|
cs.m_radius = box2d.scale_to_world(r)
|
27
|
-
|
26
|
+
|
28
27
|
fd = FixtureDef.new
|
29
28
|
fd.shape = cs
|
30
29
|
# Parameters that affect physics
|
31
30
|
fd.density = 1
|
32
31
|
fd.friction = 0.01
|
33
32
|
fd.restitution = 0.3
|
34
|
-
|
33
|
+
|
35
34
|
# Attach fixture to body
|
36
35
|
body.createFixture(fd)
|
37
36
|
body.setLinearVelocity(Vec2.new(rand(-5..5), rand(2..5)))
|
@@ -41,7 +40,7 @@ class Particle
|
|
41
40
|
def kill_body
|
42
41
|
box2d.destroy_body(body)
|
43
42
|
end
|
44
|
-
|
43
|
+
|
45
44
|
# Is the particle ready for deletion?
|
46
45
|
def done
|
47
46
|
# Let's find the screen position of the particle
|
@@ -71,5 +70,3 @@ class Particle
|
|
71
70
|
pop_matrix
|
72
71
|
end
|
73
72
|
end
|
74
|
-
|
75
|
-
|
@@ -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
|
data/examples/lib/boundary.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
|
3
1
|
# The boundary class is used to create fixtures
|
4
2
|
class Boundary
|
5
3
|
extend Forwardable
|
6
4
|
def_delegators(:@app, :fill, :stroke, :rect, :rect_mode, :box2d)
|
7
|
-
attr_reader :x, :y, :w, :h
|
5
|
+
attr_reader :pos, :size, :b, :x, :y, :w, :h
|
8
6
|
def initialize(app, x, y, w, h)
|
9
7
|
@app, @x, @y, @w, @h = app, x, y, w, h
|
10
8
|
sd = PolygonShape.new
|
data/examples/lib/box.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# A Box class, note how to access class ParticleGroupDef in jruby
|
2
|
-
# which is
|
2
|
+
# which is imported into the PB module and not into global namespace
|
3
|
+
# mainly because of probable name conflict with ParticleSystem
|
3
4
|
class Box
|
4
5
|
attr_accessor :pg
|
5
6
|
def initialize(b2d, x, y)
|
@@ -8,8 +9,8 @@ class Box
|
|
8
9
|
shape = PolygonShape.new
|
9
10
|
pos = b2d.processing_to_world(x, y)
|
10
11
|
shape.setAsBox(w, h, pos, 0)
|
11
|
-
pd =
|
12
|
+
pd = PB::ParticleGroupDef.new
|
12
13
|
pd.shape = shape
|
13
14
|
@pg = b2d.world.create_particle_group(pd)
|
14
15
|
end
|
15
|
-
end
|
16
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
class CustomShape
|
2
|
+
include Processing::Proxy
|
3
|
+
# We need to keep track of a Body and a width and height
|
4
|
+
attr_reader :body, :box2d
|
5
|
+
|
6
|
+
# Constructor
|
7
|
+
def initialize(app, x, y)
|
8
|
+
# Add the box to the box2d world
|
9
|
+
@box2d = app.box2d
|
10
|
+
make_body(Vec2.new(x, y))
|
11
|
+
end
|
12
|
+
|
13
|
+
# This function removes the particle from the box2d world
|
14
|
+
def kill_body!
|
15
|
+
box2d.destroy_body(body)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Is the particle ready for deletion?
|
19
|
+
def done
|
20
|
+
# Let's find the screen position of the particle
|
21
|
+
pos = box2d.body_coord(body)
|
22
|
+
# Is it off the bottom of the screen?
|
23
|
+
return false unless pos.y > box2d.height
|
24
|
+
kill_body!
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
# Drawing the box
|
29
|
+
def display
|
30
|
+
# We look at each body and get its screen position
|
31
|
+
pos = box2d.body_coord(body)
|
32
|
+
# Get its angle of rotation
|
33
|
+
a = body.get_angle
|
34
|
+
f = body.get_fixture_list
|
35
|
+
ps = f.get_shape
|
36
|
+
rect_mode(CENTER)
|
37
|
+
push_matrix
|
38
|
+
translate(pos.x, pos.y)
|
39
|
+
rotate(-a)
|
40
|
+
fill(175)
|
41
|
+
stroke(0)
|
42
|
+
begin_shape
|
43
|
+
# For every vertex, convert to pixel vector
|
44
|
+
ps.get_vertex_count.times do |i|
|
45
|
+
v = box2d.vector_to_processing(ps.get_vertex(i))
|
46
|
+
vertex(v.x, v.y)
|
47
|
+
end
|
48
|
+
end_shape(CLOSE)
|
49
|
+
pop_matrix
|
50
|
+
end
|
51
|
+
|
52
|
+
# This function adds the rectangle to the box2d world
|
53
|
+
def make_body(center)
|
54
|
+
# Define a polygon (this is what we use for a rectangle)
|
55
|
+
sd = PolygonShape.new
|
56
|
+
vertices = []
|
57
|
+
vertices << box2d.vector_to_world(Vec2.new(-15, 25))
|
58
|
+
vertices << box2d.vector_to_world(Vec2.new(15, 0))
|
59
|
+
vertices << box2d.vector_to_world(Vec2.new(20, -15))
|
60
|
+
vertices << box2d.vector_to_world(Vec2.new(-10, -10))
|
61
|
+
sd.set(vertices.to_java(Java::OrgJbox2dCommon::Vec2), vertices.length)
|
62
|
+
# Define the body and make it from the shape
|
63
|
+
bd = BodyDef.new
|
64
|
+
bd.type = BodyType::DYNAMIC
|
65
|
+
bd.position.set(box2d.processing_to_world(center))
|
66
|
+
@body = box2d.create_body(bd)
|
67
|
+
body.create_fixture(sd, 1.0)
|
68
|
+
# Give it some initial random velocity
|
69
|
+
body.set_linear_velocity(Vec2.new(rand(-5.0..5), rand(2.0..5)))
|
70
|
+
body.set_angular_velocity(rand(-5.0..5))
|
71
|
+
end
|
72
|
+
end
|
@@ -16,17 +16,13 @@ class ParticleSystem
|
|
16
16
|
attr_reader :x, :y
|
17
17
|
|
18
18
|
def initialize(app, num, x, y)
|
19
|
-
@particles = [] # Initialize the Array
|
20
19
|
@x, @y = x, y # Store the origin point
|
21
|
-
|
22
|
-
|
23
|
-
end
|
20
|
+
# initialize array with some particles
|
21
|
+
@particles = Array.new(num, Particle.new(app, x, y))
|
24
22
|
end
|
25
23
|
|
26
24
|
def add_particles(app, n)
|
27
|
-
n.times
|
28
|
-
self << Particle.new(app, x, y)
|
29
|
-
end
|
25
|
+
n.times { self << Particle.new(app, x, y) }
|
30
26
|
end
|
31
27
|
end
|
32
28
|
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Runnable
|
2
|
+
def run
|
3
|
+
reject! { |item| item.done? }
|
4
|
+
each { |item| item.display }
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class ShapeSystem
|
9
|
+
include Enumerable, Runnable
|
10
|
+
extend Forwardable
|
11
|
+
def_delegators(:@polygons, :each, :reject!, :<<)
|
12
|
+
|
13
|
+
attr_reader :bd
|
14
|
+
|
15
|
+
def initialize(bd)
|
16
|
+
@bd = bd
|
17
|
+
@polygons = [] # Initialize the Array
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_polygon(x, y)
|
21
|
+
self << CustomShape.new(bd, x, y)
|
22
|
+
end
|
23
|
+
end
|
data/examples/liquid_fun_test.rb
CHANGED
@@ -14,12 +14,13 @@ def setup
|
|
14
14
|
@box2d = Box2D.new(self)
|
15
15
|
box2d.init_options(gravity: [0, -10])
|
16
16
|
box2d.create_world
|
17
|
-
@boundaries = []
|
18
17
|
@boxes = []
|
19
18
|
box2d.world.set_particle_radius(0.15)
|
20
19
|
box2d.world.set_particle_damping(0.2)
|
21
|
-
boundaries
|
22
|
-
|
20
|
+
@boundaries = [
|
21
|
+
Boundary.new(self, width / 4, height - 5, width / 2 - 50, 10),
|
22
|
+
Boundary.new(self, 3 * width / 4, height - 50, width / 2 - 50, 10)
|
23
|
+
]
|
23
24
|
end
|
24
25
|
|
25
26
|
def mouse_pressed
|
data/examples/liquidy.rb
CHANGED
@@ -11,23 +11,23 @@ def setup
|
|
11
11
|
# box2d.gravity([0, -20])
|
12
12
|
# Create Arrays
|
13
13
|
@systems = []
|
14
|
-
@boundaries = [
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
@boundaries = [
|
15
|
+
Boundary.new(self, 50, 100, 5, -0.3),
|
16
|
+
Boundary.new(self, 250, 175, 5, 0.5)
|
17
|
+
]
|
18
18
|
end
|
19
19
|
|
20
20
|
def draw
|
21
21
|
background(255)
|
22
|
-
# Run all the particle systems
|
23
|
-
if systems.size > 0
|
24
|
-
systems.each do |system|
|
25
|
-
system.run
|
26
|
-
system.add_particles(self, rand(0..2))
|
27
|
-
end
|
28
|
-
end
|
29
22
|
# Display all the boundaries
|
30
23
|
boundaries.each(&:display)
|
24
|
+
# Run all the particle systems
|
25
|
+
return unless systems.size > 0
|
26
|
+
systems.each do |system|
|
27
|
+
system.run
|
28
|
+
# refresh particles (else we'll run out)
|
29
|
+
system.add_particles(self, rand(0..2))
|
30
|
+
end
|
31
31
|
end
|
32
32
|
|
33
33
|
def mouse_pressed
|
@@ -1,19 +1,19 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
# http://natureofcode.com
|
1
|
+
# dummy_spring.rb by Martin Prout
|
2
|
+
# An example of duck-typing in ruby-processing
|
4
3
|
|
5
|
-
# This class avoids
|
4
|
+
# This class avoids the tests for null seen in vanilla processing version
|
6
5
|
class DummySpring
|
7
6
|
def initialize; end
|
8
|
-
|
9
|
-
# If it exists we set its target to the mouse location
|
10
7
|
def update(_x, _y); end
|
11
|
-
|
12
8
|
def display; end
|
13
|
-
|
9
|
+
|
14
10
|
# This is the key function where
|
15
|
-
# we attach
|
16
|
-
# and the
|
11
|
+
# we attach a real spring between an x, y location
|
12
|
+
# and the box object's location
|
13
|
+
# @param x (will be mouse_x)
|
14
|
+
# @param y (will be mouse_y)
|
15
|
+
# @param box Box
|
16
|
+
# @return new bound Spring
|
17
17
|
def bind(x, y, box)
|
18
18
|
spring = Spring.new
|
19
19
|
spring.bind(x, y, box)
|
@@ -21,14 +21,15 @@ def setup
|
|
21
21
|
box2d.create_world
|
22
22
|
# Make the box
|
23
23
|
@box = Box.new(width / 2, height / 2)
|
24
|
-
# Make a dummy spring
|
24
|
+
# Make a dummy spring, that returns a real spring on :bind
|
25
25
|
@spring = DummySpring.new
|
26
|
-
#
|
27
|
-
@boundaries = [
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
# Create a bunch of fixed boundaries
|
27
|
+
@boundaries = [
|
28
|
+
Boundary.new(width / 2, height - 5, width, 10, 0),
|
29
|
+
Boundary.new(width / 2, 5, width, 10, 0),
|
30
|
+
Boundary.new(width - 5, height / 2, 10, height, 0),
|
31
|
+
Boundary.new(5, height / 2, 10, height, 0)
|
32
|
+
]
|
32
33
|
end
|
33
34
|
|
34
35
|
# When the mouse is released we're done with the spring
|
data/examples/polygons.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Basic example of falling rectangles
|
2
2
|
require 'pbox2d'
|
3
3
|
require_relative 'lib/custom_shape'
|
4
|
+
require_relative 'lib/boundary'
|
4
5
|
|
5
6
|
attr_reader :box2d, :boundaries, :polygons
|
6
7
|
|
@@ -15,12 +16,12 @@ def setup
|
|
15
16
|
# box2d.gravity([0, -20]
|
16
17
|
# Create Arrays
|
17
18
|
@polygons = []
|
18
|
-
@boundaries = [
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
@boundaries = [
|
20
|
+
Boundary.new(self, width / 4, height - 5, width / 2 - 50, 10),
|
21
|
+
Boundary.new(self, 3 * width / 4, height - 50, width / 2 - 50, 10),
|
22
|
+
Boundary.new(self, width - 5, height / 2, 10, height),
|
23
|
+
Boundary.new(self, 5, height / 2, 10, height)
|
24
|
+
]
|
24
25
|
end
|
25
26
|
|
26
27
|
def draw
|
data/examples/quick_test.rb
CHANGED
@@ -19,10 +19,10 @@ def setup
|
|
19
19
|
# box2d.gravity(0, -20)
|
20
20
|
# Create ArrayLists
|
21
21
|
@boxes = []
|
22
|
-
@boundaries = [
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
@boundaries = [
|
23
|
+
Boundary.new(self, width / 4, height - 5, width / 2 - 50, 10),
|
24
|
+
Boundary.new(self, 3 * width / 4, height - 50, width / 2 - 50, 10)
|
25
|
+
]
|
26
26
|
end
|
27
27
|
|
28
28
|
def draw
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Runnable
|
2
|
+
def run
|
3
|
+
reject!(&:done?)
|
4
|
+
each(&:display)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class ParticleSystem
|
9
|
+
include Enumerable, Runnable
|
10
|
+
extend Forwardable
|
11
|
+
def_delegators(:@particles, :each, :reject!, :<<)
|
12
|
+
|
13
|
+
attr_reader :particles
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@particles = [] # Initialize the Array
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_particles(width)
|
20
|
+
return unless rand < 0.1
|
21
|
+
sz = rand(4.0..8)
|
22
|
+
particles << Particle.new(rand(width / 2 - 100..width / 2 + 100), -20, sz)
|
23
|
+
end
|
24
|
+
end
|
@@ -6,15 +6,16 @@
|
|
6
6
|
require 'pbox2d'
|
7
7
|
require_relative 'windmill'
|
8
8
|
require_relative 'particle'
|
9
|
+
require_relative 'particle_system'
|
9
10
|
|
10
|
-
attr_reader :box2d, :windmill, :
|
11
|
+
attr_reader :box2d, :windmill, :system
|
11
12
|
|
12
13
|
def setup
|
13
14
|
size(640,360)
|
14
15
|
@box2d = Box2D.new(self)
|
15
16
|
box2d.createWorld
|
16
17
|
@windmill = Windmill.new(width / 2, 175)
|
17
|
-
@
|
18
|
+
@system = ParticleSystem.new
|
18
19
|
end
|
19
20
|
|
20
21
|
# Click the mouse to turn on or off the motor
|
@@ -24,21 +25,11 @@ end
|
|
24
25
|
|
25
26
|
def draw
|
26
27
|
background(255)
|
27
|
-
|
28
|
-
|
29
|
-
particles << Particle.new(rand(width / 2 - 100..width / 2 + 100), -20, sz)
|
30
|
-
end
|
31
|
-
# Look at all particles, in reverse order
|
32
|
-
particles.reverse_each do |p|
|
33
|
-
p.display
|
34
|
-
# Particles that leave the screen, we delete them
|
35
|
-
# (note they have to be deleted from both the box2d world and our list
|
36
|
-
next unless p.done?
|
37
|
-
particles.shift
|
38
|
-
end
|
28
|
+
system.add_particles(width)
|
29
|
+
system.run
|
39
30
|
# Draw the windmill
|
40
31
|
windmill.display
|
41
|
-
status = windmill.motor_on? ?
|
32
|
+
status = windmill.motor_on? ? 'ON' : 'OFF'
|
42
33
|
fill(0)
|
43
34
|
text(format("Click mouse to toggle motor.\nMotor: %s", status), 10, height - 30)
|
44
35
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
# when two particles collide (no change just hitting boundary)
|
3
3
|
class Particle
|
4
4
|
extend Forwardable
|
5
|
-
def_delegators(:@app, :box2d, :begin_shape, :end_shape, :line, :pop_matrix,
|
5
|
+
def_delegators(:@app, :box2d, :begin_shape, :color, :end_shape, :line, :pop_matrix,
|
6
6
|
:ellipse, :translate, :rotate, :stroke, :push_matrix,
|
7
7
|
:fill, :no_fill, :stroke_weight)
|
8
8
|
attr_accessor :body
|
@@ -12,7 +12,7 @@ class Particle
|
|
12
12
|
@app, @x, @y, @radius = app, x, y, r
|
13
13
|
# This function puts the particle in the Box2d world
|
14
14
|
make_body(x, y, radius)
|
15
|
-
@col =
|
15
|
+
@col = color('#c0c0c0') # silver
|
16
16
|
body.setUserData(self)
|
17
17
|
end
|
18
18
|
|
@@ -23,7 +23,7 @@ class Particle
|
|
23
23
|
|
24
24
|
# Change color when hit
|
25
25
|
def change
|
26
|
-
@col =
|
26
|
+
@col = color('#cc0000') # red
|
27
27
|
end
|
28
28
|
|
29
29
|
# Is the particle ready for deletion?
|
data/lib/box2d.jar
CHANGED
Binary file
|
data/lib/pbox2d.rb
CHANGED
@@ -7,7 +7,7 @@ end
|
|
7
7
|
ContactListener = Java::OrgJbox2dCallbacks::ContactListener
|
8
8
|
|
9
9
|
def import_class_list(list, string)
|
10
|
-
list.each { |
|
10
|
+
list.each { |klass| java_import format(string, klass) }
|
11
11
|
end
|
12
12
|
|
13
13
|
common = %w( Vec2 Transform )
|
@@ -22,5 +22,12 @@ import_class_list(world, world_format)
|
|
22
22
|
joint = %w( Joint JointDef DistanceJointDef RevoluteJoint RevoluteJointDef MouseJointDef)
|
23
23
|
joint_format = 'org.jbox2d.dynamics.joints.%s'
|
24
24
|
import_class_list(joint, joint_format)
|
25
|
+
module PB
|
26
|
+
particle = %w( ParticleBodyContact ParticleGroup ParticleType ParticleColor
|
27
|
+
ParticleGroupDef StackQueue ParticleContact ParticleGroupType VoronoiDiagram
|
28
|
+
ParticleDef ParticleSystem )
|
29
|
+
particle_format = 'org.jbox2d.particle.%s'
|
30
|
+
import_class_list(particle, particle_format)
|
31
|
+
end
|
25
32
|
|
26
33
|
require_relative 'pbox2d/box2d.rb'
|
data/lib/pbox2d/version.rb
CHANGED
metadata
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pbox2d
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Martin Prout
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
15
15
|
requirements:
|
16
|
-
- - ~>
|
16
|
+
- - "~>"
|
17
17
|
- !ruby/object:Gem::Version
|
18
18
|
version: '2.6'
|
19
|
-
- -
|
19
|
+
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: 2.6.4
|
22
22
|
name: ruby-processing
|
@@ -24,16 +24,16 @@ dependencies:
|
|
24
24
|
type: :runtime
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
-
- - ~>
|
27
|
+
- - "~>"
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '2.6'
|
30
|
-
- -
|
30
|
+
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 2.6.4
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
requirement: !ruby/object:Gem::Requirement
|
35
35
|
requirements:
|
36
|
-
- - ~>
|
36
|
+
- - "~>"
|
37
37
|
- !ruby/object:Gem::Version
|
38
38
|
version: '10.3'
|
39
39
|
name: rake
|
@@ -41,13 +41,13 @@ dependencies:
|
|
41
41
|
type: :development
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- - ~>
|
44
|
+
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '10.3'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
49
49
|
requirements:
|
50
|
-
- - ~>
|
50
|
+
- - "~>"
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '0.9'
|
53
53
|
name: rake-compiler
|
@@ -55,7 +55,7 @@ dependencies:
|
|
55
55
|
type: :development
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- - ~>
|
58
|
+
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0.9'
|
61
61
|
description: "\"An exemplar for how processing/java libraries can be made available\n\
|
@@ -71,18 +71,17 @@ files:
|
|
71
71
|
- LICENSE.md
|
72
72
|
- README.md
|
73
73
|
- Rakefile
|
74
|
-
- lib/pbox2d.rb
|
75
|
-
- lib/pbox2d/box2d.rb
|
76
|
-
- lib/pbox2d/version.rb
|
77
74
|
- examples/bumpy_surface_noise.rb
|
78
75
|
- examples/distance_joint/boundary.rb
|
79
76
|
- examples/distance_joint/distance_joint.rb
|
80
77
|
- examples/distance_joint/pair.rb
|
81
78
|
- examples/distance_joint/particle.rb
|
79
|
+
- examples/distance_joint/particle_system.rb
|
82
80
|
- examples/lib/boundary.rb
|
83
81
|
- examples/lib/box.rb
|
84
82
|
- examples/lib/custom_shape.rb
|
85
83
|
- examples/lib/particle_system.rb
|
84
|
+
- examples/lib/shape_system.rb
|
86
85
|
- examples/lib/surface.rb
|
87
86
|
- examples/liquid_fun_test.rb
|
88
87
|
- examples/liquidy.rb
|
@@ -95,6 +94,7 @@ files:
|
|
95
94
|
- examples/quick_test.rb
|
96
95
|
- examples/revolute_joint/box.rb
|
97
96
|
- examples/revolute_joint/particle.rb
|
97
|
+
- examples/revolute_joint/particle_system.rb
|
98
98
|
- examples/revolute_joint/revolute_joint.rb
|
99
99
|
- examples/revolute_joint/windmill.rb
|
100
100
|
- examples/test_contact/lib/boundary.rb
|
@@ -103,6 +103,9 @@ files:
|
|
103
103
|
- examples/test_contact/test_contact.rb
|
104
104
|
- lib/box2d.jar
|
105
105
|
- lib/jbox2d-library-2.2.1-ds.jar
|
106
|
+
- lib/pbox2d.rb
|
107
|
+
- lib/pbox2d/box2d.rb
|
108
|
+
- lib/pbox2d/version.rb
|
106
109
|
homepage: https://github.com/ruby-processing/jbox2d
|
107
110
|
licenses:
|
108
111
|
- MIT
|
@@ -113,12 +116,12 @@ require_paths:
|
|
113
116
|
- lib
|
114
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
115
118
|
requirements:
|
116
|
-
- -
|
119
|
+
- - ">="
|
117
120
|
- !ruby/object:Gem::Version
|
118
121
|
version: '0'
|
119
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
123
|
requirements:
|
121
|
-
- -
|
124
|
+
- - ">="
|
122
125
|
- !ruby/object:Gem::Version
|
123
126
|
version: '0'
|
124
127
|
requirements:
|
@@ -126,7 +129,7 @@ requirements:
|
|
126
129
|
- java runtime >= 1.7+
|
127
130
|
- processing = 2.2.1+
|
128
131
|
rubyforge_project:
|
129
|
-
rubygems_version: 2.
|
132
|
+
rubygems_version: 2.4.8
|
130
133
|
signing_key:
|
131
134
|
specification_version: 4
|
132
135
|
summary: jbox2d wrapped in a gem for ruby-processing
|