c ++ Sfml尝试每帧将对象移动到特定数量的鼠标位置

c++ Sfml trying to move an object to the Mouse Location by a particular amount every frame

我有点被这个问题困住了

我创建了这个功能,但由于某些原因我只能将对象移动到播放器的右侧。

如果我尝试将对象移动到播放器的左侧,则它会向右移动。

这是我的方法:

int Speed = 8;
int x = Player_x - Mouse_x;
int y = Player_y - MOuse_y; 

float deg = atan(y / x);

float erg_x = Speed * cos(deg);
float erg_y = Speed * sin(deg);

erg_x/y 是我最后用来移动对象的数字。

请帮助我:)

here所述,atan仅适用于第一和第四象限。由于向左走涉及第二象限,所以这行不通。

因此,您需要更改

float deg = atan(y / x);

float deg = atan2(y, x);