Box2D Forums

It is currently Sun May 19, 2013 12:49 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 14 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Ruby Box2d bindings
PostPosted: Mon Sep 24, 2007 3:10 pm 
Offline

Joined: Mon Sep 24, 2007 3:04 pm
Posts: 7
hello

i've wrapped the box2d-lib with swig for ruby. i also use gosu (http://code.google.com/p/gosu/) to draw things. is someone interested ? i've made only the win32-build.

http://rubyforge.org/projects/ruby-box2d/

remo


Top
 Profile  
 
 Post subject: Re: Ruby Box2d bindings
PostPosted: Fri Dec 07, 2007 1:58 am 
Offline

Joined: Thu Dec 06, 2007 6:21 pm
Posts: 8
Ahh I can't figure this out, I'm trying to compile this for OSX and Ruby, but its not working.

i renamed your interface file to RubyBox2d.i
then made extconf.rb

Code:
require 'mkmf'
create_makefile('RubyBox2d')


and ran
ARCHFLAGS="-arch i386" ruby extconf.rb

followed by make

which compiles correctly, but gives lots of link errors such as:

Undefined symbols:
"b2PolyContact::Create(b2Shape*, b2Shape*, b2BlockAllocator*)", referenced from:
_wrap_b2PolyContact_Create(int, unsigned long*, unsigned long)in RubyBox2d_wrap.o


the command make runs is:
cc -arch i386 -pipe -bundle -o RubyBox2d.bundle RubyBox2d_wrap.o -L"." -L"/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib" -L. -arch i386 -lruby -lpthread -ldl -lm

What am I doing wrong?

edit: I've made progress.

By linking with

g++ -arch i386 -pipe -bundle -o RubyBox2d.bundle RubyBox2d_wrap.o -L"." -L"/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib" -L. -arch i386 -lruby -lpthread -ldl -lm -lBox2D

(and manually making a static library for Box2D) I've managed to get it down to one missing symbol : b2BroadPhase::ValidatePairs()

another edit: There is no definition for the function for some reason, I commented it out for now. Everything is built :) Now to figure out what I need to do with it...

possibly final edit: After fixing the provided sample.rb (the function names were the wrong case...) I got the library working on OSX :) Only problem now is that when the sample program runs, I get:

Code:
...
pos: 0.0,1.25000035762787 rot: 0.0
pos: 0.0,1.12500035762787 rot: 0.0
pos: 0.0,0.997222602367401 rot: 0.0
pos: -4.35693873441778e-05,0.997230529785156 rot: 4.35693873441778e-05
pos: -3.40081496688072e-05,0.997228801250458 rot: 3.40077458531596e-05
...


Not quite what I expected, guess thats close enough to 0


Top
 Profile  
 
 Post subject: Re: Ruby Box2d bindings
PostPosted: Sat Dec 08, 2007 9:42 am 
Offline

Joined: Thu Dec 06, 2007 6:21 pm
Posts: 8
:cry: userdata is of type void *, which is kind of tricky to use in swig/ruby :(


Top
 Profile  
 
 Post subject: Re: Ruby Box2d bindings
PostPosted: Sat Dec 15, 2007 2:33 pm 
Offline

Joined: Mon Sep 24, 2007 3:04 pm
Posts: 7
hi vanjalolz

could you please help me on this "userdata is of type void *" issue ? this ruby-box2d-binding project was only a short hack ;)

thanks
remo


Top
 Profile  
 
 Post subject: Re: Ruby Box2d bindings
PostPosted: Tue Dec 18, 2007 6:01 pm 
Offline

Joined: Fri Dec 07, 2007 3:09 pm
Posts: 241
If nothing else works, you can remove it altogether. I haven't needed it yet, but I'm sure it'll come in handy later.


Top
 Profile  
 
 Post subject: Re: Ruby Box2d bindings
PostPosted: Fri Dec 21, 2007 3:57 pm 
Offline

Joined: Mon Sep 24, 2007 3:04 pm
Posts: 7
i've updated to box2d 1.4.3. i've fixed a B2Vec2 adding bug. the pyramid-test runs now with ruby:

require 'box2d'

include Box2d

# create the world

worldAABB = B2AABB.new
worldAABB.minVertex.set(-100.0, -100.0)
worldAABB.maxVertex.set(100.0, 200.0)
gravity = B2Vec2.new
gravity.set(0.0, -10.0)
do_sleep = true
m_world = B2World.new(worldAABB, gravity, do_sleep)

sd = B2BoxDef.new
sd.type = E_boxShape
sd.extents.set(50.0, 10.0)

