【问题标题】:Pushing a spaceship around推动宇宙飞船
【发布时间】:2012-02-22 15:48:39
【问题描述】:

我有一艘宇宙飞船,它的底座上有两个推进器,一个在左边,一个在右边。

当右推进器打开时,它应该在加速时以抛物线向左推动宇宙飞船。左推进器则相反。

我该如何实现?

我在 box2d 上发现了一个叫做“弧度脉冲”的东西,这样可以吗?

我还希望物理上稍微反转正确的推力(有点像那些只有一个按钮的廉价遥控车之一),但前提是另一个推进器在之前的一定时间内使用过。

任何库的工作示例(或指向正确方向的东西)就足够了。

【问题讨论】:

  • 为什么它会做抛物线运动?由于重力?
  • 因为左或右火箭的力量
  • 一个单独作用的力会使船沿直线移动。这是牛顿第二定律。也许这艘船已经有了速度?如果是这样,只需以相反的角度施加左右推进器力 - 可能偏离垂直线 15 度。
  • 是的,它会增加冲动。是的,有角度的冲动可能会起作用。

标签: cocos2d-iphone box2d chipmunk love2d libgosu


【解决方案1】:

当你的火箭偏离中心并且只有一枚开火时,你给你的飞船torque。为了模拟这一点,您需要将火箭的推力分成两个部分。第一个推动你的船向前(在它面对的方向),第二个增加你的旋转速度。示例:

pos_x,pos_y - position
vel_x,vel_y - velocity
angle - angle where ship is facing in deg
angle_vel - speed of rotation in deg/s
thrust - how much to add to speed
torque - how much to add to angle
thruster_left, thruster_right - boolean, true if left or right truster is firing

function love.update(dt)
    if thruster_left then
        angle_vel=angle_vel+dt*torque
    end
    if thruster_right then
        angle_vel=angle_vel-dt*torque
    end
    angle=angle+angle_vel
    vel_x=vel_x+thrust*math.sin(math.rad(angle))*dt
    vel_y=vel_y-thrust*math.cos(math.rad(angle))*dt
    pos_x=pos_x+vel_x*dt
    pos_y=pos_y+vel_y*dt
end

【讨论】:

    猜你喜欢
    • 2011-01-30
    • 2012-12-14
    • 1970-01-01
    • 2021-07-27
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多