如何获得两个3D矢量之间的角度(俯仰/偏航)以进行自动调整

时间:2016-09-23 15:39:38

标签: c++ math unity3d trigonometry algebra

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

游戏基于Unity,它使用左手坐标系。 X Y Z是正确的,向上的,向前的。

游戏也使用度数。

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

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;

有人可以帮我吗?我数学很糟糕。

2 个答案:

答案 0 :(得分:0)

  

我试图获取两个矢量之间的角度(我的相机位置   和敌人的位置)

在Unity中

使用Angle结构中的Vector3功能。

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引擎。这是我独立的模块   创建以装配我自己的autoaim。我试着做正确的数学运算   本身。

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

答案 1 :(得分:0)

要计算两个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在坐标vec3和角度vec3中并不总是表示相同的变量。