2016-07-25 10 views
0

私はそれが指し示している角度でプレーヤーを加速したいと思います。私はコロナのゲームエンジンでこれを試しています。私は加速が速度と時間の変化率であることを知っていますが、これをコードにどのように適用すればよいですか?どのように私はどの角度でそれを加速するのですか?ここ特定の方向に宇宙船を加速する方法(コロナSDK)

は、私が試したものです:

player.angle = 30 
player.speed = 20 
player.acceleration = 2 
print(player.angle) 
local scale_x = math.cos(player.angle) 
local scale_y = math.sin(player.angle) 

local function acceleratePlayer (event) 

if(event.phase=="began") then 

player.speed = player.speed + player.acceleration 

player.velocity_x = player.speed * scale_x 
      player.velocity_y = player.speed * scale_y 
      player.x = player.x + player.velocity_x 
      player.y = player.y + player.velocity_y 
    end 
end 
+0

accelerating_angleはplayer.angleと似ていますか?または私はラジアン値でそれを変換する必要がありますか? – Nit

答えて

0

あなたが状態として、加速度は速度の経時変化です。ここで

velocity = velocity + acceleration*deltaTime 

deltaTimeがあなたの最後の速度の更新から時間変化であるとして、あなたはコードでこれを記述します。上のコードでdeltaTime = 1を使用しています。

任意の角度で加速するには、加速に2つのパラメータが必要です。あなたが望むものに応じて、acceleration_angleまたはを追加して、acceleration_xacceleration_yと定義することができます。両方の作品は、あなたの気持ちによりますが、より直感的です。角度依存である加速から

更新速度:yについては

// acceleration is relative to player 
velocity_x = velocity_x + acceleration*cos(player.angle + acceleration_angle)*deltaTime; 
// acceleration is *not* relative to player 
velocity_x = velocity_x + acceleration*cos(acceleration_angle)*deltaTime; 

あなただけsincosを変更します。角度を使用して更新しない場合は、それぞれの方向を個別に更新するだけです:

velocity_x = velocity_x + acceleration_x*deltaTime; 
velocity_y = velocity_y + acceleration_y*deltaTime; 
+0

ありがとう、私はこのアプローチを試してみましょう!私のステータスを更新する – Nit

関連する問題