ruby-bullet 0.0.1-x86-linux → 0.0.2-x86-linux

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -20,8 +20,10 @@ This project has a pre-compiled gem.
20
20
 
21
21
  Usages
22
22
  -----
23
- Run a sample application.
23
+ Run a sample application.
24
+ The sample application uses gtk2 to draw boxes.
24
25
 
26
+ $ sudo gem install gtk2
25
27
  $ ruby <gems_path>/ruby-bullet-<version>-<arch>/sample/hello_bullet/HelloBullet.rb
26
28
 
27
29
 
data/lib/Version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Ruby
2
2
  module Bullet
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -1,7 +1,12 @@
1
- require 'tk'
1
+ # -*- coding: utf-8 -*-
2
+ require 'gtk2'
3
+ require 'cairo'
2
4
  require "Bullet.so"
3
5
 
4
6
  class Physics
7
+ attr_accessor :context
8
+ attr_accessor :objects
9
+
5
10
  def initialize()
6
11
  @collisionConfig = Bullet::BtDefaultCollisionConfiguration.new();
7
12
  @collisionDispatcher = Bullet::BtCollisionDispatcher.new(@collisionConfig)
@@ -18,23 +23,17 @@ class Physics
18
23
  gravity = Bullet::BtVector3.new(0.0, 9.8, 0.0)
19
24
  @dynamicsWorld.setGravity(gravity)
20
25
 
26
+ @objects = []
27
+
21
28
  @lastTime = -1
22
29
  end
23
30
 
24
- def createBox(motionState, mass, width, height)
25
- colObj = Bullet::BtBoxShape.new(Bullet::BtVector3.new(width / 2, height / 2, width / 2))
26
- inertia = Bullet::BtVector3.new()
27
- colObj.calculateLocalInertia(mass, inertia)
28
-
29
- rigidBody = Bullet::BtRigidBody.new(mass, motionState, colObj, inertia)
30
- rigidBody.instance_variable_set(:@collisionShape, colObj)
31
- rigidBody.setAngularFactor(Bullet::BtVector3.new(0, 0, 1)) # rotate around z-axis only.
32
-
33
- @dynamicsWorld.addRigidBody(rigidBody)
34
- return rigidBody
31
+ def addObject(obj)
32
+ @objects.push(obj)
33
+ @dynamicsWorld.addRigidBody(obj.rigidBody)
35
34
  end
36
35
 
37
- def update()
36
+ def update(context)
38
37
  @lastTime = Time.now.to_f if @lastTime < 0
39
38
 
40
39
  # calc delta
@@ -45,44 +44,40 @@ class Physics
45
44
  # step
46
45
  @dynamicsWorld.stepSimulation(delta)
47
46
 
48
- return true
47
+ @objects.each {|obj|
48
+ obj.draw(context)
49
+ }
50
+
51
+ return delta
49
52
  end
50
53
  end
51
54
 
52
55
 
53
56
  class Box < Bullet::BtMotionState
54
- def initialize(c, physics, mass, width, height, color)
57
+ attr_accessor :rigidBody
58
+
59
+ def initialize(physics, mass, width, height, color)
55
60
  super()
56
- @c = c
57
61
  @physics = physics
58
62
  @width = width
59
63
  @height = height
64
+ @color = color
60
65
 
61
66
  # position/rotation
62
67
  @transform = Bullet::BtTransform.new()
63
68
  @transform.setIdentity()
64
69
 
65
70
  # create a physics object.
66
- @rigidBody = @physics.createBox(self, mass, width, height)
67
-
68
- # create a drawing object.
69
- shape = [-width / 2, -height / 2,
70
- width / 2, -height / 2,
71
- width / 2, height / 2,
72
- -width / 2, height / 2]
73
- @obj = TkcPolygon.new(c, shape) {
74
- fill color
75
- outline 'black'
76
- disabledoutline ''
77
- width 1
78
- state 'normal'
79
- }
71
+ @colObj = Bullet::BtBoxShape.new(Bullet::BtVector3.new(width / 2, height / 2, width / 2))
72
+ inertia = Bullet::BtVector3.new()
73
+ @colObj.calculateLocalInertia(mass, inertia)
80
74
 
75
+ @rigidBody = Bullet::BtRigidBody.new(mass, self, @colObj, inertia)
76
+ @rigidBody.setAngularFactor(Bullet::BtVector3.new(0, 0, 1)) # rotate around z-axis only becase of 2D.
81
77
  end
