繁体   English   中英

如何获得自动瞄准的两个 3D 矢量之间的角度(俯仰/偏航)

[英]How to get the angle (pitch/yaw) between two 3D vectors for an autoaim

我正在尝试获取两个向量(我的相机位置和敌人位置)之间的角度来创建自动瞄准/瞄准机器人。

该游戏基于 Unity,它使用左手坐标系。 XYZ 是对的,向上,向前。

游戏也使用度数。

这是我正在尝试的伪代码,但它没有给我正确的俯仰/偏航。

diff = camera_position - enemy_position
hypotenuse = sqrt(diff.x*diff.x + diff.y*diff.y)

angle.x = asinf(diff.z / hypotenuse) * (180 / PI);
angle.y = atan2(diff.y / diff.x) * (180 / PI);
angle.z = 0.0f;

有人可以帮我弄这个吗? 我数学很差。

我正在尝试获取两个向量之间的角度(我的相机位置和敌人位置)

在统一中

使用Vector3结构中的Angle函数。

float angle = Vector3.Angle(camera_position, enemy_position);

或个人角度:

float angleX = Vector3.Angle(new Vector3(camera_position.x, 0, 0), new Vector3(enemy_position.x, 0, 0));
float angleY = Vector3.Angle(new Vector3(0, camera_position.y, 0), new Vector3(0, enemy_position.y, 0));
float angleZ = Vector3.Angle(new Vector3(0, 0, camera_position.z), new Vector3(0, 0, enemy_position.z));

编辑

我没有使用 Unity 引擎。 这是我创建的一个单独的模块,用于装配我自己的自动瞄准。 我正在努力获得正确的数学本身。

在 C++ 中

代码在下面的Angle函数中解释,这是最后一个函数

#include <iostream>
#include <numeric> //for inner_product
#include <vector> //For vector
#include <math.h> //For sqrt, acos and M_PI

float Dot(std::vector<float> lhs, std::vector<float> rhs);
float magnitude(std::vector<float> vec3);
float Angle(std::vector<float> from, std::vector<float> to);
std::vector<float> normalise();

int main()
{
    std::vector<float> from{3, 1, -2};
    std::vector<float> to{5, -3, -7 };

    float angle = Angle(from,to);
    std::cout<<"Angle: "<<angle<<std::endl;
    return 0;
}

//Find Dot/ Scalar product 
float Dot(std::vector<float> lhs, std::vector<float> rhs){
    return std::inner_product(lhs.begin(), lhs.end(), rhs.begin(), 0);
}

//Find the magnitude of the Vector
float magnitude(std::vector<float> vec3)//<! Vector magnitude
{
   return sqrt((vec3[0] * vec3[0]) + (vec3[1] * vec3[1]) + (vec3[2] * vec3[2]));
}

//Normalize Vector. Not needed here
std::vector<float> normalise(std::vector<float> vect)  
{
    std::vector<float> temp{0, 0, 0};
    float length = magnitude(vect);

    temp[0] = vect[0]/length;
    temp[1] = vect[1]/length;
    temp[2] = vect[2]/length;
    return temp;
}

float Angle(std::vector<float> from, std::vector<float> to){
   //Find the scalar/dot product of the provided 2 Vectors
   float dotProduct = Dot(from, to);
   //Find the product of both magnitudes of the vectors then divide dot from it
   dotProduct = dotProduct / (magnitude(from) * magnitude(to));
   //Get the arc cosin of the angle, you now have your angle in radians 
   float arcAcos = acos(dotProduct);
   //Convert to degrees by Multiplying the arc cosin by 180/M_PI
   float angle = arcAcos * 180 / M_PI;
   return angle; 
}

要计算两个 3d 坐标之间的角度(以度为单位),您可以使用此 CalcAngle 函数:

#include <algorithm>
#define PI 3.1415927f

struct vec3
{
    float x, y, z;
}

vec3 Subtract(vec3 src, vec3 dst)
{
    vec3 diff;
    diff.x = src.x - dst.x;
    diff.y = src.y - dst.y;
    diff.z = src.z - dst.z;
    return diff;
}

float Magnitude(vec3 vec)
{
    return sqrtf(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
}

float Distance(vec3 src, vec3 dst)
{
    vec3 diff = Subtract(src, dst);
    return Magnitude(diff);
}

vec3 CalcAngle(vec3 src, vec3 dst)
{
    vec3 angle;
    angle.x = -atan2f(dst.x - src.x, dst.y - src.y) / PI * 180.0f + 180.0f;
    angle.y = asinf((dst.z - src.z) / Distance(src, dst)) * 180.0f / PI;
    angle.z = 0.0f;

    return angle;
}

并发症:

并非所有游戏都使用相同的角度和位置技术。 x、y 和 z 角度的最小值和最大值在每个游戏中都可能不同。 所有游戏的基本思想都是一样的,它们只需要稍作修改即可匹配每个游戏。 例如,在编写代码的游戏中,X 值必须在最后变为负数才能工作。

另一个复杂因素是 X、Y 和 Z 并不总是在坐标和角度 vec3s 中表示相同的变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM