VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Math.cpp
Go to the documentation of this file.
2#include <algorithm>
3#include <cmath>
4#include <mathlib/mathlib.h>
5
6namespace VoltMod
7{
8
10{
11 Vector forward;
12 AngleVectors(angles, &forward);
13 return forward;
14}
15
17{
18 return {std::cos(yaw / DegreesPerRadian), std::sin(yaw / DegreesPerRadian), 0.0f};
19}
20
22{
23 return {-std::asin(std::clamp(Forward.z, -1.0f, 1.0f)) * DegreesPerRadian,
24 std::atan2(Forward.y, Forward.x) * DegreesPerRadian, std::atan2(Left.z, Up.z) * DegreesPerRadian};
25}
26
27Axes AngleToAxes(const QAngle& angles)
28{
29 Axes axes;
30 Vector right;
31 AngleVectors(angles, &axes.Forward, &right, &axes.Up);
32 axes.Left = -right;
33 return axes;
34}
35
36Vector OffsetLocal(const Vector& origin, const QAngle& angles, const Vector& forwardLeftUp)
37{
38 const Axes axes = AngleToAxes(angles);
39 return origin + axes.Forward * forwardLeftUp.x + axes.Left * forwardLeftUp.y + axes.Up * forwardLeftUp.z;
40}
41
42QAngle AngleTo(const Vector& from, const Vector& to)
43{
44 const Vector d = to - from;
45 const float across = std::sqrt(d.x * d.x + d.y * d.y);
46 return {-std::atan2(d.z, across) * DegreesPerRadian, std::atan2(d.y, d.x) * DegreesPerRadian, 0.0f};
47}
48
49float AngleDelta(float from, float to)
50{
51 const float diff = std::fmod(to - from + 180.0f, 360.0f);
52 return (diff < 0.0f ? diff + 360.0f : diff) - 180.0f;
53}
54
55float TurnToward(float current, float target, float maxStep)
56{
57 return current + std::clamp(AngleDelta(current, target), -maxStep, maxStep);
58}
59
60} // namespace VoltMod
QAngle AngleTo(const Vector &from, const Vector &to)
Definition Math.cpp:42
Axes AngleToAxes(const QAngle &angles)
Definition Math.cpp:27
Vector AngleToForward(const QAngle &angles)
Definition Math.cpp:9
::QAngle QAngle
Definition Math.hpp:15
Vector YawToForward(float yaw)
Definition Math.cpp:16
float TurnToward(float current, float target, float maxStep)
Definition Math.cpp:55
::Vector Vector
Definition Math.hpp:12
float AngleDelta(float from, float to)
Definition Math.cpp:49
Vector OffsetLocal(const Vector &origin, const QAngle &angles, const Vector &forwardLeftUp)
Definition Math.cpp:36
constexpr float DegreesPerRadian
Definition Math.hpp:17
Vector Left
Definition Math.hpp:29
QAngle ToAngles() const
Definition Math.cpp:21
Vector Up
Definition Math.hpp:30
Vector Forward
Definition Math.hpp:28