从原点坐标中找到代表行进方向的角度

用户名

我正在制作动画,其中许多三角形对象在屏幕上移动。为了确保每个面朝其行进的方向,我需要将图像旋转适当的角度。

我的问题是我的代码返回的角度不能正确执行此操作。返回的值变化不超过几度。

/**
 * Accepts two grid positions are arguments. The current position
 * of the object and the next grid position. Returns an angle representing 
 * the direction of travel from the current position towards the next position. By converting the Cartesian coordinates into polar coordinates. 
 *  
 */
public void setAngle(Vector2d currentPos, Vector2d nextPos ) {
    Double delta_x = current.xPos - next.xPos;
    Double delta_y = current.yPos - next.yPos;
    Double theta = Math.atan2(delta_y, delta_x);
    this.angle = theta;
}   


Example:

|| current: 1031.1438073417544 , 268.3133503758045 || next: 1033.101761841174 , 269.0819944286846 || Angle: 0.0

|| current: 1033.1901579769194 , 242.19363555578593 || next: 1035.1281222295695 , 243.08778242413436 || Angle: 0.0

|| current: 1022.1577455080815 , 255.24422527831163 || next: 1024.0301966330894 , 256.19078788718997 || Angle: 0.0
// calc the deltas as next minus current
double delta_x = next.xPos - current.xPos;
double delta_y = next.yPos - current.yPos;

// Calc the angle IN RADIANS using the atan2
double theta = Math.atan2(delta_y, delta_x);

// this.angle is now in degrees
// or leave off *180/Math.PI if you want radians
this.angle = theta*180/Math.PI;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章