【问题标题】:Rotating speed of a cannon大炮的转速
【发布时间】:2014-03-18 05:57:45
【问题描述】:

在我正在制作的当前应用程序中,我正在使用大炮向目标射击球,但大炮旋转得太快。当我触摸屏幕时,大炮以逆时针方向快速旋转,我想降低旋转速度。我将 angulardamping 设置为 100,但速度似乎没有改变。

function charge()
     cannon.angularDamping = 100
     cannon.rotation = cannon.rotation - 5
     impulse=impulse-0.2

     if(cannon.rotation < -46) then
          cannon.rotation = -46
          impulse = -3.2
     end
end

【问题讨论】:

  • 你是把大炮作为物体还是物理物体移动? AngularDamping 处理物理引擎中使用的对象。

标签: lua coronasdk


【解决方案1】:

你的大炮当前的旋转速度是每帧 5 度(假设你调用 charge() 作为 enterFrame 处理程序的一部分)因为你有这个:

cannon.rotation = cannon.rotation - 5

因此,如果您希望它旋转得更慢,请尝试以下任一方法:

local degreesPerFrame = 1 -- or 0.1 or whatever, try several
cannon.rotation = cannon.rotation - degreesPerFrame 

如果你想要一个特定的速度,那么你可以通过event.time - appStartTime 获取自上次enterFrame 以来的时间。然后你会使用

function enterFrame(event)
    if appStartTime == nil then 
         appStartTime = event.time
    else
         deltaTime = event.time - appStartTime
         charge(deltaTime)
    end
end

function charge(deltaTime) 
    local degreesPerSec = 1 -- or 0.1 or whatever, try several
    cannon.rotation = cannon.rotation - degreesPerSec * deltaTime
    ...
end

或者,您可以使您的对象成为运动物理体(注意:不是动态的):

physics.addBody(cannon, "kinematic", {isSensor = false})
cannon.angularVelocity = 1 -- deg/s

但是,您的代码中似乎有动态。如果您的对象是“动态”物理对象(使用“动态”而不是“运动学”创建),则必须施加扭矩才能使其转动。恒定扭矩将导致非零角加速度,即只要施加扭矩就会导致角速度增加,除非存在与速度相关的阻尼,否则通常会有一个最大值,阻尼正好抵消扭矩并且物体达到稳定的角速度。

使用动态对象会给你带来更平滑、更逼真的变化,但在使用大炮的情况下可能会过大。我会坚持运动学或根本没有物理学,只是改变旋转值。

【讨论】:

    【解决方案2】:

    angularDamping 降低物理体角速度。

    如果你直接设置身体旋转,它不会做任何事情。

    尝试使用canon:applyAngularImpulse(-5) 旋转它,看看angularDamping 如何降低旋转速度。不要手动触摸旋转。

    您也可以使用电机或旋转接头,精确控制卡农旋转。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多