在 Ruby with Gosu 中,我修改了chipmunk_integration.rb
示例以添加墙。有效地使用 CP::Shape::Segment,你可以在这里找到完整的解释和来源:
Ruby Chipmunk Integration 初学者 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
我在这里整合了它:
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])