【问题标题】:Corona SDK: How to make object move forward?Corona SDK:如何让物体向前移动?
【发布时间】:2012-07-05 18:50:03
【问题描述】:

我有一个物理体,我希望它朝着它所面对的方向前进。我只有 13 岁,我希望这能解释为什么我的三角学如此糟糕。谁能告诉我如何在 Corona 中做到这一点?

【问题讨论】:

  • 查看最后一条评论here,它可能就是你要找的。​​span>

标签: lua coronasdk


【解决方案1】:

呃。您不需要三角函数来移动对象。

添加

object:translate(distanceToMoveInXAxis,distanceToMoveInYAxis)

或者如果你想执行一个过渡,

transition.to(object,{x=object.x + distanceToMoveInXAxis,y=object.y + distanceToMoveInYAxis})

【讨论】:

  • 他需要数学来计算从面向和速度/移动距离的 X、Y 移动平移。例如,如果他有一个位于 200,56 的物体,并且它的朝向是 30°(时钟上的第一只手)并且我们想要移动 62 个像素,那么他对object:translate 的论点是什么?
【解决方案2】:

我会假设你想用力推动你的物体。无论哪种方式,我们都需要获取您身体所面对方向的 x 和 y 分量。以下是从旋转角度获取 x 和 y 的方法:

-- body is your physics body
local angle = math.rad(body.rotation)  -- we need angle in radians
local xComp = math.cos(angle)  -- the x component
local yComp = -math.sin(angle)  -- the y component is negative because 
                                --  "up" the screen is negative

(注意:如果这没有给出朝向的方向,您可能需要为您的角度添加 90、180 或 270 度,例如:math.rad(body.rotation+90))

上面的代码会给你unit vector在旋转方向上的x和y分量。你可能还需要一些乘数来获得你想要的力量。

local forceMag = 0.5 -- change this value to apply more or less force
-- now apply the force
body:applyLinearImpulse(forceMag*xComp, forceMag*yComp, body.x, body.y)

这是我得到数学的地方:http://www.mathopenref.com/trigprobslantangle.html。使用单位向量可以简化数学,因为斜边总是 1

【讨论】:

    【解决方案3】:

    在使用令人困惑的物理原理之前,让你自己的角色向一个角度移动怎么样?

    angle = math.rad(Insert the angle you want here)
    character.x = character.x - math.sin(angle)
    character.y = character.y + math.cos(angle)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-04
      • 2017-08-04
      • 1970-01-01
      • 2021-07-10
      • 2017-05-21
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多