c++ - 2d map 上 2 点之间的航点生成器

标签 c++ multidimensional-array

我现在正尝试在我的 2D map 路径上生成直线同步点。
换句话说,我想在 map 上的点 A 之间溢出距离,例如 X:301 Y:679 到点 B X:360 Y:630,每 8 个距离单位传递一次。
通过 sqrt(pow(a_x - b_x, 2), pow(a_y - b_y, 2)) 计算的每 8 个距离单位。
我想获取 map 上的下一个坐标,例如通过 a a_x + 距离和 b_y + 距离。
我尝试这样做,但没有成功,x 轴没有适当改变。
这是我的代码:

float base_x = active->getX();
    float base_y = active->getY();

    float destx = incoming_packet.get()->getFloat(4);
    float desty = incoming_packet.get()->getFloat(8);

    float distance = active->distance(destx, desty); // calculated by sqrt(pow(curent character x pos - destx, 2), pow(current character y pos - desty, 2))

    float delta_X = active->vDistance(base_x, destx); // calculated by sqrt(pow(base_x - destx, 2))
    float delta_Y = active->vDistance(base_y, desty);  // calculated by sqrt(pow(base_y - desty, 2))

    float cosa = delta_X / distance;
    float sina = delta_Y / distance;



    int mapx = 1;
    int mapy = 1;

    if(distance > 8)///active sync
    {
        for(float dist = 8; dist < distance;dist+=8)
        {
            base_x += mapx * (cosa * 8);
            base_y += mapy * (sina * 8);
            BOOST_LOG_TRIVIAL(debug) << "[ACTIVESYNC]NEXT SYNC ACK X : " << base_x << " Y : " << base_y;
         }
    }

我在这里做错了什么?

最佳答案

“cosa”(和 cosb)显然是无量纲的。 (即米/米)

“mapx”(和“mapy”)也是无量纲的。

请注意,在您的 for 循环中,base_x、base_y 描述了 map 上的一个点。 以及该循环中的 2 个有趣的计算

base_x += mapx * (cosa * 8);
base_y += mapy * (sina * 8);

通过尝试将无量纲数字添加到一个点而变得无意义。乘以无量纲数可能没问题,但将无量纲数加到 map 点上是不合理的。

我建议更改 cosa 和 cosb 以表示每一步的距离(即米)。

float cosa = delta_X / 8;  // size of steps in x direction
float sina = delta_Y / 8;  // size of steps in y direction

现在for循环可以适当增加8步的cosa和sina来描述路径点,cosa和sina都有合适的维度进行下一步的计算。

for 循环可以简化为:

  for(int step = 0; step < 8; step += 1)
  {
     base_x += (mapx * cosa);
     base_y += (mapy * sina);

     // remove or adapt the following 
     std::cout << std::setw(10) << std::left << (step+1) << std::setw(10) 
               << base_x  <<  std::setw(10) << base_y << std::endl;

     // you were using:
     //BOOST_LOG_TRIVIAL(debug) << "[ACTIVESYNC]NEXT SYNC ACK X : " 
     //                         << base_x << " Y : " << base_y;
  }

我的伪代码输出:

base  x/y  301 679
dest  x/y  360 630
delta x/y   59 -49

step_x  = 7.375
step_y  = -6.125

step      base_x    base_y
0         301       679       
1         308.375   672.875   
2         315.75    666.75    
3         323.125   660.625   
4         330.5     654.5     
5         337.875   648.375   
6         345.25    642.25    
7         352.625   636.125   
8         360       630       

这些方式点是否看起来更像您正在寻找的东西?

关于c++ - 2d map 上 2 点之间的航点生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21054463/

相关文章:

c++ - 如何在 Ubuntu 18.04.3 上将 "upgrade"转换为 OpenGL 3.3

c++ - Luabind 编译时错误 Unable to deduce template argument

multidimensional-array - 如何将2D String数组(未知尺寸)作为参数传递给Rust中的函数

c++ - 定义所有内容时出现链接器错误 2019

c++ - asio::io_service::strand 的正确用法?

c++ - 使用 Makefile 在多个文件 C++ 中构建互连类

python - 将带有 Numpy Nd-Array 值的 python 字典写入 Json 文件的有效方法

java - 移动数组中的元素

JavaScript 按星期几对数组数组进行排序

c - 分配一个二维数组