与红宝石的花栗鼠空间边界

时间:2014-12-23 21:34:06

标签: ruby chipmunk libgosu

我试图让我的窗户边缘可以碰撞,而不是让我的形状在创作时掉落在地板上。

当我浏览网站上的演示时,我看到了这行代码来定义空间的界限:

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

我认为这会在空间周围分配可碰撞的边界,从而包围它。

由于Objective-C的一些小知识,我知道addBounds是一种允许创建空间边界的空间方法。

但是,在查看ruby bindings for chipmunk时,我无法找到AddBounds方法。

此外,在相关问题中,我找不到cpShapeSetFriction ruby​​等效物。

我可以找到这些方法吗?如果我不能,有其他选择吗?

2 个答案:

答案 0 :(得分:1)

对于那些想知道的人,addBounds方法没有ruby绑定。

我通过创建构成窗口边界的四个静态线段形状来解决这个问题。

以下是创建边界所需的细分。

    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)

不要忘记使用@space.add_static_shape(THE_SEGMENT)

将它们添加到空间

红宝石中的形状摩擦可以使用:shape.u = 0.1 #Shape friciton of 0.1

设置

答案 1 :(得分:0)

在Ruby with Gosu中,我修改了chipmunk_integration.rb示例以添加墙。有效地使用CP :: Shape :: Segment,你可以在这里找到完整的解释和来源:

Ruby Chipmunk Integration初学者黑客攻击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

我在这里整合了它:

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

以这种方式添加墙:

    @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])