Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 快板和C++;-鼠标位置逻辑困难_C++_Logic_Mouse_Allegro - Fatal编程技术网

C++ 快板和C++;-鼠标位置逻辑困难

C++ 快板和C++;-鼠标位置逻辑困难,c++,logic,mouse,allegro,C++,Logic,Mouse,Allegro,我正在做一个简单的游戏,就是用电脑的鼠标来控制里面的一个物体,但我很难让它发挥我想要的功能。对象(在本例中标记为“fist”)将跟随鼠标在屏幕上的位置,直到某个常数(“到达”),然后它将继续跟随鼠标,但仅在最大到达范围内。基本上,这家伙的手会跟着你的鼠标走到某个点,但如果你的鼠标走得太远,他的手不会从他的肩膀上扯下来。到目前为止,我为函数编写的代码如下: void FistPosition(int& player_x,int& player_y, int& fist_x,

我正在做一个简单的游戏,就是用电脑的鼠标来控制里面的一个物体,但我很难让它发挥我想要的功能。对象(在本例中标记为“fist”)将跟随鼠标在屏幕上的位置,直到某个常数(“到达”),然后它将继续跟随鼠标,但仅在最大到达范围内。基本上,这家伙的手会跟着你的鼠标走到某个点,但如果你的鼠标走得太远,他的手不会从他的肩膀上扯下来。到目前为止,我为函数编写的代码如下:

void FistPosition(int& player_x,int& player_y, int& fist_x, int& fist_y){ //Start
ALLEGRO_MOUSE_STATE mousepos;
al_get_mouse_state(&mousepos); //Get the mouse's x and y position

const int REACH = 150; //Define the maximum distance the fist can go.

int playerc_x = player_x + 62; //Define the x and y center of the player object
int playerc_y = player_y + 92;

double x_dist = abs(playerc_x - mousepos.x); //get the x and y distance between
double y_dist = abs(playerc_y - mousepos.y); //body and mouse

int mousedist = sqrt((x_dist * x_dist) + (y_dist * y_dist)); //define mouse distance

if (mousedist < REACH){ //If within bounds of reach, follow the mouse position exactly
    fist_x = mousepos.x;
    fist_y = mousepos.y;
}
else{
    fist_x = mousepos.x - (mousepos.x - fist_x); //Otherwise it cannot leave the 
    fist_y = mousepos.y - (mousepos.y - fist_y); //maximum reach
}
return;
void FistPosition(int&player_x,int&player_y,int&fist_x,int&fist_y){//Start
快板、滑鼠、州滑鼠;
al_get_mouse_state(&mousepos);//获取鼠标的x和y位置
const int REACH=150;//定义拳头可以到达的最大距离。
int playerc_x=player_x+62;//定义player对象的x和y中心
int playerc_y=玩家_y+92;
double x_dist=abs(playerc_x-mousepos.x);//获取
双y_dist=abs(playerc_y-mousepos.y);//身体和鼠标
int mousedist=sqrt((x_dist*x_dist)+(y_dist*y_dist));//定义鼠标距离
如果(mousedist
}

我现在面临的主要问题是,当角色在关卡中移动时,当玩家走得太远时,手会留在后面并锁定到位。
我还希望第一个对象在距离到达后继续移动,但在相对鼠标位置之后保持在定义的距离“圆圈”内,但我很难在头脑中创建这样的逻辑。非常感谢更有经验的人能给予你的任何帮助

在极坐标系中思考可能更好。将鼠标位置与肩部位置进行比较,以获得所需的角度和半径。夹紧半径以达到,然后转换回直角坐标

void FistPosition(int& player_x,int& player_y, int& fist_x, int& fist_y){ //Start
    ALLEGRO_MOUSE_STATE mousepos;
    al_get_mouse_state(&mousepos); //Get the mouse's x and y position

    const int REACH = 150; //Define the maximum distance the fist can go.

    int playerc_x = player_x + 62; //Define the x and y center of the player object
    int playerc_y = player_y + 92;

    double x_dist = mousepos.x - playerc_x; //get the x and y distance between
    double y_dist = mousepos.y - playerc_y; //body and mouse

    int mousedist = sqrt((x_dist * x_dist) + (y_dist * y_dist)); //define mouse distance

    if (mousedist < REACH){ //If within bounds of reach, follow the mouse position exactly
        fist_x = mousepos.x;
        fist_y = mousepos.y;
    }
    else{
        double angle = atan2(y_dist/xdist);  //work out the angle to the mouse
        fist_x = playerc_x + REACH*cos(angle);
        fist_x = playerc_y + REACH*sin(angle);
    }
    return;
}
mousepos
术语取消,因此它实际上只是:

//this line:      mousepos.x - (mousepos.x - fist_x); 
//is the same as: mousepos.x - mousepos.x + fist_x;
fist_x = fist_x;
fist_y = fist_y;

太棒了!我在使用atan2时遇到了一个小错误,但这不会花很长时间来解决,你肯定为我解决了这个问题。正如一个小提示,我只是必须颠倒底部的两个语句,以便cos代表fist_x,sin代表fist_y,但这是一个微小的更正。再次感谢。修复了回答中的
sin
/
cos
问题。谢谢如果解决了问题,您可以接受;)
//this line:      mousepos.x - (mousepos.x - fist_x); 
//is the same as: mousepos.x - mousepos.x + fist_x;
fist_x = fist_x;
fist_y = fist_y;