rubygame-enginegame-physicschipmunklibgosu

How to create a floor with Chipmunk library


I'm using Ruby and, of course, the Ruby bindings to Chipmunk as well as the Chingu/Gosu game libraries. Seems like the syntax is different but otherwise I assume it's the same as Chipmunk C. How do I make a floor? I tried the following:

space = CP::Space.new
space.damping = 0.9
space.gravity = CP::Vec2.new(0, 50)
body = CP::StaticBody.new
shape_array = [CP::Vec2.new(0, 400), CP::Vec2.new($window.width, 400), CP::Vec2.new($window.width, 390), CP::Vec2.new(0, 390)]
shape = CP::Shape::Poly.new(body, shape_array, CP::Vec2.new(0,0))
shape.collision_type = :floor
space.add_body(body)
space.add_shape(shape)

What happens is objects hit the floor and kind of wiggle for a split second, then pass right through. I want them to land on the floor, maybe even bounce up a little (depending on the object). But not go through the floor. How do I do this?


Solution

  • To make this work, I had to remove the line:

    space.add_body(body)
    

    Static bodies can't be added to space.