bd = B2BodyDef.new
bd.position.set(0.0, -10.0)
bd.add_shape(sd)
m_world.create_body(bd)

sd = B2BoxDef.new
a = 0.5
sd.type = E_boxShape
sd.extents.set(a, a)
sd.density = 5.0

bd = B2BodyDef.new
bd.add_shape(sd);

x = B2Vec2::make(-10.0, 0.75)
y = B2Vec2::make(0.0, 0.0)
delta_x = B2Vec2::make(0.5625, 2.0)
delta_y = B2Vec2::make(1.125, 0.0)

for i in (0..24) do
y = x;

for j in (i..24) do
bd.position = y
m_world.create_body(bd)

y += delta_y
end

x += delta_x;
end


Top
 Profile  
 
 Post subject: Re: Ruby Box2d bindings
PostPosted: Sat Dec 22, 2007 3:17 pm 
Offline

Joined: Mon Sep 24, 2007 3:04 pm
Posts: 7
now, i fixed the userdata (void*) issue. pls report me when it's ok, thanks. you can download it at:

http://rubyforge.org/projects/ruby-box2d/


Top
 Profile  
 
 Post subject: Re: Ruby Box2d bindings
PostPosted: Sat Dec 22, 2007 3:24 pm 
Offline

Joined: Mon Sep 24, 2007 3:04 pm
Posts: 7
and here a working sample (with gosu): (the media files are missed)

Code:
require 'rubygems'

require 'gosu'
require 'box2d'

include Box2d

class GameWindow < Gosu::Window
   RadToDeg = 57.295779513082323

   def initialize
    super(640,480, false, 20)
    self.caption = "Box2d Demo"

      # Pointer ist 28x38, Hotspot 6,1
      @pointer = Gosu::Image.new(self, "media/img/pointer.png", true)

      @stone = Gosu::Image.new(self, "media/img/stone.png", false)

      @info = Gosu::Font.new(self, "arial", 12)
      b = B2AABB.new
      b.minVertex.set(0.0, 0.0)
      b.maxVertex.set(640.0, 480.0)
      gravity = B2Vec2.new(0.0, 180.0)
      do_sleep = true
      @w = B2World.new(b,gravity,do_sleep)

      
      groundBoxDef = B2BoxDef.new
      groundBoxDef.extents.set(320.0, 5.0)
      groundBoxDef.density = 0.0

      groundBodyDef = B2BodyDef.new
      groundBodyDef.position.set(320.0, 460.0)
      groundBodyDef.add_shape(groundBoxDef)

      @walls = []
      
      bd = create_wall(320.0,5.0,320.0,460.0)
      @walls <<  {:body => @w.create_body(bd), :shape => [320.0,5.0]};
      bd = create_wall(5.0,130.0,5.0,325.0)
      @walls <<  {:body => @w.create_body(bd), :shape => [5.0,130.0]};
      bd = create_wall(5.0,130.0,635,325.0)
      @walls <<  {:body => @w.create_body(bd), :shape => [5.0,130.0]};

      bd = create_box(20.0,20.0,320.0,40.0,0.6)
      
      @entities = []
      @entities <<  {:body => @w.create_body(bd), :shape => [20.0,20.0]};

      @last_time = Gosu::milliseconds()
  end

   def create_box(w,h,x,y,rot)
      boxDef = B2BoxDef.new
      boxDef.extents.set(w,h)
      boxDef.density = 0.1
      boxDef.friction = 1.3
      boxDef.restitution = 0.5
      
      bodyDef = B2BodyDef.new
      bodyDef.position.set(x,y)
      bodyDef.rotation = rot
      bodyDef.allowSleep = true;
      bodyDef.add_shape(boxDef);
      bodyDef.userData = ["box",10.0,10.0]
      return bodyDef;
   end

   def create_wall(w,h,x,y)
      boxDef = B2BoxDef.new
      boxDef.extents.set(w,h)
      boxDef.density = 0.0
      bodyDef = B2BodyDef.new
      bodyDef.position.set(x,y)
      bodyDef.allowSleep = true;
      bodyDef.add_shape(boxDef);
      bodyDef.userData = ["wall",10.0,10.0]
      return bodyDef;
   end

