pbox2d 0.3.1-java → 0.4.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 804efe16e11649eda15f47f166687f533b01bb13
4
- data.tar.gz: 21eacf2bd1015bc7a5029d95a6e5a9cc4d3a50ef
3
+ metadata.gz: 81258910255ac53b8912a7f3f837b9bf02a07272
4
+ data.tar.gz: f90863528fb8b5bf7b181d83242e827d76c10b69
5
5
  SHA512:
6
- metadata.gz: a703df8b21385e2d880141d34d2ac8b5ed6d9566512b694ff05c0720ab3c948fb0ea3e7e128dc49eee8be87b6824e14b7ca95ac5f9387763733994bdb43f7f79
7
- data.tar.gz: 01e4749137f5f863d8d7327a45dd231eca1becf2591661ca0b49973aa257aa1283019a7754a223ac069bea54cf0726ede70f47948b649a18db9b92773c2efa65
6
+ metadata.gz: 1cb053c08445179dfcd209feb78e86cca4dd057012e76c92c73e86a207eba663fcbd2aaa8ecbefaa7324f6093f747b1253a4cf6a76688c7638b0054ee08199d1
7
+ data.tar.gz: 3cb3b8ce51b0f8f1bf9ff9c3a42b801b6ce2d7316cca6a80a3e6df3d909d4c8956f3086beae37007c752cf56aa67863f7466cf5e56639261cf067f1fb4c015c6
data/README.md CHANGED
@@ -1,9 +1,11 @@
1
1
  ### Using JBox2D in ruby-processing
