This repository has been archived by the owner on Jan 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Math.cpp
161 lines (122 loc) · 4.1 KB
/
Math.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "../../csgo-sdk.h"
RandomSeedFn Math::RandomSeed;
RandomFloatFn Math::RandomFloat;
RandomFloatExpFn Math::RandomFloatExp;
RandomIntFn Math::RandomInt;
RandomGaussianFloatFn Math::RandomGaussianFloat;
void inline Math::SinCos(float radians, float *sine, float *cosine) {
*sine = sin(radians);
*cosine = cos(radians);
}
float Math::DotProduct(Vector a, Vector b) {
float one[] = { a.x, a.y, a.z };
float two[] = { b.x, b.y, b.z };
return std::inner_product(one, one + 3, two, 0.0f);
}
void Math::VectorAngles(const Vector& forward, QAngle& angles) {
if (forward.y == 0 && forward.x == 0) {
angles.x = forward.z > 0.f ? 270.f : 90.f;
angles.y = 0;
}
else {
angles.x = (atan2(-forward.z, forward.Length2D()) * 180 / M_PI);
if (angles.x < 0)
angles.x += 360;
angles.y = (atan2(forward.y, forward.x) * 180 / M_PI);
if (angles.y < 0)
angles.y += 360;
}
angles.z = 0;
}
void Math::AngleVectors(const QAngle& angles, Vector& forward) {
float sp, sy, cp, cy;
SinCos(DEG2RAD(angles[YAW]), &sy, &cy);
SinCos(DEG2RAD(angles[PITCH]), &sp, &cp);
forward.x = cp*cy;
forward.y = cp*sy;
forward.z = -sp;
}
void Math::AngleVectors(const QAngle& angles, Vector* forward, Vector* right, Vector* up) {
float sr, sp, sy, cr, cp, cy;
SinCos(DEG2RAD(angles[YAW]), &sy, &cy);
SinCos(DEG2RAD(angles[PITCH]), &sp, &cp);
SinCos(DEG2RAD(angles[ROLL]), &sr, &cr);
if (forward) {
forward->x = cp*cy;
forward->y = cp*sy;
forward->z = -sp;
}
if (right) {
right->x = (-1 * sr*sp*cy + -1 * cr*-sy);
right->y = (-1 * sr*sp*sy + -1 * cr*cy);
right->z = -1 * sr*cp;
}
if (up) {
up->x = (cr*sp*cy + -sr*-sy);
up->y = (cr*sp*sy + -sr*cy);
up->z = cr*cp;
}
}
float Math::GetFov(const QAngle& viewAngle, const QAngle& aimAngle) {
Vector ang, aim;
AngleVectors(viewAngle, aim);
AngleVectors(aimAngle, ang);
return RAD2DEG(acos(aim.Dot(ang) / aim.LengthSqr()));
}
QAngle Math::CalcAngle(Vector src, Vector dst) {
QAngle angles;
VectorAngles((dst - src), angles);
angles.Normalize();
return angles;
}
float Math::VectorDistance(Vector v1, Vector v2) {
return sqrtf(pow(v1.x - v2.x, 2) + pow(v1.y - v2.y, 2) + pow(v1.z - v2.z, 2));
}
Vector Math::VectorTransform(const Vector& in1, const matrix3x4a_t& in2) {
return Vector(in1.Dot(in2.m_flMatVal[0]) + in2.m_flMatVal[0][3], in1.Dot(in2.m_flMatVal[1]) + in2.m_flMatVal[1][3], in1.Dot(in2.m_flMatVal[2]) + in2.m_flMatVal[2][3]);
}
float Math::GetDelta(float hspeed, float maxspeed, float airaccelerate) {
auto term = (30.0 - (airaccelerate * maxspeed / 66.0)) / hspeed;
if (term < 1.0f && term > -1.0f) {
return acosf(static_cast<float>(term));
}
return 0.f;
}
Vector Math::ExtrapolateTick(Vector p0, Vector v0) {
return p0 + (v0 * Interfaces->Globals->interval_per_tick);
}
inline float Math::GetHitchance(CBaseEntity* ent, QAngle angle) {
CBaseCombatWeapon* Weapon = G::LocalPlayer->GetWeapon();
//angle.Normalize();
Vector Forward, Right, Up, Dir;
AngleVectors(angle, &Forward, &Right, &Up);
int SeedsHit = 0;
Weapon->UpdateAccuracyPenalty();
float wSpread = Weapon->GetWeaponSpread();
float wCone = Weapon->GetWeaponCone();
Vector myEyePos = G::LocalPlayer->GetEyePosition();
for (int i = 0; i < 256; i++) {
RandomSeed(i + 1);
float A = RandomFloat(0.0f, 1.0f);
float B = RandomFloat(0.f, 2.0f * M_PI);
float C = RandomFloat(0.0f, 1.0f);
float D = RandomFloat(0.f, 2.0f * M_PI);
float Spread = A * wSpread;
float Cone = C * wCone;
Vector vSpread((cos(B) * Spread) + (cos(D) * Cone), (sin(B) * Spread) + (sin(D) * Cone), 0), vDir;
vDir.x = Forward.x + (Right.x * vSpread.x) + (Up.x * vSpread.y);
vDir.y = Forward.y + (Right.y * vSpread.x) + (Up.y * vSpread.y);
vDir.z = Forward.z + (Right.z * vSpread.x) + (Up.z * vSpread.y);
Vector TraceEnd = myEyePos + (vDir * Weapon->GetCSWpnData()->m_flRange);
CGameTrace tr;
Ray_t ray;
ray.Init(myEyePos, TraceEnd);
CTraceFilterEntitiesOnly filter;
filter.pSkip = G::LocalPlayer;
filter.m_iCollisionGroup = COLLISION_GROUP_NONE;
Interfaces->EngineTrace->ClipRayToEntity(ray, MASK_SHOT, ent, &tr);
if (tr.m_pEnt == ent)
SeedsHit++;
}
return ((SeedsHit / 256.f) * 100.f);
}