technohippy-Pongo 0.1.0
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.
- data/History.txt +17 -0
- data/Manifest.txt +52 -0
- data/README.txt +93 -0
- data/lib/pongo.rb +33 -0
- data/lib/pongo/abstract_collection.rb +135 -0
- data/lib/pongo/abstract_constraint.rb +25 -0
- data/lib/pongo/abstract_item.rb +93 -0
- data/lib/pongo/abstract_particle.rb +226 -0
- data/lib/pongo/angular_constraint.rb +108 -0
- data/lib/pongo/apengine.rb +157 -0
- data/lib/pongo/circle_particle.rb +36 -0
- data/lib/pongo/collision.rb +11 -0
- data/lib/pongo/collision_detector.rb +222 -0
- data/lib/pongo/collision_event.rb +23 -0
- data/lib/pongo/collision_resolver.rb +39 -0
- data/lib/pongo/composite.rb +52 -0
- data/lib/pongo/container/container.rb +7 -0
- data/lib/pongo/container/shoes_container.rb +14 -0
- data/lib/pongo/group.rb +166 -0
- data/lib/pongo/iforce.rb +7 -0
- data/lib/pongo/logger/logger.rb +21 -0
- data/lib/pongo/logger/shoes_logger.rb +15 -0
- data/lib/pongo/logger/standard_logger.rb +11 -0
- data/lib/pongo/rectangle_particle.rb +79 -0
- data/lib/pongo/renderer/renderer.rb +61 -0
- data/lib/pongo/renderer/shoes_renderer.rb +51 -0
- data/lib/pongo/renderer/tk_renderer.rb +73 -0
- data/lib/pongo/rim_particle.rb +53 -0
- data/lib/pongo/spring_constraint.rb +128 -0
- data/lib/pongo/spring_constraint_particle.rb +235 -0
- data/lib/pongo/util/interval.rb +13 -0
- data/lib/pongo/util/math_util.rb +27 -0
- data/lib/pongo/util/numeric_ext.rb +4 -0
- data/lib/pongo/util/vector.rb +133 -0
- data/lib/pongo/vector_force.rb +34 -0
- data/lib/pongo/wheel_particle.rb +103 -0
- metadata +90 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
History.txt
|
2
|
+
lib/pongo/abstract_collection.rb
|
3
|
+
lib/pongo/abstract_constraint.rb
|
4
|
+
lib/pongo/abstract_item.rb
|
5
|
+
lib/pongo/abstract_particle.rb
|
6
|
+
lib/pongo/angular_constraint.rb
|
7
|
+
lib/pongo/apengine.rb
|
8
|
+
lib/pongo/circle_particle.rb
|
9
|
+
lib/pongo/collision.rb
|
10
|
+
lib/pongo/collision_detector.rb
|
11
|
+
lib/pongo/collision_event.rb
|
12
|
+
lib/pongo/collision_resolver.rb
|
13
|
+
lib/pongo/util/interval.rb
|
14
|
+
lib/pongo/util/math_util.rb
|
15
|
+
lib/pongo/util/numeric_ext.rb
|
16
|
+
lib/pongo/util/vector.rb
|
17
|
+
lib/pongo/composite.rb
|
18
|
+
lib/pongo/group.rb
|
19
|
+
lib/pongo/iforce.rb
|
20
|
+
lib/pongo/logger/logger.rb
|
21
|
+
lib/pongo/logger/shoes_logger.rb
|
22
|
+
lib/pongo/logger/standard_logger.rb
|
23
|
+
lib/pongo/rectangle_particle.rb
|
24
|
+
lib/pongo/renderer/renderer.rb
|
25
|
+
lib/pongo/renderer/shoes_renderer.rb
|
26
|
+
lib/pongo/renderer/tk_renderer.rb
|
27
|
+
lib/pongo/rim_particle.rb
|
28
|
+
lib/pongo/spring_constraint.rb
|
29
|
+
lib/pongo/spring_constraint_particle.rb
|
30
|
+
lib/pongo/vector_force.rb
|
31
|
+
lib/pongo/wheel_particle.rb
|
32
|
+
lib/pongo.rb
|
33
|
+
Manifest.txt
|
34
|
+
Rakefile
|
35
|
+
README.txt
|
36
|
+
sample/cardemo/bridge.rb
|
37
|
+
sample/cardemo/capsule.rb
|
38
|
+
sample/cardemo/car.rb
|
39
|
+
sample/cardemo/rect_composite.rb
|
40
|
+
sample/cardemo/rotator.rb
|
41
|
+
sample/cardemo/surfaces.rb
|
42
|
+
sample/cardemo/swing_door.rb
|
43
|
+
sample/robodemo/body.rb
|
44
|
+
sample/robodemo/leg.rb
|
45
|
+
sample/robodemo/motor.rb
|
46
|
+
sample/robodemo/robot.rb
|
47
|
+
sample/shoes/basic.rb
|
48
|
+
sample/shoes/cardemo.rb
|
49
|
+
sample/shoes/robodemo.rb
|
50
|
+
sample/shoes/simple.rb
|
51
|
+
sample/tk/basic.rb
|
52
|
+
sample/tk/robotdemo.rb
|
data/README.txt
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
= Pongo
|
2
|
+
|
3
|
+
* http://github.com/technohippy/Pongo/
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Ruby port of APE (Actionscript Physics Engine)
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* CircleParticles
|
12
|
+
* RectangleParticles
|
13
|
+
* WheelParticles
|
14
|
+
* SpringConstraints
|
15
|
+
* Grouping
|
16
|
+
* Collision
|
17
|
+
|
18
|
+
see also: http://www.cove.org/ape/index.htm
|
19
|
+
|
20
|
+
== EXECUTE SAMPLES:
|
21
|
+
|
22
|
+
* ruby sample/tk/robotdemo.rb
|
23
|
+
* shoes sample/shoes/robotdemo.rb
|
24
|
+
* shoes sample/shoes/cardemo.rb
|
25
|
+
|
26
|
+
== SYNOPSIS:
|
27
|
+
|
28
|
+
require 'pongo'
|
29
|
+
require 'pongo/renderer/shoes_renderer'
|
30
|
+
require 'pongo/logger/shoes_logger'
|
31
|
+
|
32
|
+
include Pongo
|
33
|
+
Shoes.app :width => 500, :height => 350 do
|
34
|
+
APEngine.renderer = Renderer::ShoesRenderer.new(self)
|
35
|
+
APEngine.logger = Logger::ShoesLogger.new(self)
|
36
|
+
APEngine.setup
|
37
|
+
APEngine.add_force VectorForce.new(false, 0, 2)
|
38
|
+
|
39
|
+
default_group = Group.new
|
40
|
+
default_group.collide_internal = true
|
41
|
+
|
42
|
+
ball = CircleParticle.new(245, 100, 10)
|
43
|
+
default_group.add_particle(ball)
|
44
|
+
|
45
|
+
ground = RectangleParticle.new(250, 250, 300, 50, 0, true)
|
46
|
+
ground.always_redraw!
|
47
|
+
default_group.add_particle(ground)
|
48
|
+
|
49
|
+
APEngine.add_group(default_group)
|
50
|
+
|
51
|
+
animate(60) do |anim|
|
52
|
+
APEngine.step
|
53
|
+
APEngine.draw
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
== REQUIREMENTS:
|
58
|
+
|
59
|
+
* Shoes2 ( http://shoooes.net/ )
|
60
|
+
|
61
|
+
== INSTALL:
|
62
|
+
|
63
|
+
* sudo gem install pongo
|
64
|
+
|
65
|
+
== IMAGES:
|
66
|
+
|
67
|
+
* http://www.flickr.com/photos/24557420@N05/3366850475/
|
68
|
+
* http://www.aa.alles.or.jp/~palette/icon/icon.html
|
69
|
+
|
70
|
+
== LICENSE:
|
71
|
+
|
72
|
+
(The MIT License)
|
73
|
+
|
74
|
+
Copyright (c) 2009 Ando Yasushi
|
75
|
+
|
76
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
77
|
+
a copy of this software and associated documentation files (the
|
78
|
+
'Software'), to deal in the Software without restriction, including
|
79
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
80
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
81
|
+
permit persons to whom the Software is furnished to do so, subject to
|
82
|
+
the following conditions:
|
83
|
+
|
84
|
+
The above copyright notice and this permission notice shall be
|
85
|
+
included in all copies or substantial portions of the Software.
|
86
|
+
|
87
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
88
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
89
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
90
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
91
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
92
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
93
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/pongo.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
$: << File.expand_path(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'pongo/abstract_collection'
|
4
|
+
require 'pongo/abstract_constraint'
|
5
|
+
require 'pongo/abstract_item'
|
6
|
+
require 'pongo/abstract_particle'
|
7
|
+
require 'pongo/angular_constraint'
|
8
|
+
require 'pongo/apengine'
|
9
|
+
require 'pongo/circle_particle'
|
10
|
+
require 'pongo/collision'
|
11
|
+
require 'pongo/collision_detector'
|
12
|
+
require 'pongo/collision_event'
|
13
|
+
require 'pongo/collision_resolver'
|
14
|
+
require 'pongo/composite'
|
15
|
+
require 'pongo/group'
|
16
|
+
require 'pongo/iforce'
|
17
|
+
require 'pongo/rectangle_particle'
|
18
|
+
require 'pongo/rim_particle'
|
19
|
+
require 'pongo/spring_constraint'
|
20
|
+
require 'pongo/spring_constraint_particle'
|
21
|
+
require 'pongo/vector_force'
|
22
|
+
require 'pongo/wheel_particle'
|
23
|
+
|
24
|
+
require 'pongo/util/interval'
|
25
|
+
require 'pongo/util/math_util'
|
26
|
+
require 'pongo/util/numeric_ext'
|
27
|
+
require 'pongo/util/vector'
|
28
|
+
|
29
|
+
module Pongo
|
30
|
+
VERSION = '0.1.0'
|
31
|
+
class SubclassResponsibilityError < StandardError; end
|
32
|
+
class UnknownItemError < StandardError; end
|
33
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'pongo/abstract_item'
|
2
|
+
|
3
|
+
module Pongo
|
4
|
+
class AbstractCollection < AbstractItem
|
5
|
+
attr_accessor :sprite, :particles, :constraints, :is_parented
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
super
|
9
|
+
self.is_parented = false
|
10
|
+
self.particles = []
|
11
|
+
self.constraints = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def <<(item)
|
15
|
+
case item
|
16
|
+
when AbstractParticle
|
17
|
+
add_particle(item)
|
18
|
+
when AbstractConstraint
|
19
|
+
add_constraint(item)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_particle(particle)
|
24
|
+
particle.init if parented?
|
25
|
+
particles << particle
|
26
|
+
end
|
27
|
+
|
28
|
+
def remove_particle(particle)
|
29
|
+
if particles.delete(particle)
|
30
|
+
particle.cleanup
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_constraint(constraint)
|
35
|
+
constraint.init if parented?
|
36
|
+
constraints << constraint
|
37
|
+
end
|
38
|
+
|
39
|
+
def remove_constraint(constraint)
|
40
|
+
if constraints.delete(constraint)
|
41
|
+
constraint.cleanup
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def init
|
46
|
+
particles.each {|p| p.init}
|
47
|
+
constraints.each {|c| c.init}
|
48
|
+
end
|
49
|
+
|
50
|
+
def draw
|
51
|
+
if renderer
|
52
|
+
renderer.draw(self)
|
53
|
+
else
|
54
|
+
particles.each {|p| p.draw if p.always_redraw? or not p.fixed?}
|
55
|
+
constraints.each {|c| c.draw if c.always_redraw? or not c.fixed?}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
alias paint draw
|
59
|
+
|
60
|
+
def cleanup
|
61
|
+
if renderer
|
62
|
+
renderer.cleanup(self)
|
63
|
+
else
|
64
|
+
particles.each {|p| p.cleanup}
|
65
|
+
constraints.each {|c| c.cleanup}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def all
|
70
|
+
particles + constraints
|
71
|
+
end
|
72
|
+
alias get_all all
|
73
|
+
|
74
|
+
def parented?
|
75
|
+
@is_parented
|
76
|
+
end
|
77
|
+
|
78
|
+
def integrate(dt2)
|
79
|
+
particles.each {|p| p.update(dt2)}
|
80
|
+
end
|
81
|
+
|
82
|
+
def satisfy_constraints
|
83
|
+
constraints.each {|c| c.resolve}
|
84
|
+
end
|
85
|
+
|
86
|
+
def collidable_particles(constraint=nil)
|
87
|
+
particles.select do |p|
|
88
|
+
p.collidable? and (constraint.nil? or not constraint.is_connected_to?(p))
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def collidable_constraints(particle=nil)
|
93
|
+
constraints.select do |c|
|
94
|
+
c.collidable? and (particle.nil? or not c.is_connected_to?(particle))
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def check_internal_collisions
|
99
|
+
collidable_particles.each do |pa|
|
100
|
+
collidable_particles.each do |pb|
|
101
|
+
CollisionDetector.test(pa, pb)
|
102
|
+
end
|
103
|
+
|
104
|
+
collidable_constraints(pa).each do |c|
|
105
|
+
c.scp.update_position
|
106
|
+
CollisionDetector.test(pa, c.scp)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def check_collisions_vs_collection(collection)
|
112
|
+
# every particle in this collection...
|
113
|
+
collidable_particles.each do |pga|
|
114
|
+
# ...vs every particle in the other collection
|
115
|
+
collection.collidable_particles.each do |pgb|
|
116
|
+
CollisionDetector.test(pga, pgb)
|
117
|
+
end
|
118
|
+
|
119
|
+
# ...vs every constraint in the other collection
|
120
|
+
collection.collidable_constraints(pga).each do |cgb|
|
121
|
+
cgb.scp.update_position
|
122
|
+
CollisionDetector.test(pga, cgb.scp)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# every constraint in this collection...
|
127
|
+
collidable_constraints.each do |cga|
|
128
|
+
collection.collidable_particles(cga).each do |pgb|
|
129
|
+
cga.scp.update_position
|
130
|
+
CollisionDetector.test(pgb, cga.scp)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'pongo/abstract_item'
|
2
|
+
|
3
|
+
module Pongo
|
4
|
+
# The abstract base class for all constraints.
|
5
|
+
#
|
6
|
+
# You should not instantiate this class directly -- instead use one of the subclasses.
|
7
|
+
class AbstractConstraint < AbstractItem
|
8
|
+
# The stiffness of the constraint. Higher values result in result in
|
9
|
+
# stiffer constraints. Values should be > 0 and <= 1. Depending on the situation,
|
10
|
+
# setting constraints to very high values may result in instability.
|
11
|
+
attr_accessor :stiffness
|
12
|
+
|
13
|
+
def initialize(stiffness)
|
14
|
+
super()
|
15
|
+
@stiffness = stiffness
|
16
|
+
set_style
|
17
|
+
end
|
18
|
+
|
19
|
+
# Corrects the position of the attached particles based on their position and
|
20
|
+
# mass. This method is called automatically during the APEngine.step() cycle.
|
21
|
+
def resolve
|
22
|
+
raise SubclassResponsibilityError
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module Pongo
|
2
|
+
class AbstractItem
|
3
|
+
attr_accessor :solid, :visible, :always_repaint, :renderer
|
4
|
+
attr_reader :line_thickness, :line_color, :line_alpha, :fill_color, :fill_alpha, :display_object, :display_object_offset, :display_object_rotation
|
5
|
+
attr_reader :user_data
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@solid = true
|
9
|
+
@visible = true
|
10
|
+
@always_repaint = false
|
11
|
+
@events = Hash.new
|
12
|
+
@user_data = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def visible?
|
16
|
+
@visible
|
17
|
+
end
|
18
|
+
|
19
|
+
def visible!
|
20
|
+
@visible = true
|
21
|
+
end
|
22
|
+
|
23
|
+
def renderer
|
24
|
+
@renderer || APEngine.renderer
|
25
|
+
end
|
26
|
+
|
27
|
+
# This method is automatically called when an item's parent group is added to the engine,
|
28
|
+
# an item's Composite is added to a Group, or the item is added to a Composite or Group.
|
29
|
+
def init
|
30
|
+
cleanup
|
31
|
+
draw
|
32
|
+
end
|
33
|
+
|
34
|
+
def cleanup
|
35
|
+
renderer.cleanup(self)
|
36
|
+
end
|
37
|
+
|
38
|
+
def draw
|
39
|
+
renderer.draw(self)
|
40
|
+
end
|
41
|
+
alias paint draw
|
42
|
+
|
43
|
+
def has_event_listener(event_type)
|
44
|
+
@events[event_type]
|
45
|
+
end
|
46
|
+
|
47
|
+
def add_event_listener(event_type, callable=nil, &block)
|
48
|
+
(@events[event_type] ||= []) << (callable || block)
|
49
|
+
end
|
50
|
+
|
51
|
+
def dispatch_event(event)
|
52
|
+
(@events[event.type] || []).each do |listener|
|
53
|
+
listener.call(event)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# For performance, fixed Particles and SpringConstraints don't have their <code>paint()</code>
|
58
|
+
# method called in order to avoid unnecessary redrawing. A SpringConstraint is considered
|
59
|
+
# fixed if its two connecting Particles are fixed. Setting this property to <code>true</code>
|
60
|
+
# forces <code>paint()</code> to be called if this Particle or SpringConstraint <code>fixed</code>
|
61
|
+
# property is true. If you are rotating a fixed Particle or SpringConstraint then you would set
|
62
|
+
# it's repaintFixed property to true. This property has no effect if a Particle or
|
63
|
+
# SpringConstraint is not fixed.
|
64
|
+
def always_redraw?
|
65
|
+
@always_repaint
|
66
|
+
end
|
67
|
+
alias always_redraw always_redraw?
|
68
|
+
|
69
|
+
def always_redraw=(bool)
|
70
|
+
@always_repaint = bool
|
71
|
+
end
|
72
|
+
|
73
|
+
def always_redraw!
|
74
|
+
@always_repaint = true
|
75
|
+
end
|
76
|
+
|
77
|
+
def set_style(line_thickness=0, line_color=0x000000, line_alpha=1, fill_color=0xffffff, fill_alpha=1)
|
78
|
+
set_line(line_thickness, line_color, line_alpha)
|
79
|
+
set_fill(fill_color, fill_alpha)
|
80
|
+
end
|
81
|
+
|
82
|
+
def set_line(thickness=0, color=0x000000, alpha=1)
|
83
|
+
@line_thickness = thickness
|
84
|
+
@line_color = color
|
85
|
+
@line_alpha = alpha
|
86
|
+
end
|
87
|
+
|
88
|
+
def set_fill(color=0xffffff, alpha=1)
|
89
|
+
@fill_color = color
|
90
|
+
@fill_alpha = alpha
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|