在libGosu / Chingu中创建额外的子弹,根据宇宙飞船的角度偏移?

时间:2013-09-02 00:27:51

标签: ruby 2d-games libgosu

我正在尝试使用libGosu和Chingu为我的基本教程式Spaceship创建武器升级。

在播放器类中,我尝试了以下几种变体:

def fire
  Bullet.create(:x => @x, :y => @y, :angle => @angle)
  Bullet.create(:x => @x + Gosu::offset_x(90, 25), :y => @y + Gosu::offset_y(@angle, 0), :angle => @angle)
end

它有点有用,但不完全是理想的应该如何。作为参考,这是Bullet类的样子:

class Bullet < Chingu::GameObject
  def initialize(options)
    super(options.merge(:image => Gosu::Image["assets/laser.png"]))
    @speed = 7
    self.velocity_x = Gosu::offset_x(@angle, @speed)
    self.velocity_y = Gosu::offset_y(@angle, @speed)
  end

  def update
    @y += self.velocity_y
    @x += self.velocity_x
  end
end

当宇宙飞船旋转时,我应该如何构建“def fire”以使额外的子弹正确对齐?

2 个答案:

答案 0 :(得分:0)

x需要以y偏移的相同方式进行偏移。

另外,如果@weapon == 2

,为什么要以不同方式创建两次子弹?

答案 1 :(得分:0)

以下简单的解决方案可以解决问题:

Bullet.create(:x => @x + Gosu::offset_x(@angle+90, 25), :y => @y + Gosu::offset_y(@angle+90, 25), :angle => @angle)

GameDev.StackExchange对this answer中的力量进行了深入描述。

我还使用Sin和Cos发现了以下“沿着谷仓的长路”,并使用PI将角度转换为弧度:

Bullet.create(:x => @x + 20 * Math.cos(@angle*Math::PI/180) , :y => @y + 20 * Math.sin(@angle*Math::PI/180), :angle => @angle)

可以找到关于Sin / Cos方法的更详细描述here

将度数转换为弧度的公式为degrees*Π/180