【发布时间】:2012-07-05 18:50:03
【问题描述】:
我有一个物理体,我希望它朝着它所面对的方向前进。我只有 13 岁,我希望这能解释为什么我的三角学如此糟糕。谁能告诉我如何在 Corona 中做到这一点?
【问题讨论】:
-
查看最后一条评论here,它可能就是你要找的。span>
我有一个物理体,我希望它朝着它所面对的方向前进。我只有 13 岁,我希望这能解释为什么我的三角学如此糟糕。谁能告诉我如何在 Corona 中做到这一点?
【问题讨论】:
呃。您不需要三角函数来移动对象。
添加
object:translate(distanceToMoveInXAxis,distanceToMoveInYAxis)
或者如果你想执行一个过渡,
transition.to(object,{x=object.x + distanceToMoveInXAxis,y=object.y + distanceToMoveInYAxis})
【讨论】:
object:translate 的论点是什么?
我会假设你想用力推动你的物体。无论哪种方式,我们都需要获取您身体所面对方向的 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
【讨论】:
在使用令人困惑的物理原理之前,让你自己的角色向一个角度移动怎么样?
angle = math.rad(Insert the angle you want here)
character.x = character.x - math.sin(angle)
character.y = character.y + math.cos(angle)
【讨论】: