pbox2d 0.0.1-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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d58b2de1118d6e9cb55590c19e0cd0dd3d699e18
4
+ data.tar.gz: 1f2b801732b33388098101e973565821dd778661
5
+ SHA512:
6
+ metadata.gz: 8c323a434d3df3e99e9ab3cdfef46d528bad0cda056b31760852ae12d27f7a245e39e86be592f47c13c5639ea5a7c78efa8326b4dc8bf97d9ca2667f134a2779
7
+ data.tar.gz: 52c187591b38c671d9f13f0253dfe1bb53218340685621ae82976ba2b8a3c727b758219dd7da259b3eb7a46612e2250cb8b73a710d07765a69776ef9e23934ff
data/LICENSE.md ADDED
@@ -0,0 +1,36 @@
1
+ ### License
2
+
3
+ This work makes use of jbox2d see included LICENCE, and is derived from,
4
+ but different significantly different from Dan Shiffmans PBox2D-for-processing
5
+ who claims this:-
6
+
7
+ Copyright (c) 2014, Daniel Shiffman
8
+ All rights reserved.
9
+ Redistribution and use in source and binary forms, with or without modification,
10
+ are permitted provided that the following conditions are met:
11
+ * Redistributions of source code must retain the above copyright notice, this
12
+ list of conditions and the following disclaimer.
13
+ * Redistributions in binary form must reproduce the above copyright notice, this
14
+ list of conditions and the following disclaimer in the documentation and/or
15
+ other materials provided with the distribution.
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
20
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ I include the Shiffman licence mainly because of the included example
28
+ (albeit in processing) is his idea. It would be better if everyone stuck to
29
+ recognized licences.....
30
+
31
+ For my part I lean toward GPL3 but MIT is OK and matches ruby-processing and
32
+ vanilla processing.
33
+
34
+ Copyright (c) 2014, Martin Prout
35
+
36
+
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ ### Using JBox2D in ruby-processing
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
4
+
5
+ ### Web Links
6
+
7
+ [JBox2D Home][]
8
+
9
+ [JBox2D on github][]
10
+
11
+ [Box2D for processing on github][]
12
+
13
+ ### Licensing
14
+
15
+ Daniel Murphy is currently claiming ownership of jbox2d (although surely a derived work):-
16
+
17
+ Copyright (c) 2014 Daniel Murphy
18
+
19
+ Dan Shiffman wrote the original PBox2D for processing (on which this implementation is based)
20
+
21
+ Copyright (c) 2014 Daniel Shiffman
22
+
23
+ This gem was created by Martin Prout
24
+
25
+ Copyright (c) 2014 Martin Prout
26
+
27
+ ### To compile
28
+
29
+ To compile the gem follow the instructions for [JRubyArt][]
30
+
31
+ [JBox2D Home]:http://www.jbox2d.org/
32
+ [JBox2D on github]:https://github.com/jbox2d/jbox2d
33
+ [Box2D for processing on github]:https://github.com/shiffman/Box2D-for-Processing
34
+ [JRubyArt]:https://github.com/ruby-processing/JRubyArt
data/Rakefile ADDED
@@ -0,0 +1,101 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rubygems/package_task'
5
+ require 'rdoc/task'
6
+ require 'rake/testtask'
7
+ require 'rspec/core/rake_task'
8
+ require_relative 'lib/box2d/version'
9
+
10
+ spec = Gem::Specification.new do |s|
11
+ s.name = 'pbox2d'
12
+ s.version = Pbox2D::VERSION
13
+ s.has_rdoc = true
14
+ s.extra_rdoc_files = ['README.md', 'LICENSE.md']
15
+ s.summary = 'jbox2d wrapped in a gem for ruby-processing'
16
+ s.description = <<-EOF
17
+ "An exemplar for how processing/java libraries can be made available
18
+ for use in ruby-processing. In this example avoiding an overdose of java
19
+ reflection by letting jruby sugar implement an interface"
20
+ EOF
21
+ s.license = 'MIT'
22
+ s.authors = ['Martin Prout']
23
+ s.email = 'martin_p@lineone.net'
24
+ s.homepage = 'https://github.com/ruby-processing/jbox2d'
25
+ s.files = %w(LICENSE.md README.md Rakefile) + FileList['lib/**/*.rb', 'example/**/*.rb']
26
+ s.files << 'lib/pbox2d.jar'
27
+ s.files << 'lib/jbox2d-library-2.2.1-ds.jar'
28
+ s.require_path = 'lib'
29
+ s.add_dependency "ruby-processing", "~> 2.6.4"
30
+ s.add_development_dependency "rake", "~> 10.3"
31
+ s.add_development_dependency "rake-compiler", "~> 0.9"
32
+ s.platform='java'
33
+ s.requirements << 'A decent graphics card'
34
+ s.requirements << 'java runtime >= 1.7+'
35
+ s.requirements << 'processing = 2.2.1+'
36
+ end
37
+
38
+ # -*- ruby -*-
39
+
40
+ require 'java'
41
+ require 'rake/javaextensiontask'
42
+
43
+ # -*- encoding: utf-8 -*-
44
+ require 'psych'
45
+
46
+ def copy_jars(name, dest)
47
+ conf = '~/.jruby_art/config.yml'
48
+ begin
49
+ path = File.expand_path(conf)
50
+ rp_config = (Psych.load_file(path))
51
+ source= "#{rp_config["PROCESSING_ROOT"]}/core/library/"
52
+ rescue
53
+ raise "WARNING: you must set PROCESSING_ROOT in #{conf} compile"
54
+ end
55
+ body = proc {
56
+ Dir["#{source}/core.jar"].each do |f|
57
+ puts "Copying #{f} To #{dest}"
58
+ FileUtils.cp f, dest
59
+ end
60
+ }
61
+ Rake::Task.define_task(name, &body)
62
+ end
63
+
64
+ copy_jars(:processing_jars, 'lib')
65
+
66
+ Rake::JavaExtensionTask.new('processing') do |ext|
67
+ jars = FileList['lib/*.jar']
68
+ ext.classpath = jars.map { |x| File.expand_path x}.join ':'
69
+ ext.name = 'pbox2d'
70
+ ext.debug = true
71
+ ext.lib_dir = 'lib'
72
+ ext.source_version='1.7'
73
+ ext.target_version='1.7'
74
+ end
75
+
76
+ Gem::PackageTask.new(spec) do |p|
77
+ p.gem_spec = spec
78
+ p.need_tar = true
79
+ p.need_zip = true
80
+ end
81
+
82
+ Rake::RDocTask.new do |rdoc|
83
+ files = ['README.md', 'LICENSE.md'] + FileList['lib/**/*.rb']
84
+
85
+ rdoc.rdoc_files.add(files)
86
+ rdoc.main = "README.md" # page to start on
87
+ rdoc.title = "Box2D Docs"
88
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
89
+ rdoc.options << '--line-numbers'
90
+ end
91
+
92
+ Rake::TestTask.new do |t|
93
+ t.test_files = FileList['test/**/*.rb']
94
+ end
95
+
96
+ RSpec::Core::RakeTask.new do |spec|
97
+ spec.pattern = 'spec/*_spec.rb'
98
+ spec.rspec_opts = [Dir["lib"].to_a.join(':')]
99
+ end
100
+
101
+
@@ -0,0 +1,152 @@
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
@@ -0,0 +1,40 @@
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.rb ADDED
@@ -0,0 +1,15 @@
1
+ working_directory = File.join(File.dirname(__FILE__))
2
+ $LOAD_PATH << working_directory unless $LOAD_PATH.include?(working_directory)
3
+ Dir[File.join(working_directory, '*.jar')].each do |jar|
4
+ require jar
5
+ end
6
+
7
+ module PB
8
+ include_package 'org.jbox2d.collision.shapes'
9
+ include_package 'org.jbox2d.common'
10
+ include_package 'org.jbox2d.dynamics'
11
+ include_package 'processing.box2d'
12
+ end
13
+
14
+ ContactListener = Java::OrgJbox2dCallbacks::ContactListener
15
+ Box2D = PB::Box2DProcessing
@@ -0,0 +1,3 @@
1
+ module Pbox2D
2
+ VERSION = '0.0.1'
3
+ end
Binary file
data/lib/pbox2d.jar ADDED
Binary file
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pbox2d
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: java
6
+ authors:
7
+ - Martin Prout
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ~>
17
+ - !ruby/object:Gem::Version
18
+ version: 2.6.4
19
+ name: ruby-processing
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.6.4
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '10.3'
33
+ name: rake
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '0.9'
47
+ name: rake-compiler
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ description: "\"An exemplar for how processing/java libraries can be made available\n\
56
+ for use in ruby-processing. In this example avoiding an overdose of java \nreflection\
57
+ \ by letting jruby sugar implement an interface\"\n"
58
+ email: martin_p@lineone.net
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files:
62
+ - README.md
63
+ - LICENSE.md
64
+ files:
65
+ - LICENSE.md
66
+ - README.md
67
+ - Rakefile
68
+ - lib/box2d.rb
69
+ - lib/box2d/version.rb
70
+ - example/lib/particle_system.rb
71
+ - example/liquidy.rb
72
+ - lib/pbox2d.jar
73
+ - lib/jbox2d-library-2.2.1-ds.jar
74
+ homepage: https://github.com/ruby-processing/jbox2d
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements:
93
+ - A decent graphics card
94
+ - java runtime >= 1.7+
95
+ - processing = 2.2.1+
96
+ rubyforge_project:
97
+ rubygems_version: 2.1.9
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: jbox2d wrapped in a gem for ruby-processing
101
+ test_files: []