c++ - Allegro 和 C++ - 鼠标位置逻辑有困难

标签 c++ logic mouse allegro

我正在制作一个简单的游戏,该游戏旨在使用计算机的鼠标来控制其中的一个对象,但我很难让它按照我想要的方式运行。该对象(在本例中标记为“拳头”)旨在跟随鼠标在屏幕上的位置,直到某个常数(“到达”),然后它将继续跟随鼠标,但只有在最大值的范围内抵达。基本上,这个人的手会跟随您的鼠标到达一个点,但如果您的鼠标离得太远,他的手也不会从他的肩膀上扯下来。到目前为止,我的函数代码是这样的:

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;

我现在遇到的主要问题是,当角色在关卡中移动时,当玩家走得太远时,手会留在后面并锁定在原位。 我还希望拳头对象在距离达到 REACH 后继续移动,但要保持在相对鼠标位置之后定义的距离“圆圈”内,但我很难在脑海中创建逻辑。非常感谢您更有经验的人可以提供的任何帮助!

最佳答案

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;
}

我做了一些改变:

  1. 删除了 abs() 函数并更改了 x_disty_dist 赋值中的项顺序。不需要绝对值(x*x 始终为正),它现在表示距离和方向(+x 向右等)。
  2. 添加了鼠标角度的计算
  3. 计算出拳头在最大范围内的位置

详细说明:

2:atan2 与标准 atan 函数不同,它返回一个代表输入描述的扇区的角度。这使我们能够使用像“135 度”这样的角度,而不必计算角度的位置(从原点顺时针方向的第二个扇区)并为每个扇区添加 90 度。
3:只需使用 sin 和 cos 计算 radius = REACH 的位置

使用这些,拳头 应该会继续移动。

它以前没有:

fist_x = mousepos.x - (mousepos.x - fist_x);
fist_y = mousepos.y - (mousepos.y - fist_y);

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;

关于c++ - Allegro 和 C++ - 鼠标位置逻辑有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26383604/

相关文章:

c++ - 如何配置 Qt 以使用 Visual Studio 2010?

computer-science - 逻辑门 : Realize OR gate using ONLY XOR gates

javascript - 在 framer 滚动组件上监听滚动

Javascript "onmousemove"事件不适用于类

python - win32api虚拟移动鼠标

javascript - PreventDefault 与 if check 跳过处理鼠标和触摸?

c++ - 如何从 Windows 库的 GUID 获取 PIDL?

c++ - 为 MyVector 创建一个 push_back() 函数

c++ - 编译错误 : cpumask. h: "and"可能不会出现在宏参数列表中

vba - 如果 OR 语句已经找到 FALSE 条件,它会检查所有条件吗?