1

私は物理的な体を持っていて、それが向いている方向に前進させたいと思っています。私はまだ 13 歳ですが、三角法が苦手な理由がこれで説明できると思います。コロナでこれを行う方法を誰か教えてもらえますか?

4

3 に答える 3

5

I'm gonna assume you want to push your object with a force. Either way we'll need to get an x and y component of the direction your body is facing. Here's how to get the x and y from the rotation angle:

-- 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

(note: if this doesn't give the facing direction, you may need to add 90, 180, or 270 degrees to your angle, for example: math.rad(body.rotation+90) )

The above code will give you the x and y components of the unit vector in the direction of the rotation. You'll probably also need some multiplier to get the magnitude of force you want.

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)

Here's where I got the math: http://www.mathopenref.com/trigprobslantangle.html. Using a unit vector simplifies the math because the hypotenuse is always 1

于 2012-07-06T03:52:36.200 に答える
-1

ええと。オブジェクトを動かすためだけに三角法は必要ありません。

追加

object:translate(distanceToMoveInXAxis,distanceToMoveInYAxis)

または、トランジションを実行したい場合は、

transition.to(object,{x=object.x + distanceToMoveInXAxis,y=object.y + distanceToMoveInYAxis})
于 2012-07-05T12:36:24.127 に答える