如何将 X 和 Y 速度转换为单一速度

Ken*_*oid 5 javascript math vector

我该如何将 X 和 Y 速度转换为一种速度?我指的不是角度,而是速度。

var velocityX = some velocity;
var velocityY = some velocity;

// Convert the two X and Y velocities to one velocity
Run Code Online (Sandbox Code Playgroud)

tev*_*dar 5

毕达哥拉斯会说

var velocity = Math.sqrt(velocityX*velocityX+velocityY*velocityX);
Run Code Online (Sandbox Code Playgroud)

他是对的。

其他一些人可能会补充:

var angleInDegrees = Math.atan2(velocityX,velocityY)*180/Math.PI;
Run Code Online (Sandbox Code Playgroud)


Nin*_*olz 3

Math.hypot以所有速度进行。

newVelocity = Math.hypot(velocityX, velocityY);
Run Code Online (Sandbox Code Playgroud)