82
78
 
83
79
  def setWorldTransform(worldTrans)
84
80
  @transform = Bullet::BtTransform.new(worldTrans)
85
- draw()
86
81
  end
87
82
 
88
83
  def getWorldTransform(worldTrans)
@@ -91,11 +86,9 @@ class Box < Bullet::BtMotionState
91
86
  def setPosition(x, y)
92
87
  @transform.setOrigin(Bullet::BtVector3.new(x, y, 0.0))
93
88
  @rigidBody.setCenterOfMassTransform(@transform)
94
-
95
- draw()
96
89
  end
97
90
 
98
- def draw()
91
+ def draw(context)
99
92
  newPos = @transform.getOrigin()
100
93
  newRot = @transform.getRotation()
101
94
 
@@ -106,39 +99,72 @@ class Box < Bullet::BtMotionState
106
99
  lb = Bullet::quatRotate(newRot, lb)
107
100
  rb = -lt
108
101
  rt = -lb
109
-
110
- shape = [newPos.x + lt.x, newPos.y + lt.y,
111
- newPos.x + rt.x, newPos.y + rt.y,
112
- newPos.x + rb.x, newPos.y + rb.y,
113
- newPos.x + lb.x, newPos.y + lb.y]
114
- @obj.coords(shape)
102
+
103
+ context.move_to(newPos.x + lt.x, newPos.y + lt.y)
104
+ context.line_to(newPos.x + rt.x, newPos.y + rt.y)
105
+ context.line_to(newPos.x + rb.x, newPos.y + rb.y)
106
+ context.line_to(newPos.x + lb.x, newPos.y + lb.y)
107
+ context.line_to(newPos.x + lt.x, newPos.y + lt.y)
108
+ context.set_source_color(@color)
109
+ context.fill(true)
110
+ context.set_source_color("black")
111
+ context.stroke
115
112
  end
116
113
  end
117
114
 
118
115
 
116
+ ## create a physics world.
119
117
  physics = Physics.new()
120
118
 
121
- c = TkCanvas.new
122
- c.width(300)
123
- c.height(300)
124
-
125
119
  # falling boxes.
126
- box = []
127
120
  id = 0
128
121
  11.times {|x|
129
122
  5.times {|y|
130
- box[id] = Box.new(c, physics, 1.0, 10, 10, 'SkyBlue')
131
- box[id].setPosition(100 + 10 * x, 50 - 10 * y)
132
- id += 1
123
+ box = Box.new(physics, 1.0, 10, 10, 'sky blue')
124
+ box.setPosition(100 + 10 * x, 50 - 10 * y)
125
+ physics.addObject(box)
133
126
  }
134
127
  }
135
128
 
136
129
  # floor
137
- floor = Box.new(c, physics, 0, 120, 6, 'gray')
130
+ floor = Box.new(physics, 0, 100, 5, 'gray')
138
131
  floor.setPosition(150, 200)
132
+ physics.addObject(floor)
133
+
134
+
135
+ ## create a drawing window.
136
+ window = Gtk::Window.new
137
+ window.set_default_size(300, 300)
138
+ window.signal_connect("destroy") do
139
+ Gtk.main_quit
140
+ false
141
+ end
142
+
143
+ drawing_area = Gtk::DrawingArea.new
144
+
145
+ delta = 0
146
+ drawing_area.signal_connect("expose-event") do |widget, event|
147
+ context = widget.window.create_cairo_context
148
+ delta += physics.update(context)
149
+
150
+ # create a new box per 3sec.
151
+ if (delta > 3.0)
152
+ box = Box.new(physics, 1.0, 10, 10, 'orange')
153
+ box.setPosition(125 + (physics.objects.length % 10) * 5, 0)
154
+ physics.addObject(box)
155
+ delta = 0
156
+ end
157
+ true
158
+ end
159
+
160
+ Gtk.timeout_add(1000.0 / 60.0) {
161
+ drawing_area.queue_draw
162
+ true
163
+ }
164
+
165
+ window.add(drawing_area)
166
+ window.show_all
139
167
 
140
- c.pack
168
+ Gtk.main
141
169
 
142
- TkAfter.new(1000 / 60, -1, proc{ physics.update()}).start
143
170
 
144
- Tk.mainloop
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-bullet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: x86-linux
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-15 00:00:00.000000000 Z
12
+ date: 2012-09-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: This is a binary package of the ruby wrapper of Bullet.
15
15
  email: