chippunk 0.0.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/README +3 -0
- data/Rakefile +25 -0
- data/examples/world.rb +94 -0
- data/lib/chippunk.rb +89 -0
- data/lib/chippunk/builder.rb +41 -0
- data/lib/chippunk/helper.rb +25 -0
- data/lib/chippunk/object.rb +80 -0
- data/lib/chippunk/world.rb +41 -0
- data/lib/cp/shape.rb +0 -0
- data/lib/cp/shape/circle.rb +0 -0
- data/lib/cp/shape/poly.rb +0 -0
- data/lib/cp/shape/segment.rb +0 -0
- metadata +109 -0
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |spec|
|
4
|
+
spec.name = "chippunk"
|
5
|
+
spec.summary = "Wrapper For Chipmunk Physics"
|
6
|
+
spec.description = "No long description available but is probably coming soon"
|
7
|
+
spec.email = "mhennig@hgb-leipzig.de"
|
8
|
+
spec.homepage = "http://github.com/mhennig/chippunk"
|
9
|
+
spec.authors = ["Matthias Hennig"]
|
10
|
+
spec.post_install_message = %q{
|
11
|
+
========================================================================
|
12
|
+
Thanks for installing Chippunk!
|
13
|
+
------------------------------------------------------------------------
|
14
|
+
}
|
15
|
+
spec.require_path = 'lib'
|
16
|
+
spec.files = %w(README Rakefile init.rb) + Dir.glob("{lib}/**/*")
|
17
|
+
|
18
|
+
# Runtime dependencies: When installing Formtastic these will be checked if they are installed.
|
19
|
+
# Will be offered to install these if they are not already installed.
|
20
|
+
spec.add_dependency 'gosu', '>= 0.7.0'
|
21
|
+
spec.add_dependency 'chipmunk', '>= 5.2.0'
|
22
|
+
end
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
25
|
+
end
|
data/examples/world.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require "../lib/chippunk"
|
5
|
+
|
6
|
+
module Dimensions
|
7
|
+
Mothership = [
|
8
|
+
[-30.0, 60.0],
|
9
|
+
[30, 60.0],
|
10
|
+
[30, -60],
|
11
|
+
[-30, -60]
|
12
|
+
]
|
13
|
+
end
|
14
|
+
|
15
|
+
class Game < Gosu::Window
|
16
|
+
|
17
|
+
include Chippunk::Builder
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
super(1024, 768, false)
|
21
|
+
self.caption = "WORLD!!"
|
22
|
+
|
23
|
+
@world = Chippunk::World.new do |config|
|
24
|
+
config.gravity = CP::Vec2.new(0.0, 1800.0)
|
25
|
+
config.damping = 0.3
|
26
|
+
config.iterations = 20
|
27
|
+
config.substeps = 20
|
28
|
+
config.dt = (1.0/60) / 20
|
29
|
+
end
|
30
|
+
|
31
|
+
ground = build_segment_object(1024, CP::INFINITY, CP::INFINITY) do |config|
|
32
|
+
config.position = [1024/2, 760]
|
33
|
+
config.friction = 0.9
|
34
|
+
config.static!
|
35
|
+
end
|
36
|
+
|
37
|
+
@ship = build_polygon_object(Dimensions::Mothership, 10, 900) do |ship|
|
38
|
+
ship.position = [600 + rand(300), 10 + rand(30)]
|
39
|
+
ship.elasticity = 0.5
|
40
|
+
ship.friction = 0.3
|
41
|
+
end
|
42
|
+
|
43
|
+
balls = Array.new(50) do |n|
|
44
|
+
build_circle_object(rand(30)+10, rand(3)+1, 0.4) do |ball|
|
45
|
+
ball.position = [200+rand(400), 10 + rand(10)]
|
46
|
+
ball.elasticity = 0.9
|
47
|
+
ball.friction = 0.1
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
@world.add_objects(@ship)
|
52
|
+
@world.add_objects(*balls)
|
53
|
+
@world.add_objects(ground)
|
54
|
+
end
|
55
|
+
|
56
|
+
def update
|
57
|
+
@world.update do
|
58
|
+
@ship.body.reset_forces
|
59
|
+
@ship.body.apply_force(vec2(0, -18000), vec2(0,0))
|
60
|
+
if self.button_down?(Gosu::KbUp)
|
61
|
+
@ship.body.apply_force(vec2(0, -10000), vec2(0,0))
|
62
|
+
puts "btn down"
|
63
|
+
end
|
64
|
+
if self.button_down?(Gosu::KbDown)
|
65
|
+
@ship.body.apply_force(vec2(0, 9000), vec2(0,0))
|
66
|
+
end
|
67
|
+
if self.button_down?(Gosu::KbLeft)
|
68
|
+
@ship.body.apply_force(vec2(-9000,0), vec2(0,0))
|
69
|
+
end
|
70
|
+
if self.button_down?(Gosu::KbRight)
|
71
|
+
@ship.body.apply_force(vec2(9000,0), vec2(0,0))
|
72
|
+
end
|
73
|
+
|
74
|
+
puts @ship.body.vel
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def button_down(id)
|
79
|
+
case id
|
80
|
+
when Gosu::KbEscape
|
81
|
+
close
|
82
|
+
when Gosu::KbUp
|
83
|
+
#@ship.body.apply_force(vec2(0, -10), vec2(0,0))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def draw
|
88
|
+
self.draw_quad(0, 0, Gosu::Color.new(0xff002040), 1024, 0, Gosu::Color.new(0xff002040), 0, 768, Gosu::Color.new(0xff002040), 1024, 768, Gosu::Color.new(0xff002040), 0)
|
89
|
+
@world.draw(self)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
window = Game.new.show
|
94
|
+
|
data/lib/chippunk.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require "rubygems"
|
5
|
+
require "gosu"
|
6
|
+
#require 'chipmunk-ffi'
|
7
|
+
require 'chipmunk'
|
8
|
+
|
9
|
+
require 'chippunk/helper.rb'
|
10
|
+
require 'chippunk/world.rb'
|
11
|
+
require 'chippunk/object.rb'
|
12
|
+
require 'chippunk/builder.rb'
|
13
|
+
|
14
|
+
unless [].respond_to?(:enum_cons)
|
15
|
+
module Enumerable
|
16
|
+
alias :enum_cons :each_cons
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Numeric
|
21
|
+
def radians_to_vec2
|
22
|
+
CP::Vec2.new(Math::cos(self), Math::sin(self))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module CP
|
27
|
+
module Shape
|
28
|
+
|
29
|
+
class Segment
|
30
|
+
attr_reader :a, :b, :radius
|
31
|
+
alias_method :orig_init, :initialize
|
32
|
+
|
33
|
+
def initialize(body, a, b, radius)
|
34
|
+
@a, @b, @radius = a, b, radius
|
35
|
+
orig_init(body, a, b, radius)
|
36
|
+
end
|
37
|
+
|
38
|
+
def draw(canvas)
|
39
|
+
v1 = body.p + self.a.rotate(body.rot)
|
40
|
+
v2 = body.p + self.b.rotate(body.rot)
|
41
|
+
canvas.draw_line(v1.x, v1.y, Gosu::Color.new(0xffff0000), v2.x, v2.y, Gosu::Color.new(0xffff0000))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Circle
|
46
|
+
attr_reader :radius, :center
|
47
|
+
alias_method :orig_init, :initialize
|
48
|
+
|
49
|
+
def initialize(body, radius, center)
|
50
|
+
@radius, @center = radius, center
|
51
|
+
orig_init(body,radius,center)
|
52
|
+
end
|
53
|
+
|
54
|
+
def draw(canvas)
|
55
|
+
c = body.p + self.center.rotate(body.rot)
|
56
|
+
a = body.a
|
57
|
+
segs = 20
|
58
|
+
coef = 2.0*Math::PI/(segs.to_f)
|
59
|
+
|
60
|
+
points = Array.new(segs) do |n|
|
61
|
+
rads = n*coef
|
62
|
+
[radius*Math.cos(rads+a) + c.x, radius*Math.sin(rads + a) + c.y]
|
63
|
+
end
|
64
|
+
|
65
|
+
points.enum_cons(2) do |v1,v2|
|
66
|
+
canvas.draw_line(v1[0], v1[1], Gosu::Color.new(0xffff0000), v2[0], v2[1], Gosu::Color.new(0xffff0000))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class Poly
|
72
|
+
attr_reader :verts, :offset
|
73
|
+
alias_method :orig_init, :initialize
|
74
|
+
|
75
|
+
def initialize(body, verts, offset)
|
76
|
+
@verts, @offset = verts, offset
|
77
|
+
orig_init(body, verts, offset)
|
78
|
+
end
|
79
|
+
|
80
|
+
def draw(canvas)
|
81
|
+
ary = verts.map {|v| body.p + self.offset + v.rotate(body.rot)}
|
82
|
+
segs = ary.enum_cons(2).to_a << [ary[-1],ary[0]]
|
83
|
+
segs.each do |v1,v2|
|
84
|
+
canvas.draw_line(v1.x,v1.y,Gosu::Color.new(0xffff0000),v2.x,v2.y,Gosu::Color.new(0xffff0000))
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'chippunk/object.rb'
|
2
|
+
require 'chippunk/helper.rb'
|
3
|
+
|
4
|
+
module Chippunk
|
5
|
+
module Builder
|
6
|
+
|
7
|
+
include Chippunk::Helper
|
8
|
+
|
9
|
+
def build_segment_object(width, mass=0, inertia=0, radius=0.0)
|
10
|
+
obj = Chippunk::Object.new
|
11
|
+
a = vec2(width/2,0)
|
12
|
+
b = vec2(width/2*-1,0)
|
13
|
+
obj.shape = CP::Shape::Segment.new(obj.body(mass, inertia), a, b, radius)
|
14
|
+
yield(obj) if block_given?
|
15
|
+
obj
|
16
|
+
end
|
17
|
+
|
18
|
+
def build_circle_object(size, mass=0, moment=0, offset=[0,0], &block)
|
19
|
+
obj = Chippunk::Object.new
|
20
|
+
moment = CP.moment_for_circle(mass, size/2, 0.0, vec2(0,0))
|
21
|
+
obj.shape = CP::Shape::Circle.new(obj.body(mass, 100000), size/2, vec2(0,0))
|
22
|
+
yield(obj) if block_given?
|
23
|
+
obj
|
24
|
+
end
|
25
|
+
|
26
|
+
def build_polygon_object(list_of_coords, mass=0, inertia=0)
|
27
|
+
obj = Chippunk::Object.new
|
28
|
+
verts = coords_to_verts(list_of_coords)
|
29
|
+
moment = CP.moment_for_poly(mass, verts, vec2(0,0))
|
30
|
+
obj.shape = CP::Shape::Poly.new(obj.body(mass, CP::INFINITY), verts, vec2(0,0))
|
31
|
+
obj.angle = (3*Math::PI/2.0)
|
32
|
+
yield(obj) if block_given?
|
33
|
+
obj
|
34
|
+
end
|
35
|
+
|
36
|
+
def build_rectangle_object(width, height, mass=0, inertia=0)
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
require "pp"
|
3
|
+
|
4
|
+
module Chippunk
|
5
|
+
module Helper
|
6
|
+
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
def coords_to_verts(list_of_coords)
|
10
|
+
verts = []
|
11
|
+
|
12
|
+
#puts list_of_coords
|
13
|
+
|
14
|
+
#puts " + + + + + + + +"
|
15
|
+
|
16
|
+
list_of_coords.flatten.each_slice(2) do |coords|
|
17
|
+
x,y = coords.values_at(0,1)
|
18
|
+
verts.push(vec2(x,y))
|
19
|
+
end
|
20
|
+
#puts verts
|
21
|
+
#exit
|
22
|
+
verts
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Chippunk
|
2
|
+
class Object
|
3
|
+
include Chippunk::Helper
|
4
|
+
|
5
|
+
attr :body
|
6
|
+
attr_reader :shape, :view
|
7
|
+
attr_accessor :world
|
8
|
+
|
9
|
+
def initialize(*args)
|
10
|
+
super
|
11
|
+
@is_static = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def shape=(shape)
|
15
|
+
@shape ||= shape
|
16
|
+
end
|
17
|
+
|
18
|
+
def body(mass=0, inertia=0)
|
19
|
+
@body ||= CP::Body.new(mass, inertia)
|
20
|
+
end
|
21
|
+
|
22
|
+
def position=(coords)
|
23
|
+
x, y = coords.values_at(0, 1)
|
24
|
+
puts x, y
|
25
|
+
|
26
|
+
body.pos = CP::Vec2.new(x.to_f,y.to_f)
|
27
|
+
end
|
28
|
+
|
29
|
+
def mass=(m)
|
30
|
+
body.mass = m
|
31
|
+
end
|
32
|
+
|
33
|
+
def angle=(a)
|
34
|
+
body.angle = a
|
35
|
+
end
|
36
|
+
|
37
|
+
def inertia=(i)
|
38
|
+
body.i = 0
|
39
|
+
end
|
40
|
+
|
41
|
+
def velocity=(v)
|
42
|
+
x_direction, y_direction = v.values_at(0,1)
|
43
|
+
body.vel = vec2(x_direction, y_direction)
|
44
|
+
end
|
45
|
+
|
46
|
+
def elasticity=(e)
|
47
|
+
shape.e = e
|
48
|
+
end
|
49
|
+
|
50
|
+
def friction=(u)
|
51
|
+
shape.u = u
|
52
|
+
end
|
53
|
+
|
54
|
+
def collision_type=(type)
|
55
|
+
shape.collision_type = type.to_sym
|
56
|
+
end
|
57
|
+
|
58
|
+
def static!
|
59
|
+
@is_static = true
|
60
|
+
end
|
61
|
+
|
62
|
+
def static?
|
63
|
+
@is_static
|
64
|
+
end
|
65
|
+
|
66
|
+
def draw(canvas)
|
67
|
+
unless view.respond_to?(:draw)
|
68
|
+
@shape.draw(canvas)
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class StaticObject < Chippunk::Object
|
75
|
+
def initialize(*args)
|
76
|
+
super
|
77
|
+
@is_static = true
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Chippunk
|
2
|
+
class World
|
3
|
+
attr_reader :space, :objects
|
4
|
+
attr_accessor :dt, :substeps
|
5
|
+
|
6
|
+
def initialize(&block)
|
7
|
+
@space = CP::Space.new
|
8
|
+
@objects = []
|
9
|
+
yield(self) if block_given?
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing(method, *args, &block)
|
13
|
+
@space.send(method, *args, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_object(obj)
|
17
|
+
obj.world = self
|
18
|
+
obj.shape.obj = obj
|
19
|
+
@space.add_shape(obj.shape)
|
20
|
+
@space.add_body(obj.shape.body) unless obj.static?
|
21
|
+
@objects << obj
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_objects(*objects)
|
25
|
+
objects.each{ |obj| self.add_object(obj) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def update(&block)
|
29
|
+
@substeps.times do
|
30
|
+
@objects.each{ |o| o.body.reset_forces }
|
31
|
+
yield(self) if block_given?
|
32
|
+
@space.step(@dt)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def draw(canvas)
|
37
|
+
@objects.each{ |obj| obj.draw(canvas) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
data/lib/cp/shape.rb
ADDED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chippunk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 0.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matthias Hennig
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-16 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: gosu
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 7
|
33
|
+
- 0
|
34
|
+
version: 0.7.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: chipmunk
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 63
|
46
|
+
segments:
|
47
|
+
- 5
|
48
|
+
- 2
|
49
|
+
- 0
|
50
|
+
version: 5.2.0
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
description: No long description available but is probably coming soon
|
54
|
+
email: mhennig@hgb-leipzig.de
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
extra_rdoc_files:
|
60
|
+
- README
|
61
|
+
files:
|
62
|
+
- README
|
63
|
+
- Rakefile
|
64
|
+
- lib/chippunk.rb
|
65
|
+
- lib/chippunk/builder.rb
|
66
|
+
- lib/chippunk/helper.rb
|
67
|
+
- lib/chippunk/object.rb
|
68
|
+
- lib/chippunk/world.rb
|
69
|
+
- lib/cp/shape.rb
|
70
|
+
- lib/cp/shape/circle.rb
|
71
|
+
- lib/cp/shape/poly.rb
|
72
|
+
- lib/cp/shape/segment.rb
|
73
|
+
- examples/world.rb
|
74
|
+
has_rdoc: true
|
75
|
+
homepage: http://github.com/mhennig/chippunk
|
76
|
+
licenses: []
|
77
|
+
|
78
|
+
post_install_message: "\n ========================================================================\n Thanks for installing Chippunk!\n ------------------------------------------------------------------------\n "
|
79
|
+
rdoc_options:
|
80
|
+
- --charset=UTF-8
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 3
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.3.7
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Wrapper For Chipmunk Physics
|
108
|
+
test_files:
|
109
|
+
- examples/world.rb
|