Newbie: Rails bindings

Official forum for the Chipmunk2D Physics Library.
Post Reply
Nexum
Posts: 1
Joined: Sat Dec 08, 2007 9:04 pm
Contact:

Newbie: Rails bindings

Post by Nexum »

Hi all,

I'm new to Chipmunk, and a eager to use it via the Ruby bindings. I haven't used any Ruby bindings to external libraries before so I am after a little bit of help in getting started. I have successfully followed the instructions in the downloaded source and run 'ruby extconf.rb' and then 'make' - but where do I go from here (in terms of Ruby code) in order to get a basic Chipmunk setup running now?

Apologies if this is a dumb question or answered before - I did search for some time on the forum, and only posted reluctantly.

Many thanks in advance,

- Nex.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Newbie: Rails bindings

Post by slembcke »

Well, depending on what platform you are on, compiling it will give you a chipmunk.[so/bundle/dll]. You can require this just like any other ruby source file.

Code: Select all

require 'chipmunk'
There are Ruby Docs online, but they assume you've absorbed at least some of what was said in the C Docs.

Theoretically Chipmunk works without graphics, but it's sort of like Schrodinger's cat, you don't know until you look. Unfortunately this is where Ruby is a bit of a hinderance as there isn't a lowest common denominator that you can expect all platforms to have. This is why I've never written any sample Ruby code. You could probably find help from the Shattered Ruby guys in order to get something up and running.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
citrys
Posts: 16
Joined: Thu Nov 15, 2007 12:26 am
Contact:

Re: Newbie: Rails bindings

Post by citrys »

I found the Chipmunk example from the Gosu folks extremely helpful in getting a feel for a Chipmunk-based engine in Ruby. I use Rubygame myself, but the physics simulation concepts are about the same.
slembcke wrote:Unfortunately this is where Ruby is a bit of a hinderance as there isn't a lowest common denominator that you can expect all platforms to have. This is why I've never written any sample Ruby code.
Regarding a lowest common denominator, what graphics/control library do you use for your C/C++ samples?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Newbie: Rails bindings

Post by slembcke »

I've been writing my own environment for Ruby game development called Aerosol. It has some nice things like a scenegraph that integrates with Chipmunk, but it's definitely not in a state where I would recommend others to use it at the moment.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
vanjab
Posts: 1
Joined: Wed Dec 12, 2007 12:42 am
Contact:

Re: Newbie: Rails bindings

Post by vanjab »

C/C++ can use gl/glut since that is a fairly standard library on recent systems.
Ruby does not come standard with a graphics library at all, but I think a sample like the one in box2d should be included:
create a world
put a static ground in it
put a dynamic falling body in it
print coordinates every frame for a second or two
end.
citrys
Posts: 16
Joined: Thu Nov 15, 2007 12:26 am
Contact:

Re: Newbie: Rails bindings

Post by citrys »

I wouldn't call myself a Ruby expert, but here's an animated text mode demo that ought to work just about anywhere. Might have to change FRAME_DELAY according to your system speed; 0.01 makes it pretty smooth on a low-powered Windows PC.

It should show 2 balls "falling" to the left and bouncing a bunch. Add more balls by increasing BALL_COUNT. Uncomment the line in the engine loop to show movement history "smoke trails" vertically.

Strangely, on a much faster PC, the animation wasn't as nice. YMMV.

Code: Select all

# chipmunk_demo.rb
# Created 12/12/2007
# This code is PUBLIC DOMAIN - use any way you see fit

require 'chipmunk'

module ChipmunkDemo
    # Convenient constants
    INFINITY = 1.0 / 0.0
    ORIGIN = CP::Vec2.new(0, 0)
    SCREEN_WIDTH = 80
    
    # Space properties
    SUBSTEPS = 30
    STEP_DELTA = 1.0 / 60.0
    GRAVITY = CP::Vec2.new(-50, 0)
    FRAME_DELAY = 0.01

    # Ball properties
    MASS = 5
    ELASTICITY = 0.875
    STARTING_POS = CP::Vec2.new(SCREEN_WIDTH, 0)
    RADIUS = SCREEN_WIDTH / STARTING_POS.x / 2.0
    SPRITE = "o"
    BALL_COUNT = 2

    frame_count = 0

    moi = CP.moment_for_circle(MASS, 0, RADIUS, ORIGIN)

    balls = []

    # Create the physics representations for the balls
    BALL_COUNT.times do |num|
        ball_body = CP::Body.new(MASS, moi)
        ball_shape = CP::Shape::Circle.new(ball_body, RADIUS, ORIGIN)
        ball_body.p = STARTING_POS - CP::Vec2.new(14, 0) * num
        ball_shape.e = ELASTICITY
        balls << ball_shape
    end

    # Create an immovable ground plane
    ground_body = CP::Body.new(INFINITY, INFINITY)
    ground_shape = CP::Shape::Segment.new(ground_body, CP::Vec2.new(-1, 5), CP::Vec2.new(-1, -5), 1.0)
    ground_body.p = ORIGIN
    ground_shape.e = 1.0

    space = CP::Space.new

    balls.each do |shape|
        space.add_body(shape.body)
        space.add_shape(shape)
    end

    space.add_static_shape(ground_shape)

    # Exit cleanly on CTRL+C
    trap(:INT) { exit }

    # Start the engine!
    while true
        frame_count += 1

        # Physics loop
        SUBSTEPS.times do
            balls.each do |shape|
                shape.body.reset_forces
                shape.body.apply_force(GRAVITY, ORIGIN)
            end
            
            space.step(STEP_DELTA / SUBSTEPS)
        end
        
        # Clear the buffer
        buffer = " " * SCREEN_WIDTH

        # Draw balls on the buffer
        balls.each do |shape|
            loc = ((shape.body.p.x - RADIUS) * (SCREEN_WIDTH.to_f / STARTING_POS.x.to_f)).round
            buffer[loc] = SPRITE if loc >=0 and loc < SCREEN_WIDTH
        end
        
        # Uncomment for "smoke trails"
        #print "\r" + buffer.gsub(/#{SPRITE}/, ".") + "\n" if frame_count % 20 == 0

        # Swap the buffer to the display
        print "\r" + buffer
        $stdout.flush
        
        # Keep the framerate in check
        sleep FRAME_DELAY
    end
end
Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests