角度方向 [英] Direction to angle

查看:53
本文介绍了角度方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太擅长数学(而且我从没去过),但我遇到了一个问题:我试图使精灵始终旋转到鼠标光标处.那就是我所拥有的:

I'm not very good at math( and i never was ) and I've a problem: I'm trying to make sprite always rotated to mouse cursor. That's what i have:

 // mx is mouse X and my is mouse y
 double rotate = (atan2(my, mx) * PI);

 // Set rotation takes rotation in degrees
 ship.SetRotation( rad2deg(rotate) );

其中 deg2rad 函数是:

double rad2deg(double rad)
{
    double deg = 0;
    deg = rad * (180/M_PI);
    return deg;
}

很遗憾,它无法正常工作.船旋转得很奇怪(很难定义).而且我不知道要解决这个问题.

Unfortunately it is not working. The ship is rotating very weird ( really hard to define that ). And I don't have any idea to solve this problem.

我正在研究SFML和 SetRotation 需要度数.

I'm working on SFML and SetRotation takes degrees.

谢谢.

推荐答案

您需要定义相对于某个点的旋转.目前,您正在相对于屏幕的一角进行操作,这只会给您90度的角度,可能不是您想要的.您可能希望相对于精灵的位置或相对于屏幕中心旋转,例如

You need to define the rotation relative to some point. Currently you're doing it relative to the corner of the screen, which will only give you 90 degrees and is probably not what you want. You probably want rotation relative to the sprite's location or perhaps relative to the centre of the screen, e.g.

// define centre of screen
const int Y0 = SCREEN_HEIGHT / 2;
const int X0 = SCREEN_WIDTH / 2;

// get rotation angle of mouse location relative to centre of screen
double rotate = (float) (atan2(Y0 - my), ((mx - X0));

[正如其他人所指出的,例如@duffymo,您可能还对 atan2 的参数进行了转置,因此我也进行了此更改.]

这篇关于角度方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