简体   繁体   English

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

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

I'm trying to get the angles between two vectors (My Camera Position and Enemy Position) to create an autoaim/aimbot.我正在尝试获取两个向量(我的相机位置和敌人位置)之间的角度来创建自动瞄准/瞄准机器人。

The game is Unity based, it uses the left handed coordinate system.该游戏基于 Unity,它使用左手坐标系。 XYZ is right, up, forward. XYZ 是对的,向上,向前。

The game also uses degrees.游戏也使用度数。

Here is the pseudocode I am trying but its failing to give me the proper pitch/yaw.这是我正在尝试的伪代码,但它没有给我正确的俯仰/偏航。

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;

Can someone help me with this?有人可以帮我弄这个吗? I am terrible at math.我数学很差。

I'm trying to get the angles between two vectors (My Camera Position and Enemy Position)我正在尝试获取两个向量之间的角度(我的相机位置和敌人位置)

In Unity :在统一中

Use the Angle function from Vector3 structure.使用Vector3结构中的Angle函数。

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

Or Individual angles:或个人角度:

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

EDIT :编辑

I'm not using the Unity engine.我没有使用 Unity 引擎。 This is a separate module I am creating to rig my own autoaim.这是我创建的一个单独的模块,用于装配我自己的自动瞄准。 I'm trying to do get the proper math itself.我正在努力获得正确的数学本身。

In C++ :在 C++ 中

The code is explained in the Angle function below which is the last function代码在下面的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; 
}

To calculate the angle between two 3d coordinates, in degrees you can use this CalcAngle Function:要计算两个 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;
}

Complications:并发症:

Not all games use the same technique for angles and positions.并非所有游戏都使用相同的角度和位置技术。 Min and Max values for x, y and z angles can be different in every game. x、y 和 z 角度的最小值和最大值在每个游戏中都可能不同。 The basic idea is the same in all games, they just require minor modification to match each game.所有游戏的基本思想都是一样的,它们只需要稍作修改即可匹配每个游戏。 For example, in the game the code was written for, the X value has to be made negative at the end for it to work.例如,在编写代码的游戏中,X 值必须在最后变为负数才能工作。

Another complication is X, Y and Z don't always represent the same variables in both coordinates and angle vec3s.另一个复杂因素是 X、Y 和 Z 并不总是在坐标和角度 vec3s 中表示相同的变量。

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

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