pbox2d 0.0.1-java → 0.1.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -1
- data/Rakefile +3 -3
- data/lib/box2d.jar +0 -0
- data/lib/{box2d.rb → pbox2d.rb} +0 -0
- data/lib/pbox2d/version.rb +3 -0
- metadata +5 -7
- data/example/lib/particle_system.rb +0 -152
- data/example/liquidy.rb +0 -40
- data/lib/box2d/version.rb +0 -3
- data/lib/pbox2d.jar +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aec44c030cbe9cc96ddd4cc7b82ec504a2d16b35
|
4
|
+
data.tar.gz: 22f93126c2b21d6500771cc08b1050e1258a5d45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abc802790642e67608f31891b40b3bd7da46146a9d6eac4b20aa0f6a45295332a71fcfd934c07307b0936141c1b2123ccb1e4464323d4a233f1f2b93d05dc973
|
7
|
+
data.tar.gz: 395b5211c73016df0872ab50d84a56f0ede04c5e9d68c9c801c55af5da8cb97836369011fba26d8edb4158718f3fbc78c2dae6504c7f52a8b817253552899874
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
### Using JBox2D in ruby-processing
|
2
2
|
|
3
3
|
Here we demonstrate how to use JBox2D library as a rubygem. This approach could be an exemplar of how to use other processing/java libraries with ruby-processing
|
4
|
+
![liquidy](http://4.bp.blogspot.com/-dwnDQZVugwo/VFXrDxGOy4I/AAAAAAAAEgo/irsZxW_WLOA/s400/liquidy.png)
|
5
|
+
|
4
6
|
|
5
7
|
### Web Links
|
6
8
|
|
@@ -26,9 +28,28 @@ Copyright (c) 2014 Martin Prout
|
|
26
28
|
|
27
29
|
### To compile
|
28
30
|
|
29
|
-
To compile the gem follow the instructions for [JRubyArt][]
|
31
|
+
To compile the gem follow the instructions for [JRubyArt][], but also available as a ruby gem:-
|
32
|
+
```bash
|
33
|
+
jruby -S gem install box2d
|
34
|
+
```
|
35
|
+
Like really easy, but if you have to use rvm or rbenv you will also know what to do (but then you might find this all a bit difficult to understand, and wonder where the tests are, believe me you should throw away those crutches).
|
36
|
+
|
37
|
+
### To use
|
38
|
+
|
39
|
+
You need to `require 'pbox2d'` in the the usual way (to require a gem) but as in the included [examples][] you must also `include ContactListener` interface (by [jruby magic], including the interface as a module implements the java interface). Now you should create a new instance of Box2D.
|
40
|
+
```ruby
|
41
|
+
@box2d = Box2D.new(self)
|
42
|
+
box2d.create_world
|
43
|
+
box2d.gravity(0, -20) # to set a custom gravity
|
44
|
+
```
|
45
|
+
That's about all you need to know, use the box2d instance to access the jbox2d physics world. Ordinarily (with jbox2d) you need to set some other parameters, and call `box2d.step` in the draw loop, to update the physics world.
|
46
|
+
To make things dead simple, we have set those parameters to sensible defaults, and call `step` in the draw loop for you (under the hood using java reflection). The other thing you should know is there is a mismatch between the physics world and the sketch world (processing got it wrong to my view, down is up), further the scaling is different. This is why you need to keep translating from one worlds (coordinates) to the others coordinates, Dan Shiffman explains it in his [Nature of Code book][], Chapter 5 physics libraries, not that I've read it, I prefer to read code or [Sandi Metz][].
|
30
47
|
|
31
48
|
[JBox2D Home]:http://www.jbox2d.org/
|
32
49
|
[JBox2D on github]:https://github.com/jbox2d/jbox2d
|
33
50
|
[Box2D for processing on github]:https://github.com/shiffman/Box2D-for-Processing
|
34
51
|
[JRubyArt]:https://github.com/ruby-processing/JRubyArt
|
52
|
+
[examples]:https://github.com/ruby-processing/jbox2d/blob/master/examples/liquidy.rb
|
53
|
+
[jruby magic]:https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby
|
54
|
+
[Nature of Code book]:http://natureofcode.com/
|
55
|
+
[Sandi Metz]:http://www.poodr.com/
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rubygems/package_task'
|
|
5
5
|
require 'rdoc/task'
|
6
6
|
require 'rake/testtask'
|
7
7
|
require 'rspec/core/rake_task'
|
8
|
-
require_relative 'lib/
|
8
|
+
require_relative 'lib/pbox2d/version'
|
9
9
|
|
10
10
|
spec = Gem::Specification.new do |s|
|
11
11
|
s.name = 'pbox2d'
|
@@ -23,7 +23,7 @@ EOF
|
|
23
23
|
s.email = 'martin_p@lineone.net'
|
24
24
|
s.homepage = 'https://github.com/ruby-processing/jbox2d'
|
25
25
|
s.files = %w(LICENSE.md README.md Rakefile) + FileList['lib/**/*.rb', 'example/**/*.rb']
|
26
|
-
s.files << 'lib/
|
26
|
+
s.files << 'lib/box2d.jar'
|
27
27
|
s.files << 'lib/jbox2d-library-2.2.1-ds.jar'
|
28
28
|
s.require_path = 'lib'
|
29
29
|
s.add_dependency "ruby-processing", "~> 2.6.4"
|
@@ -66,7 +66,7 @@ copy_jars(:processing_jars, 'lib')
|
|
66
66
|
Rake::JavaExtensionTask.new('processing') do |ext|
|
67
67
|
jars = FileList['lib/*.jar']
|
68
68
|
ext.classpath = jars.map { |x| File.expand_path x}.join ':'
|
69
|
-
ext.name = '
|
69
|
+
ext.name = 'box2d'
|
70
70
|
ext.debug = true
|
71
71
|
ext.lib_dir = 'lib'
|
72
72
|
ext.source_version='1.7'
|
data/lib/box2d.jar
ADDED
Binary file
|
data/lib/{box2d.rb → pbox2d.rb}
RENAMED
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pbox2d
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Martin Prout
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
@@ -65,11 +65,9 @@ files:
|
|
65
65
|
- LICENSE.md
|
66
66
|
- README.md
|
67
67
|
- Rakefile
|
68
|
-
- lib/
|
69
|
-
- lib/
|
70
|
-
-
|
71
|
-
- example/liquidy.rb
|
72
|
-
- lib/pbox2d.jar
|
68
|
+
- lib/pbox2d.rb
|
69
|
+
- lib/pbox2d/version.rb
|
70
|
+
- lib/box2d.jar
|
73
71
|
- lib/jbox2d-library-2.2.1-ds.jar
|
74
72
|
homepage: https://github.com/ruby-processing/jbox2d
|
75
73
|
licenses:
|
@@ -1,152 +0,0 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
|
3
|
-
module Runnable
|
4
|
-
def run
|
5
|
-
reject! { |item| item.done }
|
6
|
-
each { |item| item.display }
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
class ParticleSystem
|
11
|
-
include Enumerable, Runnable
|
12
|
-
extend Forwardable
|
13
|
-
def_delegators(:@particles, :each, :reject!, :<<, :empty?)
|
14
|
-
def_delegator(:@particles, :empty?, :dead?)
|
15
|
-
|
16
|
-
attr_reader :particles, :x, :y
|
17
|
-
|
18
|
-
def initialize(bd, num, x, y)
|
19
|
-
@particles = [] # Initialize the Array
|
20
|
-
@x, @y = x, y # Store the origin point
|
21
|
-
num.times do
|
22
|
-
self << Particle.new(bd, x, y)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def add_particles(bd, n)
|
27
|
-
n.times do
|
28
|
-
self << Particle.new(bd, x, y)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# A Particle
|
34
|
-
require 'pbox2d'
|
35
|
-
|
36
|
-
class Particle
|
37
|
-
include Processing::Proxy
|
38
|
-
include PB
|
39
|
-
TRAIL_SIZE = 6
|
40
|
-
# We need to keep track of a Body
|
41
|
-
|
42
|
-
attr_reader :trail, :body, :box2d
|
43
|
-
|
44
|
-
# Constructor
|
45
|
-
def initialize(b2d, x, y)
|
46
|
-
@box2d = b2d
|
47
|
-
@trail = Array.new(TRAIL_SIZE, [x, y])
|
48
|
-
|
49
|
-
# Add the box to the box2d world
|
50
|
-
# Here's a little trick, let's make a tiny tiny radius
|
51
|
-
# This way we have collisions, but they don't overwhelm the system
|
52
|
-
make_body(PB::Vec2.new(x,y), 0.2)
|
53
|
-
end
|
54
|
-
|
55
|
-
# This function removes the particle from the box2d world
|
56
|
-
def kill_body
|
57
|
-
box2d.destroy_body(body)
|
58
|
-
end
|
59
|
-
|
60
|
-
# Is the particle ready for deletion?
|
61
|
-
def done
|
62
|
-
# Let's find the screen position of the particle
|
63
|
-
pos = box2d.get_body_pixel_coord(body)
|
64
|
-
# Is it off the bottom of the screen?
|
65
|
-
return false unless (pos.y > $app.height + 20)
|
66
|
-
kill_body
|
67
|
-
true
|
68
|
-
end
|
69
|
-
|
70
|
-
# Drawing the box
|
71
|
-
def display
|
72
|
-
# We look at each body and get its screen position
|
73
|
-
pos = box2d.get_body_pixel_coord(body)
|
74
|
-
# Keep track of a history of screen positions in an array
|
75
|
-
(TRAIL_SIZE - 1).times do |i|
|
76
|
-
trail[i] = trail[i + 1]
|
77
|
-
end
|
78
|
-
trail[TRAIL_SIZE - 1] = [pos.x, pos.y]
|
79
|
-
# Draw particle as a trail
|
80
|
-
begin_shape
|
81
|
-
no_fill
|
82
|
-
stroke_weight(2)
|
83
|
-
stroke(0, 150)
|
84
|
-
trail.each do |v|
|
85
|
-
vertex(v[0], v[1])
|
86
|
-
end
|
87
|
-
end_shape
|
88
|
-
end
|
89
|
-
|
90
|
-
# This function adds the rectangle to the box2d world
|
91
|
-
def make_body(center, r)
|
92
|
-
# Define and create the body
|
93
|
-
bd = PB::BodyDef.new
|
94
|
-
bd.type = PB::BodyType::DYNAMIC
|
95
|
-
bd.position.set(box2d.coord_pixels_to_world(center))
|
96
|
-
@body = box2d.create_body(bd)
|
97
|
-
# Give it some initial random velocity
|
98
|
-
body.set_linear_velocity(PB::Vec2.new(rand(-1.0..1), rand(-1.0..1)))
|
99
|
-
# Make the body's shape a circle
|
100
|
-
cs = PB::CircleShape.new
|
101
|
-
cs.m_radius = box2d.scalar_pixels_to_world(r)
|
102
|
-
fd = PB::FixtureDef.new
|
103
|
-
fd.shape = cs
|
104
|
-
fd.density = 1
|
105
|
-
fd.friction = 0 # Slippery when wet!
|
106
|
-
fd.restitution = 0.5
|
107
|
-
# We could use this if we want to turn collisions off
|
108
|
-
#cd.filter.groupIndex = -10
|
109
|
-
# Attach fixture to body
|
110
|
-
body.create_fixture(fd)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
|
115
|
-
class Boundary
|
116
|
-
include Processing::Proxy
|
117
|
-
include PB
|
118
|
-
attr_reader :box2d, :b, :x, :y, :w, :h
|
119
|
-
|
120
|
-
def initialize(b2d, x, y, w, h, a)
|
121
|
-
@box2d, @x, @y, @w, @h = b2d, x, y, w, h
|
122
|
-
# Define the polygon
|
123
|
-
sd = PB::PolygonShape.new
|
124
|
-
# Figure out the box2d coordinates
|
125
|
-
box2dW = box2d.scalar_pixels_to_world(w / 2)
|
126
|
-
box2dH = box2d.scalar_pixels_to_world(h / 2)
|
127
|
-
# We're just a box
|
128
|
-
sd.set_as_box(box2dW, box2dH)
|
129
|
-
# Create the body
|
130
|
-
bd = PB::BodyDef.new
|
131
|
-
bd.type = PB::BodyType::STATIC
|
132
|
-
bd.angle = a
|
133
|
-
bd.position.set(box2d.coord_pixels_to_world(x,y))
|
134
|
-
@b = box2d.create_body(bd)
|
135
|
-
# Attached the shape to the body using a Fixture
|
136
|
-
b.create_fixture(sd, 1)
|
137
|
-
end
|
138
|
-
|
139
|
-
# Draw the boundary, it doesn't move so we don't have to ask the Body for location
|
140
|
-
def display
|
141
|
-
fill(0)
|
142
|
-
stroke(0)
|
143
|
-
stroke_weight(1)
|
144
|
-
rect_mode(CENTER)
|
145
|
-
a = b.get_angle
|
146
|
-
push_matrix
|
147
|
-
translate(x, y)
|
148
|
-
rotate(-a)
|
149
|
-
rect(0, 0, w, h)
|
150
|
-
pop_matrix
|
151
|
-
end
|
152
|
-
end
|
data/example/liquidy.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'box2d'
|
2
|
-
require_relative 'lib/particle_system'
|
3
|
-
attr_reader :box2d, :boundaries, :systems
|
4
|
-
|
5
|
-
include ContactListener
|
6
|
-
|
7
|
-
def setup
|
8
|
-
size(400,300)
|
9
|
-
@box2d = Box2D.new(self)
|
10
|
-
box2d.create_world
|
11
|
-
# We are setting a custom gravity
|
12
|
-
box2d.set_gravity(0, -20)
|
13
|
-
|
14
|
-
# Create Arrays
|
15
|
-
@systems = []
|
16
|
-
@boundaries = []
|
17
|
-
# Add a bunch of fixed boundaries
|
18
|
-
boundaries << Boundary.new(box2d, 50, 100, 300, 5, -0.3)
|
19
|
-
boundaries << Boundary.new(box2d, 250, 175, 300, 5, 0.5)
|
20
|
-
end
|
21
|
-
|
22
|
-
def draw
|
23
|
-
background(255)
|
24
|
-
# Run all the particle systems
|
25
|
-
if systems.size > 0
|
26
|
-
systems.each do |system|
|
27
|
-
system.run
|
28
|
-
system.add_particles(box2d, rand(0..2))
|
29
|
-
end
|
30
|
-
end
|
31
|
-
# Display all the boundaries
|
32
|
-
boundaries.each(&:display)
|
33
|
-
end
|
34
|
-
|
35
|
-
def mouse_pressed
|
36
|
-
# Add a new Particle System whenever the mouse is clicked
|
37
|
-
systems << ParticleSystem.new(box2d, 0, mouse_x, mouse_y)
|
38
|
-
end
|
39
|
-
|
40
|
-
|
data/lib/box2d/version.rb
DELETED
data/lib/pbox2d.jar
DELETED
Binary file
|