# SolvedSubtracting angles and normalizing them

#### Rake

I'm using good ol' vec3 to hold angles in degrees. I subtract the angle to look at the enemy, from my current view angle to get the difference. I use this difference to sort targets by angle.

I'm using this homebrew function I made about a year ago:
C++:
``````float DifferenceOfAngles(vec3_t src, vec3_t dst)
{
vec3_t diff;

diff.y = src.y - dst.y;
diff.x = src.x - dst.x;

//normalize by making them positive values if they are negative
if (diff.y < 0)
{
diff.y *= -1;
}
if (diff.x < 0)
{
diff.x *= -1;
}

//add them together and divide by 2, gives an average of the 2 angles
float fDifference = (diff.y + diff.x) / 2;
return fDifference;
}``````
This works very well except when the enemy is behind you it's not 100% perfect. I wanted to make a cooler function using real trigonometry

I thought you could subtract the vectors and divide the resulting vector by the magnitude to normalize the angle, this works to some degree but same issue, when the enemy is behind you the calculation isn't 100% perfect.

Function definitions:
C++:
``````vec3_t Subtract(vec3_t src, vec3_t dst)
{
vec3_t diff;
diff.x = src.x - dst.x;
diff.y = src.y - dst.y;
diff.z = src.z - dst.z;
return diff;
}

vec3_t Divide(vec3_t src, float num)
{
vec3_t vec;
vec.x = src.x / num;
vec.y = src.y / num;
vec.z = src.z / num;
return vec;
}

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

vec3_t Normalize(vec3_t src)
{
vec3_t vec = Divide(src, Magnitude(src));
return vec;
}``````
C++:
``````vec3_t angleDiff = Subtract(localPlayer.angle, angleToEnemy);

float diff = Normalize(angleDiff);``````

I believe this is because the discrepancy between one angle being based on ~360 and the other angle being based on ~180.
Writing 2 seperate normalize functions would be the solution?
By adding 360 and 180 respectively to the angles?

#### Rake

The solution to this was not to use my stupid DifferenceOfAngles function (which actually worked decently for being hacky), but instead just to use the distance formula

C++:
``````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);
}``````

Distance(CurrentViewAngles, AimbotAngles) will give you the difference of the angles. You can sort your target list with these results, aim at the lowest one = closest to crosshair

Last edited:

#### mambda

Escobar Tier VIP
Trump Tier Donator
Just to decide which enemy is closer for sorting?

I'd just either calculate the angles necessary to aim at them and check the angle difference of each one, or, before aimbot calculations, get their positions on screen and just getting the hypotenuse of a triangle from your crosshair -> their head or whatever

#### Rake

I'd just either calculate the angles necessary to aim at them and check the angle difference of each one
Yeah this is what I'm doing but my problem is when the enemy is very close to 360/0 degrees Yaw, it will do a 180 to look at them instead of the enemy 30 degrees away

#### mambda

Escobar Tier VIP
Trump Tier Donator
Rake;42005 said:
Yeah this is what I'm doing but my problem is when the enemy is very close to 360/0 degrees Yaw, it will do a 180 to look at them instead of the enemy 30 degrees away
Do you want to aim at enemies behind you if theres none in front? If not i'd just W2S and check if theyre behind and just omit them

Otherwise, you'd want to do something like, if the angle to an enemy > 180, enemyAngle-=360, and if < 180, enemyAngle += 360
That may work out

Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

• Fill out the form correctly
• Tell us the game name & coding language
• Post everything we need to know to help you
• Ask specific questions, be descriptive
• Post errors, line numbers & screenshots
• Post code snippets using code tags
• If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.