2
2
 
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
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 both ruby-processing and JRubyArt.
4
4
  ![liquidy](http://4.bp.blogspot.com/-dwnDQZVugwo/VFXrDxGOy4I/AAAAAAAAEgo/irsZxW_WLOA/s400/liquidy.png)
5
5
 
6
6
 
7
+ [![Gem Version](https://badge.fury.io/rb/pbox2d.svg)](http://badge.fury.io/rb/pbox2d)
8
+
7
9
  ### Web Links
8
10
 
9
11
  [JBox2D Home][]
@@ -40,7 +42,7 @@ Like really easy, but if you have to use rvm or rbenv you will also know what to
40
42
 
41
43
  ### To use
42
44
 
43
- You need to `require 'pbox2d'` in the the usual way (to require a gem). Now you should create a new instance of Box2D. However as in the included [example][] you must also `include ContactListener` interface (by [jruby magic][], including the interface as a module implements the java interface) if you wish to create your own jbox2d listener.
45
+ You need to `require 'pbox2d'` in the the usual way (to require a gem). Now you should create a new instance of Box2D. However as in this included [example][] you may also need to `include ContactListener` interface (by [jruby magic][], including the interface as a module implements the java interface) if you wish to create your own jbox2d listener.
44
46
  ```ruby
45
47
  @box2d = Box2D.new(self)
46
48
  box2d.init_options(gravity: [0, -20]) # this is new since version 0.2.0
data/Rakefile CHANGED
@@ -22,11 +22,11 @@ EOF
22
22
  s.authors = ['Martin Prout']
23
23
  s.email = 'martin_p@lineone.net'
24
24
  s.homepage = 'https://github.com/ruby-processing/jbox2d'
25
- s.files = %w(LICENSE.md README.md Rakefile) + FileList['lib/**/*.rb', 'example/**/*.rb']
25
+ s.files = %w(LICENSE.md README.md Rakefile) + FileList['lib/**/*.rb', 'examples/**/*.rb']
26
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
- s.add_dependency "ruby-processing", "~> 2.6.4"
29
+ s.add_dependency 'ruby-processing', '~> 2.6', '>= 2.6.4'
30
30
  s.add_development_dependency "rake", "~> 10.3"
31
31
  s.add_development_dependency "rake-compiler", "~> 0.9"
32
32
  s.platform='java'
@@ -0,0 +1,41 @@
1
+ # The Nature of Code
2
+ # PBox2D example
3
+ # An uneven surface
4
+
5
+ require 'pbox2d'
6
+ require_relative 'lib/surface'
7
+
8
+ attr_reader :surface, :box2d, :particles
9
+
10
+ def setup
11
+ size(500, 300)
12
+ smooth 4
13
+ # Initialize box2d physics and create the world
14
+ @box2d = Box2D.new(self)
15
+ box2d.init_options(gravity: [0, -20])
16
+ box2d.create_world
17
+ # to later set a custom gravity
18
+ # box2d.gravity([0, -20])
19
+ # Create the empty list
20
+ @particles = []
21
+ # Create the surface
22
+ @surface = Surface.new(self)
23
+ end
24
+
25
+ def draw
26
+ # If the mouse is pressed, we make new particles
27
+ # We must always step through time!
28
+ background(138, 66, 54)
29
+ # Draw the surface
30
+ surface.display
31
+ # NB ? reqd to call mouse_pressed value, else method gets called.
32
+ particles << Particle.new(self, mouse_x, mouse_y, rand(2.0..6)) if mouse_pressed?
33
+ # Draw all particles
34
+ particles.each(&:display)
35
+ # Particles that leave the screen, we delete them
36
+ # (note they have to be deleted from both the box2d world and our list
37
+ particles.reject!(&:done)
38
+ # Just drawing the framerate to see how many particles it can handle
39
+ fill(0)
40
+ text("framerate: #{frame_rate.to_i}", 12, 16)
41
+ end
@@ -0,0 +1,39 @@
1
+ # The Nature of Code
2
+ # Daniel Shiffman
3
+ # http://natureofcode.com
4
+ require 'forwardable'
5
+
6
+ # A fixed boundary class
7
+ class Boundary
8
+ extend Forwardable
9
+ def_delegators(:@app, :box2d, :rect_mode, :rect, :fill, :stroke)
10
+ # A boundary is a simple rectangle with x, y, width, and height
11
+ attr_reader :x, :y, :w, :h
12
+
13
+ def initialize(x, y, w, h)
14
+ @x, @y, @w, @h = x, y, w, h
15
+ @app = $app
16
+ # Define the polygon
17
+ sd = PolygonShape.new
18
+ # Figure out the box2d coordinates
19
+ box2dW = box2d.scale_to_world(w / 2)
20
+ box2dH = box2d.scale_to_world(h / 2)
21
+ # We're just a box
22
+ sd.setAsBox(box2dW, box2dH)
23
+ # Create the body
24
+ bd = BodyDef.new
25
+ bd.type = BodyType::STATIC
26
+ bd.position.set(box2d.processing_to_world(x,y))
27
+ b = box2d.createBody(bd)
28
+ # Attached the shape to the body using a Fixture
29
+ b.createFixture(sd,1)
30
+ end
31
+
32
+ # Draw the boundary, if it were at an angle we'd have to do something fancier
33
+ def display
34
+ fill(0)
35
+ stroke(0)
36
+ rect_mode(Java::ProcessingCore::PConstants::CENTER)
37
+ rect(x, y, w, h)
38
+ end
39
+ end
@@ -0,0 +1,38 @@
1
+ # The Nature of Code
2
+ # Daniel Shiffman
3
+ # http://natureofcode.com
4
+
5
+ # Example demonstrating distance joints
6
+ # A bridge is formed by connected a series of particles with joints
7
+
8
+ require 'pbox2d'
9
+ require_relative 'boundary'
10
+ require_relative 'pair'
11
+ require_relative 'particle'
12
+
13
+ attr_reader :box2d, :boundaries, :pairs
14
+
15
+ def setup
16
+ size(640, 360)
17
+ # Initialize box2d physics and create the world
18
+ @box2d = Box2D.new(self)
19
+ box2d.create_world
20
+ @pairs = []
21
+ @boundaries = []
22
+ # Add a bunch of fixed boundaries
23
+ boundaries << Boundary.new(width / 4, height - 5, width / 2 - 50, 10)
24
+ boundaries << Boundary.new(3 * width / 4, height - 50, width / 2 - 50, 10)
25
+ end
26
+
27
+ def draw
28
+ background(255)
29
+ pairs.each(&:display)
30
+ # Display all the boundaries
31
+ boundaries.each(&:display)
32
+ fill(0)
33
+ text('Click mouse to add connected particles.', 10, 20)
34
+ end
35
+
36
+ def mouse_pressed
37
+ pairs << Pair.new(mouse_x, mouse_y)
38
+ end
@@ -0,0 +1,41 @@
1
+ # The Nature of Code
2
+ # Daniel Shiffman
3
+ # http://natureofcode.com
4
+ require 'forwardable'
5
+
6
+ # Series of Particles connected with distance joints
7
+ class Pair
8
+ extend Forwardable
9
+ def_delegators(:@app, :box2d, :stroke, :line, :stroke_weight)
10
+ attr_reader :p1, :p2, :len
11
+ # Chain constructor
12
+ def initialize(x, y)
13
+ @app = $app
14
+ @len = 32
15
+ @p1 = Particle.new(x, y)
16
+ @p2 = Particle.new(x + rand(-1..1.0), y + rand(-1..1.0))
17
+ djd = DistanceJointDef.new
18
+ # Connection between previous particle and this one
19
+ djd.bodyA = p1.body
20
+ djd.bodyB = p2.body
21
+ # Equilibrium length
22
+ djd.length = box2d.scale_to_world(len)
23
+ # These properties affect how springy the joint is
24
+ djd.frequencyHz = 3 # Try a value less than 5 (0 for no elasticity)
25
+ 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)
29
+ end
30
+
31
+ def display
32
+ pos1 = box2d.body_coord(p1.body)
33
+ pos2 = box2d.body_coord(p2.body)
34
+ stroke(0)
35
+ stroke_weight(2)
36
+ line(pos1.x, pos1.y, pos2.x, pos2.y)
37
+ p1.display
38
+ p2.display
39
+ end
40
+ end
41
+
@@ -0,0 +1,75 @@
1
+ # The Nature of Code
2
+ # Daniel Shiffman
3
+ # http://natureofcode.com
4
+ require 'forwardable'
5
+
6
+ # A circular particle
7
+ class Particle
8
+ extend Forwardable
9
+ def_delegators(:@app, :fill, :stroke, :stroke_weight, :box2d, :height, :line,
10
+ :push_matrix, :pop_matrix, :ellipse, :rotate, :translate)
11
+ # We need to keep track of a Body and a radius
12
+ attr_reader :body, :r
13
+
14
+ def initialize(x, y)
15
+ @r = 8
16
+ @app = $app
17
+ # Define a body
18
+ bd = BodyDef.new
19
+ # Set its position
20
+ bd.position = box2d.processing_to_world(x,y)
21
+ bd.type = BodyType::DYNAMIC
22
+ @body = box2d.world.createBody(bd)
23
+
24
+ # Make the body's shape a circle
25
+ cs = CircleShape.new
26
+ cs.m_radius = box2d.scale_to_world(r)
27
+
28
+ fd = FixtureDef.new
29
+ fd.shape = cs
30
+ # Parameters that affect physics
31
+ fd.density = 1
32
+ fd.friction = 0.01
33
+ fd.restitution = 0.3
34
+
35
+ # Attach fixture to body
36
+ body.createFixture(fd)
37
+ body.setLinearVelocity(Vec2.new(rand(-5..5), rand(2..5)))
38
+ end
39
+
40
+ # This function removes the particle from the box2d world
41
+ def kill_body
42
+ box2d.destroy_body(body)
43
+ end
44
+
45
+ # Is the particle ready for deletion?
46
+ def done
47
+ # Let's find the screen position of the particle
48
+ pos = box2d.body_coord(body)
49
+ # Is it off the bottom of the screen?
50
+ if pos.y > height + r * 2
51
+ kill_body
52
+ return true
53
+ end
54
+ false
55
+ end
56
+
57
+ def display
58
+ # We look at each body and get its screen position
59
+ pos = box2d.body_coord(body)
60
+ # Get its angle of rotation
61
+ a = body.get_angle
62
+ push_matrix
63
+ translate(pos.x, pos.y)
64
+ rotate(a)
65
+ fill(127)
66
+ stroke(0)
67
+ stroke_weight(2)
68
+ ellipse(0, 0, r * 2, r * 2)
69
+ # Let's add a line so we can see the rotation
70
+ line(0, 0, r, 0)
71
+ pop_matrix
72
+ end
73
+ end
74
+
75
+
@@ -0,0 +1,30 @@
1
+ require 'forwardable'
2
+
3
+ # The boundary class is used to create fixtures
4
+ class Boundary
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
10
+ sd = PolygonShape.new
11
+ box2d_w = box2d.scale_to_world(w / 2)
12
+ box2d_h = box2d.scale_to_world(h / 2)
13
+ sd.setAsBox(box2d_w, box2d_h)
14
+ # Create the body
15
+ bd = BodyDef.new
16
+ bd.type = BodyType::STATIC
17
+ bd.position.set(box2d.processing_to_world(x, y))
18
+ @b = box2d.create_body(bd)
19
+ # Attached the shape to the body using a Fixture
20
+ b.create_fixture(sd, 1)
21
+ end
22
+
23
+ # Draw the boundary, if it were at an angle we'd have to do something fancier
24
+ def display
25
+ fill(0)
26
+ stroke(0)
27
+ rect_mode(Java::ProcessingCore::PConstants::CENTER)
28
+ rect(x, y, w, h)
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ # A Box class, note how to access class ParticleGroupDef in jruby
2
+ # which is currently not on an included path for pbox2d
3
+ class Box
4
+ attr_accessor :pg
5
+ def initialize(b2d, x, y)
6
+ w = rand(1..3)
7
+ h = rand(1..3)
8
+ shape = PolygonShape.new
9
+ pos = b2d.processing_to_world(x, y)
10
+ shape.setAsBox(w, h, pos, 0)
11
+ pd = Java::OrgJbox2dParticle::ParticleGroupDef.new
12
+ pd.shape = shape
13
+ @pg = b2d.world.create_particle_group(pd)
14
+ end
15
+ end
File without changes
@@ -0,0 +1,154 @@
1
+ require 'forwardable'
2
+
3
+ module Runnable
4
+ def run
5
+ reject!(&:done)
6
+ each(&: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 :x, :y
17
+
18
+ def initialize(app, 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(app, x, y)
23
+ end
24
+ end
25
+
26
+ def add_particles(app, n)
27
+ n.times do
28
+ self << Particle.new(app, x, y)
29
+ end
30
+ end
31
+ end
32
+
33
+ # A Particle
34
+ require 'pbox2d'
35
+
36
+ class Particle
37
+ extend Forwardable
38
+ def_delegators(:@app, :box2d, :begin_shape, :end_shape,
39
+ :vertex, :translate, :rotate, :stroke,
40
+ :fill, :no_fill, :stroke_weight)
41
+ TRAIL_SIZE = 6
42
+ # We need to keep track of a Body
43
+
44
+ attr_reader :trail, :body
45
+
46
+ # Constructor
47
+ def initialize(app, x, y)
48
+ @app = app
49
+ @trail = Array.new(TRAIL_SIZE, [x, y])
50
+ # Add the box to the box2d world
51
+ # Here's a little trick, let's make a tiny tiny radius
52
+ # This way we have collisions, but they don't overwhelm the system
53
+ make_body(x, y, 0.2)
54
+ end
55
+
56
+ # This function removes the particle from the box2d world
57
+ def kill_body
58
+ box2d.destroy_body(body)
59
+ end
60
+
61
+ # Is the particle ready for deletion?
62
+ def done
63
+ # Let's find the screen position of the particle
64
+ pos = box2d.body_coord(body)
65
+ # Is it off the bottom of the screen?
66
+ return false unless pos.y > box2d.height + 20
67
+ kill_body
68
+ true
69
+ end
70
+
71
+ # Drawing the box
72
+ def display
73
+ # We look at each body and get its screen position
74
+ pos = box2d.body_coord(body)
75
+ # Keep track of a history of screen positions in an array
76
+ (TRAIL_SIZE - 1).times do |i|
77
+ trail[i] = trail[i + 1]
78
+ end
79
+ trail[TRAIL_SIZE - 1] = [pos.x, pos.y]
80
+ # Draw particle as a trail
81
+ begin_shape
82
+ no_fill
83
+ stroke_weight(2)
84
+ stroke(0, 150)
85
+ trail.each do |v|
86
+ vertex(v[0], v[1])
87
+ end
88
+ end_shape
89
+ end
90
+
91
+ # This function adds the rectangle to the box2d world
92
+ def make_body(x, y, r)
93
+ # Define and create the body
94
+ bd = BodyDef.new
95
+ bd.type = BodyType::DYNAMIC
96
+ bd.position.set(box2d.processing_to_world(x, y))
97
+ @body = box2d.create_body(bd)
98
+ # Give it some initial random velocity
99
+ body.set_linear_velocity(Vec2.new(rand(-1.0..1), rand(-1.0..1)))
100
+ # Make the body's shape a circle
101
+ cs = CircleShape.new
102
+ cs.m_radius = box2d.scale_to_world(r)
103
+ fd = FixtureDef.new
104
+ fd.shape = cs
105
+ fd.density = 1
106
+ fd.friction = 0 # Slippery when wet!
107
+ fd.restitution = 0.5
108
+ # We could use this if we want to turn collisions off
109
+ # cd.filter.groupIndex = -10
110
+ # Attach fixture to body
111
+ body.create_fixture(fd)
112
+ end
113
+ end
114
+
115
+ class Boundary
116
+ extend Forwardable
117
+ def_delegators(:@app, :box2d, :push_matrix, :pop_matrix, :stroke, :width,
118
+ :vertex, :translate, :rotate, :rect_mode, :rect,
119
+ :fill, :no_fill, :stroke_weight)
120
+ attr_reader :b, :x, :y, :h
121
+
122
+ def initialize(app, x, y, h, a)
123
+ @app, @x, @y, @h = app, x, y, h
124
+ # Define the polygon
125
+ sd = PolygonShape.new
126
+ # Figure out the box2d coordinates
127
+ box2d_w = box2d.scale_to_world(width / 2)
128
+ box2d_h = box2d.scale_to_world(h / 2)
129
+ # We're just a box
130
+ sd.set_as_box(box2d_w, box2d_h)
131
+ # Create the body
132
+ bd = BodyDef.new
133
+ bd.type = BodyType::STATIC
134
+ bd.angle = a
135
+ bd.position.set(box2d.processing_to_world(x, y))
136
+ @b = box2d.create_body(bd)
137
+ # Attached the shape to the body using a Fixture
138
+ b.create_fixture(sd, 1)
139
+ end
140
+
141
+ # Draw the boundary, it doesn't move so we don't have to ask the Body for location
142
+ def display
143
+ fill(0)
144
+ stroke(0)
145
+ stroke_weight(1)
146
+ rect_mode(Java::ProcessingCore::PConstants::CENTER)
147
+ a = b.get_angle
148
+ push_matrix
149
+ translate(x, y)
150
+ rotate(-a)
151
+ rect(0, 0, width, h)
152
+ pop_matrix
153
+ end
154
+ end