0

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

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

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”以便在宇宙飞船旋转时让额外的子弹正确对齐?

4

2 回答 2

0

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

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

GameDev.StackExchange的这个答案中对起作用的力量进行了深入的描述。

我还遇到了以下“在谷仓周围很长的路”,使用 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 方法的更详细说明,请参见此处

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

于 2013-09-06T01:13:24.637 回答
0

x需要以与偏移相同的方式偏移y

另外,如果 ,您为什么要以不同的方式两次创建子弹@weapon == 2

于 2013-09-02T02:45:29.793 回答