Game Audio Module
A C++ audio system using miniaudio with Python bindings
Loading...
Searching...
No Matches
vec3.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4
24namespace audio {
25
46struct Vec3 {
47 float x;
48 float y;
49 float z;
50
54 Vec3() : x(0.0f), y(0.0f), z(0.0f) {}
55
63 Vec3(float x, float y, float z) : x(x), y(y), z(z) {}
64
70 float Length() const {
71 return std::sqrt(x * x + y * y + z * z);
72 }
73
79 float LengthSquared() const {
80 return x * x + y * y + z * z;
81 }
82
86 void Normalize() {
87 float len = Length();
88 if (len > 0.0f) {
89 x /= len;
90 y /= len;
91 z /= len;
92 }
93 }
94
100 Vec3 Normalized() const {
101 Vec3 result = *this;
102 result.Normalize();
103 return result;
104 }
105
112 float Distance(const Vec3& other) const {
113 return (*this - other).Length();
114 }
115
122 float DistanceSquared(const Vec3& other) const {
123 return (*this - other).LengthSquared();
124 }
125
126 // Arithmetic operators
127 Vec3 operator+(const Vec3& other) const {
128 return Vec3(x + other.x, y + other.y, z + other.z);
129 }
130
131 Vec3 operator-(const Vec3& other) const {
132 return Vec3(x - other.x, y - other.y, z - other.z);
133 }
134
135 Vec3 operator*(float scalar) const {
136 return Vec3(x * scalar, y * scalar, z * scalar);
137 }
138
139 Vec3 operator/(float scalar) const {
140 return Vec3(x / scalar, y / scalar, z / scalar);
141 }
142
143 Vec3& operator+=(const Vec3& other) {
144 x += other.x;
145 y += other.y;
146 z += other.z;
147 return *this;
148 }
149
150 Vec3& operator-=(const Vec3& other) {
151 x -= other.x;
152 y -= other.y;
153 z -= other.z;
154 return *this;
155 }
156
157 Vec3& operator*=(float scalar) {
158 x *= scalar;
159 y *= scalar;
160 z *= scalar;
161 return *this;
162 }
163
164 Vec3& operator/=(float scalar) {
165 x /= scalar;
166 y /= scalar;
167 z /= scalar;
168 return *this;
169 }
170
171 bool operator==(const Vec3& other) const {
172 return x == other.x && y == other.y && z == other.z;
173 }
174
175 bool operator!=(const Vec3& other) const {
176 return !(*this == other);
177 }
178};
179
187inline float Dot(const Vec3& a, const Vec3& b) {
188 return a.x * b.x + a.y * b.y + a.z * b.z;
189}
190
198inline Vec3 Cross(const Vec3& a, const Vec3& b) {
199 return Vec3(
200 a.y * b.z - a.z * b.y,
201 a.z * b.x - a.x * b.z,
202 a.x * b.y - a.y * b.x
203 );
204}
205
206} // namespace audio
Definition audio_group.cpp:7
Vec3 Cross(const Vec3 &a, const Vec3 &b)
Cross product of two vectors.
Definition vec3.h:198
float Dot(const Vec3 &a, const Vec3 &b)
Dot product of two vectors.
Definition vec3.h:187
3D vector for spatial audio positioning
Definition vec3.h:46
Vec3 operator/(float scalar) const
Definition vec3.h:139
float y
Y component.
Definition vec3.h:48
Vec3 & operator*=(float scalar)
Definition vec3.h:157
Vec3 operator+(const Vec3 &other) const
Definition vec3.h:127
float z
Z component.
Definition vec3.h:49
bool operator==(const Vec3 &other) const
Definition vec3.h:171
float LengthSquared() const
Get the squared length of the vector (faster, no sqrt)
Definition vec3.h:79
Vec3 & operator-=(const Vec3 &other)
Definition vec3.h:150
Vec3()
Default constructor (initializes to origin)
Definition vec3.h:54
Vec3(float x, float y, float z)
Constructor with components.
Definition vec3.h:63
float x
X component.
Definition vec3.h:47
void Normalize()
Normalize the vector in place.
Definition vec3.h:86
Vec3 operator-(const Vec3 &other) const
Definition vec3.h:131
float Length() const
Get the length of the vector.
Definition vec3.h:70
Vec3 & operator+=(const Vec3 &other)
Definition vec3.h:143
float DistanceSquared(const Vec3 &other) const
Calculate squared distance to another point (faster, no sqrt)
Definition vec3.h:122
Vec3 operator*(float scalar) const
Definition vec3.h:135
Vec3 & operator/=(float scalar)
Definition vec3.h:164
float Distance(const Vec3 &other) const
Calculate distance to another point.
Definition vec3.h:112
Vec3 Normalized() const
Get a normalized copy of the vector.
Definition vec3.h:100
bool operator!=(const Vec3 &other) const
Definition vec3.h:175