【问题标题】:Allegro and C++ - Difficulty with mouse position logicAllegro 和 C++ - 鼠标位置逻辑的困难
【发布时间】:2014-10-15 13:17:32
【问题描述】:

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

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 后继续移动,但要在相对鼠标位置之后保持在定义的距离“圆圈”内,但我很难在脑海中创建逻辑。非常感谢您更有经验的人可以提供的任何帮助!

【问题讨论】:

    标签: c++ logic mouse allegro


    【解决方案1】:
    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. fist 在最大范围内的计算位置

    详细说明:

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

    使用这些fist 应该继续移动。

    以前没有:

    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;
    

    【讨论】:

    • 太棒了!我在使用 atan2 时遇到了一个小错误,但很快就会弄清楚,你肯定为我解决了这个问题。
    • 作为一个小提示,我只需要颠倒底部的两个语句,以便为 fist_x 提供 cos,为 fist_y 提供 sin,但这是一个微小的修正。再次感谢您。
    • 修复了答案中的sin/cos 问题。谢谢。如果它解决了问题,您可以接受;)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      相关资源
      最近更新 更多