def button_down(id)
    if id == Gosu::Button::MsRight then
         bd = create_box(20.0,20.0,self.mouse_x, self.mouse_y,0.0)
         @entities << {:body => @w.create_body(bd), :shape => [20.0,20.0]};
      end
      
   end

  def update
  end

  def draw
      #c = Gosu::Color.new(255,0,0,255)
      c = Gosu::Color.new(255,255,255,255)
      c1 = Gosu::Color.new(167,167,167,167)

      #time_step = (Gosu::milliseconds() - @last_time) / 1000.0
      time_step = 1.0 / 60
      @last_time = Gosu::milliseconds()

      iterations = 10
      @w.step(time_step, iterations)
      @entities.each{|e|
         pos = e[:body].get_origin_position()
         rot = e[:body].get_rotation()
         self.draw_stone(pos.x - e[:shape][0],pos.y - e[:shape][1], e[:shape][0] * 2, e[:shape][1] * 2,rot,c)
      }
      
      @walls.each{|w|
         pos = w[:body].get_origin_position()
         rot = w[:body].get_rotation()
         self.draw_rectangle(pos.x - w[:shape][0],pos.y - w[:shape][1], w[:shape][0] * 2, w[:shape][1] * 2,rot,c1)
      }
      
      @info.draw("x: #{mouse_x} y: #{mouse_y} ms: #{time_step}",10,10,0)
      @pointer.draw(self.mouse_x-6, self.mouse_y-1,0)

   end

   def draw_stone(x, y, w, h, rot,c)
      if rot == 0.0
         lt = [ x, y ]
         lb = [ lt[0], lt[1] + h ]
         rb = [ lt[0] + w, lt[1] + h ]
         rt = [ lt[0] + w, lt[1] ]
         @stone.draw_as_quad(lt[0], lt[1], c, lb[0], lb[1], c, rt[0], rt[1], c, rb[0], rb[1], c,-1.0)
      else
         pos = [x+(w/2.0),y+(h/2.0)]
         lt = move(rotate([-w/2.0,-h/2.0], rot), pos)
         lb = move(rotate([-w/2.0,h/2.0], rot), pos)
         rb = move(rotate([w/2.0,h/2.0], rot), pos)
         rt = move(rotate([w/2.0,-h/2.0], rot), pos)
         @stone.draw_as_quad(lt[0], lt[1], c, lb[0], lb[1], c, rt[0], rt[1], c, rb[0], rb[1], c,-1.0)
      end
   end


   def draw_rectangle(x, y, w, h, rot, c)
      if rot == 0.0
         lt = [ x, y ]
         lb = [ lt[0], lt[1] + h ]
         rb = [ lt[0] + w, lt[1] + h ]
         rt = [ lt[0] + w, lt[1] ]
         self.draw_quad(lt[0], lt[1], c, lb[0], lb[1], c, rt[0], rt[1], c, rb[0], rb[1], c)
      else
         pos = [x+(w/2.0),y+(h/2.0)]
         lt = move(rotate([-w/2.0,-h/2.0], rot), pos)
         lb = move(rotate([-w/2.0,h/2.0], rot), pos)
         rb = move(rotate([w/2.0,h/2.0], rot), pos)
         rt = move(rotate([w/2.0,-h/2.0], rot), pos)
         self.draw_quad(lt[0], lt[1], c, lb[0], lb[1], c, rt[0], rt[1], c, rb[0], rb[1], c)
      end
   end

   def move(p,pos)
      return [p[0] + pos[0], p[1] + pos[1]]
   end
   
   def rotate(p, theta)
      # x' = cos(theta)*x - sin(theta)*y
      # y' = sin(theta)*x + cos(theta)*y
      ct = Math.cos(theta)
      cs = Math.sin(theta)
      return [ct *p[0] - cs *p[1],cs*p[0] + ct*p[1]]
   end
end


window = GameWindow.new
window.show


Top
 Profile  
 
 Post subject: Re: Ruby Box2d bindings
PostPosted: Sun Dec 23, 2007 5:34 am 
Offline

Joined: Thu Dec 06, 2007 6:21 pm
Posts: 8
Sorry I've been away for a while, I think you've figured out the userdata problem.

Basically, all shapes and bodies are allowed to have a 32 bit field of 'userdata', but it is of a pointer type. I was looking to do something like:

@box1.userdata = :groundtype

for use in collision detection.
Also, what is the best way to compile and install this project? I ask because I need to build myself a mac version.

Thanks


Top
 Profile  
 
 Post subject: Re: Ruby Box2d bindings
PostPosted: Mon Dec 24, 2007 4:24 pm 
Offline

Joined: Mon Sep 24, 2007 3:04 pm
Posts: 7
hi vanjalolz

i've updated now a gem that works with MacOSX Tiger/PPC. you can tryit out :)

thx


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group