Space borders in Chipmunk with Ruby

152 views Asked by At

I am trying to make the edges of my window collidable, rather than having my shape drop through the floor on creation.

When I was looking through the demos on the site, I saw this line of code that was ment to define the bounds of the space:

[_space addBounds:CGRectMake(130, 139, 767, 1500) thickness:20 elasticity:1.0 friction:1.0 layers:PhysicsEdgeLayers group:CP_NO_GROUP collisionType:nil];

I think that this assigns collidable borders around the space, thus enclosing it.

Due to some minor knowledge in Objective-C, I know that addBounds is a method for space that allows to create the space border.

However, when looking through the ruby bindings for chipmunk, I was not able to find the AddBounds method.

Furthermore, in a related problem, I could not find the cpShapeSetFriction ruby equivilant.

Were can I find these methods, and If I can't, are there any alternatives?

2

There are 2 answers

0
Evan Hruskar On BEST ANSWER

For those who are wondering, There are no ruby bindings for the addBounds method.

I got around this by creating four static segment shapes that made up the borders of my window.

Here are the segments you need to create the bounds.

    CP::Shape::Segment.new(YOUR_STATIC_BODY, CP::Vec2.new(0, 0), CP::Vec2.new(WIDTH, 0), 1.0)
    CP::Shape::Segment.new(YOUR_STATIC_BODY, CP::Vec2.new(0, 0), CP::Vec2.new(0, HEIGHT), 1.0)
    CP::Shape::Segment.new(YOUR_STATIC_BODY, CP::Vec2.new(WIDTH, 0), CP::Vec2.new(WIDTH, HEIGHT), 1.0)
    CP::Shape::Segment.new(YOUR_STATIC_BODY, CP::Vec2.new(0, HEIGHT), CP::Vec2.new(WIDTH, HEIGHT), 1.0)

Don't forget to add them to the space with @space.add_static_shape(THE_SEGMENT)

Shape friction in ruby can be set with: shape.u = 0.1 #Shape friciton of 0.1

0
Sylvain On

In Ruby with Gosu, I modified the chipmunk_integration.rb example to add wall. Effectively using CP::Shape::Segment, You can find the full explanation here and sources :

Ruby Chipmunk Integration beginner hacking gosu-example https://www.libgosu.org/cgi-bin/mwf/topic_show.pl?tid=1324

class Wall
  attr_reader :a, :b

  def initialize(window, shape, pos)
    # window needs to have a attr_accessor for :space
    @window = window

    @color = Gosu::Color::WHITE

    @a = CP::Vec2.new(shape[0][0], shape[0][1])
    @b = CP::Vec2.new(shape[1][0], shape[1][1])

    @body = CP::Body.new(CP::INFINITY, CP::INFINITY)
    @body.p = CP::Vec2.new(pos[0], pos[1])
    @body.v = CP::Vec2.new(0,0)

    @shape = CP::Shape::Segment.new(@body, @a, @b, 1)
    @shape.e = 0.5
    @shape.u = 1

    @window.space.add_static_shape(@shape)
  end

  def draw
    @window.draw_line(@body.p.x + a.x, @body.p.y + a.y, @color,
                      @body.p.x + b.x, @body.p.y + b.y, @color,
                      ZOrder::Wall)
  end
end

I integrated it here:

https://github.com/Sylvain303/gosu-examples/blob/test_chipmunk/examples/chipmunk_integration.rb#L218

add walls that way:

    @borders = []
    # add space wall !
    # first couple is point (x,y) of the segement, last couple is it top point
    # position.
    # left
    @borders << Wall.new(self, [[1, 1], [1,HEIGHT-1]], [1, 1])
    # top
    @borders << Wall.new(self, [[1, 1], [WIDTH-1, 1]], [1,1])
    # right
    @borders << Wall.new(self, [[1, 1], [1,HEIGHT-1]], [WIDTH-1, 1])
    # bottom
@borders << Wall.new(self, [[1, 1], [WIDTH-1, 1]], [1,HEIGHT-1])