pbox2d 0.4.0-java → 0.4.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 +4 -4
- data/examples/mouse_joint/boundary.rb +47 -0
- data/examples/mouse_joint/box.rb +72 -0
- data/examples/mouse_joint/dummy_spring.rb +24 -0
- data/examples/mouse_joint/mouse_joint.rb +57 -0
- data/examples/mouse_joint/spring.rb +65 -0
- data/lib/box2d.jar +0 -0
- data/lib/pbox2d/version.rb +1 -1
- data/lib/pbox2d.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 484938096a6218ebe38a1b79215bf8f0e1062b91
|
4
|
+
data.tar.gz: 71f92da9c98bc3a5a73753ae6d63a0dfb0225bba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d63ff12ca750102d8e68927f3af4f64fc6c41b2fa9db8a4deb1f4036904263f83e2a57de5d146a66a3bb873449358d21a3962360197bf4d5a87c589f5a080138
|
7
|
+
data.tar.gz: 7e404dd6bcf9f5af267aefaef8e8b4facb1508096c946ca9dd3fd90436a4f30af9fe071d997a86ee5689d854f22713faba3c237af733a02ecb3648a09d592fa3
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# The Nature of Code
|
2
|
+
# Daniel Shiffman
|
3
|
+
# http://natureofcode.com
|
4
|
+
|
5
|
+
# A fixed boundary class (now incorporates angle)
|
6
|
+
class Boundary
|
7
|
+
extend Forwardable
|
8
|
+
def_delegators(:@app, :fill, :no_fill, :stroke, :rect, :rect_mode, :box2d,
|
9
|
+
:stroke_weight, :translate, :push_matrix, :pop_matrix, :rotate)
|
10
|
+
# A boundary is a simple rectangle with x,y,width,and height
|
11
|
+
attr_reader :x, :y, :w, :h, :b
|
12
|
+
|
13
|
+
def initialize(x, y, w, h, a)
|
14
|
+
@x, @y, @w, @h, @a = x, y, w, h, a
|
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.angle = a
|
27
|
+
bd.position.set(box2d.processing_to_world(x, y))
|
28
|
+
@b = box2d.create_body(bd)
|
29
|
+
# Attached the shape to the body using a Fixture
|
30
|
+
b.create_fixture(sd, 1)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Draw the boundary, if it were at an angle we'd have to do something fancier
|
34
|
+
def display
|
35
|
+
no_fill
|
36
|
+
stroke(127)
|
37
|
+
fill(127)
|
38
|
+
stroke_weight(1)
|
39
|
+
rect_mode(Java::ProcessingCore::PConstants::CENTER)
|
40
|
+
a = b.get_angle
|
41
|
+
push_matrix
|
42
|
+
translate(x, y)
|
43
|
+
rotate(-a)
|
44
|
+
rect(0, 0, w, h)
|
45
|
+
pop_matrix
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# The Nature of Code
|
2
|
+
# Daniel Shiffman
|
3
|
+
# http://natureofcode.com
|
4
|
+
|
5
|
+
# A rectangular box
|
6
|
+
class Box
|
7
|
+
extend Forwardable
|
8
|
+
def_delegators(:@app, :fill, :stroke, :stroke_weight, :rect, :rect_mode,
|
9
|
+
:box2d, :rotate, :translate, :push_matrix, :pop_matrix)
|
10
|
+
# We need to keep track of a Body and a width and height
|
11
|
+
attr_accessor :body, :w, :h
|
12
|
+
# Constructor
|
13
|
+
def initialize(x, y)
|
14
|
+
@app = $app
|
15
|
+
@w, @h = 24, 24
|
16
|
+
# Add the box to the box2d world
|
17
|
+
make_body(Vec2.new(x, y), w, h)
|
18
|
+
end
|
19
|
+
|
20
|
+
# This function removes the particle from the box2d world
|
21
|
+
def kill_body
|
22
|
+
box2d.destroy_body(body)
|
23
|
+
end
|
24
|
+
|
25
|
+
def contains(x, y)
|
26
|
+
world_point = box2d.processing_to_world(x, y)
|
27
|
+
f = body.get_fixture_list
|
28
|
+
f.test_point(world_point)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Drawing the box
|
32
|
+
def display
|
33
|
+
# We look at each body and get its screen position
|
34
|
+
pos = box2d.body_coord(body)
|
35
|
+
# Get its angle of rotation
|
36
|
+
a = body.getAngle
|
37
|
+
rect_mode(Java::ProcessingCore::PConstants::CENTER)
|
38
|
+
push_matrix
|
39
|
+
translate(pos.x, pos.y)
|
40
|
+
rotate(a)
|
41
|
+
fill(127)
|
42
|
+
stroke(0)
|
43
|
+
stroke_weight(2)
|
44
|
+
rect(0, 0, w, h)
|
45
|
+
pop_matrix
|
46
|
+
end
|
47
|
+
|
48
|
+
# This function adds the rectangle to the box2d world
|
49
|
+
def make_body(center, w, h)
|
50
|
+
# Define and create the body
|
51
|
+
bd = BodyDef.new
|
52
|
+
bd.type = BodyType::DYNAMIC
|
53
|
+
bd.position.set(box2d.processing_to_world(center))
|
54
|
+
@body = box2d.createBody(bd)
|
55
|
+
# Define a polygon (this is what we use for a rectangle)
|
56
|
+
sd = PolygonShape.new
|
57
|
+
box2dw = box2d.scale_to_world(w / 2)
|
58
|
+
box2dh = box2d.scale_to_world(h / 2)
|
59
|
+
sd.setAsBox(box2dw, box2dh)
|
60
|
+
# Define a fixture
|
61
|
+
fd = FixtureDef.new
|
62
|
+
fd.shape = sd
|
63
|
+
# Parameters that affect physics
|
64
|
+
fd.density = 1
|
65
|
+
fd.friction = 0.3
|
66
|
+
fd.restitution = 0.5
|
67
|
+
body.create_fixture(fd)
|
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
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# The Nature of Code
|
2
|
+
# Daniel Shiffman
|
3
|
+
# http://natureofcode.com
|
4
|
+
|
5
|
+
# This class avoids test for nil
|
6
|
+
class DummySpring
|
7
|
+
def initialize; end
|
8
|
+
|
9
|
+
# If it exists we set its target to the mouse location
|
10
|
+
def update(_x, _y); end
|
11
|
+
|
12
|
+
def display; end
|
13
|
+
|
14
|
+
# This is the key function where
|
15
|
+
# we attach the spring to an x,y location
|
16
|
+
# and the Box object's location
|
17
|
+
def bind(x, y, box)
|
18
|
+
spring = Spring.new
|
19
|
+
spring.bind(x, y, box)
|
20
|
+
spring
|
21
|
+
end
|
22
|
+
|
23
|
+
def destroy; end
|
24
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# The Nature of Code
|
2
|
+
# Daniel Shiffman
|
3
|
+
# http://natureofcode.com
|
4
|
+
|
5
|
+
# Basic example of controlling an object with the mouse (by attaching a spring)
|
6
|
+
|
7
|
+
require 'pbox2d'
|
8
|
+
require_relative 'box'
|
9
|
+
require_relative 'boundary'
|
10
|
+
require_relative 'spring'
|
11
|
+
require_relative 'dummy_spring'
|
12
|
+
require 'forwardable'
|
13
|
+
|
14
|
+
# A reference to our box2d world
|
15
|
+
attr_reader :box2d, :boundaries, :box, :spring
|
16
|
+
|
17
|
+
def setup
|
18
|
+
size(640, 360)
|
19
|
+
# Initialize box2d physics and create the world
|
20
|
+
@box2d = Box2D.new self
|
21
|
+
box2d.create_world
|
22
|
+
# Make the box
|
23
|
+
@box = Box.new(width / 2, height / 2)
|
24
|
+
# Make a dummy spring
|
25
|
+
@spring = DummySpring.new
|
26
|
+
# Add a bunch of fixed boundaries
|
27
|
+
@boundaries = []
|
28
|
+
boundaries << Boundary.new(width / 2, height - 5, width, 10, 0)
|
29
|
+
boundaries << Boundary.new(width / 2, 5, width, 10, 0)
|
30
|
+
boundaries << Boundary.new(width - 5, height / 2, 10, height, 0)
|
31
|
+
boundaries << Boundary.new(5, height / 2, 10, height, 0)
|
32
|
+
end
|
33
|
+
|
34
|
+
# When the mouse is released we're done with the spring
|
35
|
+
def mouse_released
|
36
|
+
spring.destroy
|
37
|
+
@spring = DummySpring.new
|
38
|
+
end
|
39
|
+
|
40
|
+
# When the mouse is pressed we. . .
|
41
|
+
def mouse_pressed
|
42
|
+
# Check to see if the mouse was clicked on the box and if so create
|
43
|
+
# a real spring and bind the mouse location to the box with a spring
|
44
|
+
@spring = spring.bind(mouse_x, mouse_y, box) if box.contains(mouse_x, mouse_y)
|
45
|
+
end
|
46
|
+
|
47
|
+
def draw
|
48
|
+
background(255)
|
49
|
+
# Always alert the spring to the new mouse location
|
50
|
+
spring.update(mouse_x, mouse_y)
|
51
|
+
# Draw the boundaries
|
52
|
+
boundaries.each(&:display)
|
53
|
+
# Draw the box
|
54
|
+
box.display
|
55
|
+
# Draw the spring (it only appears when active)
|
56
|
+
spring.display
|
57
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# The Nature of Code
|
2
|
+
# Daniel Shiffman
|
3
|
+
# http://natureofcode.com
|
4
|
+
|
5
|
+
# Class to describe the spring joint (displayed as a line)
|
6
|
+
class Spring
|
7
|
+
extend Forwardable
|
8
|
+
def_delegators(:@app, :line, :box2d, :stroke, :stroke_weight)
|
9
|
+
# This is the box2d object we need to create
|
10
|
+
attr_reader :mouse_joint
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@app = $app
|
14
|
+
end
|
15
|
+
|
16
|
+
# If it exists we set its target to the mouse location
|
17
|
+
def update(x, y)
|
18
|
+
# Always convert to world coordinates!
|
19
|
+
mouse_world = box2d.processing_to_world(x, y)
|
20
|
+
mouse_joint.set_target(mouse_world)
|
21
|
+
end
|
22
|
+
|
23
|
+
def display
|
24
|
+
# We can get the two anchor points
|
25
|
+
v1 = Vec2.new
|
26
|
+
mouse_joint.getAnchorA(v1)
|
27
|
+
v2 = Vec2.new
|
28
|
+
mouse_joint.getAnchorB(v2)
|
29
|
+
# Convert them to screen coordinates
|
30
|
+
vd1 = box2d.world_to_processing(v1)
|
31
|
+
vd2 = box2d.world_to_processing(v2)
|
32
|
+
# And just draw a line
|
33
|
+
stroke(0)
|
34
|
+
stroke_weight(1)
|
35
|
+
line(vd1.x, vd1.y, vd2.x, vd2.y)
|
36
|
+
end
|
37
|
+
|
38
|
+
# This is the key function where
|
39
|
+
# we attach the spring to an x,y location
|
40
|
+
# and the Box object's location
|
41
|
+
def bind(x, y, box)
|
42
|
+
# Define the joint
|
43
|
+
md = MouseJointDef.new
|
44
|
+
# Body A is just a fake ground body for simplicity
|
45
|
+
# (there isn't anything at the mouse)
|
46
|
+
md.bodyA = box2d.ground_body
|
47
|
+
# Body 2 is the box's boxy
|
48
|
+
md.bodyB = box.body
|
49
|
+
# Get the mouse location in world coordinates
|
50
|
+
mp = box2d.processing_to_world(x, y)
|
51
|
+
# And that's the target
|
52
|
+
md.target.set(mp)
|
53
|
+
# Some stuff about how strong and bouncy the spring should be
|
54
|
+
md.maxForce = 1000.0 * box.body.m_mass
|
55
|
+
md.frequencyHz = 5.0
|
56
|
+
md.dampingRatio = 0.9
|
57
|
+
# Make the joint!
|
58
|
+
@mouse_joint = box2d.world.create_joint(md)
|
59
|
+
end
|
60
|
+
|
61
|
+
def destroy
|
62
|
+
# We can get rid of the joint when the mouse is released
|
63
|
+
box2d.world.destroy_joint(mouse_joint)
|
64
|
+
end
|
65
|
+
end
|
data/lib/box2d.jar
CHANGED
Binary file
|
data/lib/pbox2d/version.rb
CHANGED
data/lib/pbox2d.rb
CHANGED
@@ -19,7 +19,7 @@ import_class_list(shape, shape_format)
|
|
19
19
|
world = %w( Body BodyDef BodyType World FixtureDef )
|
20
20
|
world_format = 'org.jbox2d.dynamics.%s'
|
21
21
|
import_class_list(world, world_format)
|
22
|
-
joint = %w( Joint JointDef DistanceJointDef RevoluteJoint RevoluteJointDef )
|
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
25
|
|
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.4.
|
4
|
+
version: 0.4.1
|
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-04-
|
11
|
+
date: 2015-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,6 +86,11 @@ files:
|
|
86
86
|
- examples/lib/surface.rb
|
87
87
|
- examples/liquid_fun_test.rb
|
88
88
|
- examples/liquidy.rb
|
89
|
+
- examples/mouse_joint/boundary.rb
|
90
|
+
- examples/mouse_joint/box.rb
|
91
|
+
- examples/mouse_joint/dummy_spring.rb
|
92
|
+
- examples/mouse_joint/mouse_joint.rb
|
93
|
+
- examples/mouse_joint/spring.rb
|
89
94
|
- examples/polygons.rb
|
90
95
|
- examples/quick_test.rb
|
91
96
|
- examples/revolute_joint/box.rb
|