box2Dをためす
Ruby-Box2D
http://rubyforge.org/projects/ruby-box2d/
2007年でなんか止まってたけど、つい最近別の人?がいじりはじめたようだ。
サンプルのプログラムを参考に、Box2Dで計算した結果を使ってDXRubyで描画してみた。
すなわち、別にDXRubyがサポートしなくても、Box2Dバインドの拡張ライブラリがあればどーにでもなる、ということだ。
# http://www.box2d.org/manual.html require 'rubygems' require 'dxruby' require 'box2d' include Box2d b = B2AABB.new b.minVertex.set(0.0, 0.0) # 世界の左上 b.maxVertex.set(640.0, 480.0) # 世界の右下 gravity = B2Vec2.new(0, 100.0) # 重力 do_sleep = true w = B2World.new(b,gravity,do_sleep) # 世界作成 groundBoxDef = B2BoxDef.new # 地面の四角作成 groundBoxDef.extents.set(320.0, 20.0) # 大きさ? groundBoxDef.density = 0.0 groundBodyDef = B2BodyDef.new # 地面作成 groundBodyDef.position.set(320.0, 400.0) # 場所 groundBodyDef.add_shape(groundBoxDef) # 四角の形を設定 ground = w.create_body(groundBodyDef) # 世界に追加 #なんで地面は落ちない? body = [] for i in 0..10 boxDef = B2BoxDef.new boxDef.extents.set(30.0, 30.0) boxDef.density = 1.0 boxDef.friction = 0.3 bodyDef = B2BodyDef.new bodyDef.position.set(rand(700),rand(300)) bodyDef.add_shape(boxDef); body.push w.create_body(bodyDef); end timeStep = 1.0 / 60.0 iterations = 10 image = Image.new(60,60).box(0,0,99,99, [255,255,255]) gimage = Image.new(640,40).box(0,0,639,479, [255,255,255]) Window.loop do w.step(timeStep, iterations) body.each do |b| pos = b.get_origin_position() rot = b.get_rotation() Window.drawRot(pos.x-30, pos.y-30, image, rot / 2 / Math::PI * 360) end pos = ground.get_origin_position() rot = ground.get_rotation() Window.drawRot(pos.x-320, pos.y-20, gimage, rot / 2 / Math::PI * 360) end