2007-06-22
■[ode][ruby]rubyで3D物理シミュレーション
rubyからODEを使えるようになったのでとりあえずうp。
http://ssktkr.com/ode-ruby5.tgz
こんなソースで3Dシミュレーションできます!
require "ode" require "draw_stuff" require "pp" require "dl" $world = Ode::World.new $world.gravity = [0.0, 0.0, -0.2] # 重力設定 $space = Ode_lib.dHashSpaceCreate(nil); # 衝突用空間の創造 $contactgroup = Ode_lib.dJointGroupCreate(0); # ジョイントグループの生成 $ground = Ode_lib.dCreatePlane($space, 0,0, 1.0, 0.0); # 平面ジオメトリの生成 def rand_within(from, to) rand*(to-from)+from end $objects = [] 100.times{ $objects << Ode::Material::Ball.new(:m=>rand_within(1,9), :r=>rand_within(1,5), :position=>[rand_within(-7,7), rand_within(-7,7), rand_within(5,500)], :color=>[rand(2).to_f, rand(2).to_f, rand(2).to_f] ) } $objects << Ode::Material::Box.new(:m=>1000.0, :x=>10.0, :y=>50.0, :z=>50.0, :position=> [-30.0, 0.0, 30.0], :color => [0.3, 0.0, 0.0] ) ds = DrawStuff.new ds.world = $world ds.width = 800 ds.height = 600 ds.path_to_textures = "../drawstuff/textures" def ds.start puts "start" xyz = [ 50.0, 0.0, 1.0] # 視点の位置 hpr = [-180.0, 20.0, 0.0] # 視線の方向 DrawStuff::DSLib.dsSetViewpoint(xyz.to_ptr("F"), hpr.to_ptr("F")) # カメラの設定 end def ds.step(pause) @step_count += 1 if @step_count%5000==0 puts "==============================================" puts "step #{pause} #{@step_count}" xyz = [0.0, 0.0, 0.0].to_ptr("F") # 視点の位置 hpr = [0.0, 0.0, 0.0].to_ptr("F") # 視線の方向 DrawStuff::DSLib.dsGetViewpoint(xyz, hpr) puts "xyz = " + xyz.to_a("F").inspect puts "hpr = " + hpr.to_a("F").inspect end Ode_lib.dSpaceCollide($space, Proc.new{|o1,o2| # puts "cb #{o1._ptr} #{o2._ptr} #{$ground._ptr} #{$ground._ptr==o1._ptr} #{$ground._ptr==o2._ptr}" # 衝突検出 #if $ground._ptr==o1._ptr || $ground._ptr==o2._ptr contact_geoms = Ode_lib.dCollide(o1, o2, 10000) # 衝突情報の生成 contact_geoms.each do |cg| # pp cg contact = Ode_lib::DContact.new contact.geom = cg contact.surface.mode = Ode_lib::DContactBounce; # 接触面の反発性を設定 contact.surface.bounce = 0.8; # 反発係数(0.0から1.0) contact.surface.bounce_vel = 1.0; # 反発に必要な最低速度 # 接触ジョイントの生成 joint = Ode_lib.dJointCreateContact($world._id, $contactgroup, contact) # 接触している2つの剛体を接触ジョイントにより拘束 Ode_lib.dJointAttach(joint, Ode_lib.dGeomGetBody(cg.g1), Ode_lib.dGeomGetBody(cg.g2)) end #end }); @world.step(0.05) Ode_lib.dJointGroupEmpty($contactgroup); # ジョイントグループを空にする $objects.each{|o| o.draw } end def ds.command(cmd) puts "command #{cmd}" end def ds.stop puts "stop" end ds.run
今回学んだ技術:
- SWIG
- Ruby/DL
- Rubyの中身を少々(Ruby Hacking Guideを読んだ)
- ODE この本がすごい!
簡単!実践!ロボットシミュレーション - Open Dynamics Engineによるロボットプログラミング
- 作者: 出村公成
- 出版社/メーカー: 森北出版
- 発売日: 2007/05/18
- メディア: 単行本(ソフトカバー)
- 購入: 5人 クリック: 89回
- この商品を含むブログ (10件) を見る
Wiiリモコンと組み合わせたら絶対たのしい。
トラックバック - http://d.hatena.ne.jp/urekat/20070622/1182484745
リンク元
- 90 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4GFRC_ja___JP210&q=Cannot+find+gem+for+Rails+~>1.2.3.0:
- 60 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja:official&q=rake+unit テスト&btnG=検索&lr=lang_ja
- 51 http://demura.net/9ode/425.html
- 49 http://www.google.co.jp/search?q=ruby+3d&lr=lang_ja&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:ja:official&client=firefox
- 39 http://reader.livedoor.com/reader/
- 36 http://www.google.co.jp/url?sa=t&rct=j&q=ruby 3d&source=web&cd=2&ved=0CDUQFjAB&url=http://d.hatena.ne.jp/urekat/20070622/1182484745&ei=d0CxTsjICOOJmQWk852LAg&usg=AFQjCNGxOVjGC1LCiZEY9wQ_OZeffLEsQg&sig2=93O6MBNQNxBh0KAfkn68zg&cad=rja
- 31 http://www.google.co.jp/search?hl=ja&client=firefox&rls=org.mozilla:ja-JP-mac:official&hs=UmV&q=Ruby+Cocoa&btnG=検索&lr=lang_ja
- 30 http://www.google.com/search?hl=ja&lr=lang_ja&ie=UTF-8&oe=UTF-8&q=rails+find+limit&num=50
- 24 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CCYQFjAA&url=http://d.hatena.ne.jp/urekat/20070622/1182484745&ei=mLSTT4T8Bsf6mAWN54zKBw&usg=AFQjCNGxOVjGC1LCiZEY9wQ_OZeffLEsQg&sig2=9ktXIGUmrZbS5OiJeZQVBw
- 23 http://www.google.co.jp/search?hl=ja&q=ruby+シミュレーション&lr=

