A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Vector.cc
1#include "Vector.hh"
2
3namespace EGG {
4
6f32 Vector2f::normalise() {
7 f32 len = length();
8 if (len != 0.0f) {
9 *this = *this * (1.0f / len);
10 }
11
12 return len;
13}
14
16void Vector2f::normalise2() {
17 f32 sqLen = dot();
18 if (sqLen > std::numeric_limits<f32>::epsilon()) {
19 *this *= EGG::Mathf::frsqrt(sqLen);
20 }
21}
22
24void Vector2f::read(Stream &stream) {
25 x = stream.read_f32();
26 y = stream.read_f32();
27}
28
30f32 Vector3f::ps_dot() const {
31 return ps_dot(*this);
32}
33
36f32 Vector3f::ps_dot(const Vector3f &rhs) const {
37 f32 y_ = y * rhs.y;
38 f32 xy = Mathf::fma(x, rhs.x, y_);
39 return xy + z * rhs.z;
40}
41
44 f32 x_ = x * x;
45 f32 zx = Mathf::fma(z, z, x_);
46 return zx + y * y;
47}
48
53 f32 len = 0.0f;
54
55 if (squaredLength() > std::numeric_limits<f32>::epsilon()) {
56 len = length();
57 *this *= (1.0f / len);
58 }
59
60 return len;
61}
62
64void Vector3f::normalise2() {
65 f32 sqLen = squaredLength();
66 if (sqLen > std::numeric_limits<f32>::epsilon()) {
67 *this *= Mathf::frsqrt(sqLen);
68 }
69}
70
74 Vector3f out;
75
76 out.x = x > rhs.x ? x : rhs.x;
77 out.y = y > rhs.y ? y : rhs.y;
78 out.z = z > rhs.z ? z : rhs.z;
79
80 return out;
81}
82
86 Vector3f out;
87
88 out.x = x < rhs.x ? x : rhs.x;
89 out.y = y < rhs.y ? y : rhs.y;
90 out.z = z < rhs.z ? z : rhs.z;
91
92 return out;
93}
94
97f32 Vector3f::ps_sqDistance(const Vector3f &rhs) const {
98 const EGG::Vector3f diff = *this - rhs;
99 return diff.ps_dot();
100}
101
104Vector3f Vector3f::perpInPlane(const EGG::Vector3f &rhs, bool normalise) const {
105 if (Mathf::abs(dot(rhs)) == 1.0f) {
106 return EGG::Vector3f::zero;
107 }
108
109 f32 _x = (rhs.z * x - rhs.x * z) * rhs.z - (rhs.x * y - rhs.y * x) * rhs.y;
110 f32 _y = (rhs.x * y - rhs.y * x) * rhs.x - (rhs.y * z - rhs.z * y) * rhs.z;
111 f32 _z = (rhs.y * z - rhs.z * y) * rhs.y - (rhs.z * x - rhs.x * z) * rhs.x;
112
113 EGG::Vector3f ret(_x, _y, _z);
114
115 if (normalise) {
116 ret.normalise();
117 }
118
119 return ret;
120}
121
123void Vector3f::read(Stream &stream) {
124 x = stream.read_f32();
125 y = stream.read_f32();
126 z = stream.read_f32();
127}
128
129} // namespace EGG
A stream of data, abstracted to allow for continuous seeking.
Definition Stream.hh:10
static f32 fma(f32 x, f32 y, f32 z)
Fused multiply-add operation.
Definition Math.hh:69
f32 frsqrt(f32 x)
Definition Math.cc:315
EGG core library.
Definition Archive.cc:6
void read(Stream &stream)
Initializes a Vector2f by reading 8 bytes from the stream.
Definition Vector.cc:24
A 3D float vector.
Definition Vector.hh:88
f32 normalise()
Normalizes the vector and returns the original length.
Definition Vector.cc:52
f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
Definition Vector.hh:187
f32 length() const
The square root of the vector's dot product.
Definition Vector.hh:192
f32 squaredLength() const
The dot product between the vector and itself.
Definition Vector.hh:182
f32 ps_squareMag() const
Differs from ps_dot due to variation in which operands are fused.
Definition Vector.cc:43
void read(Stream &stream)
Initializes a Vector3f by reading 12 bytes from the stream.
Definition Vector.cc:123
Vector3f perpInPlane(const EGG::Vector3f &rhs, bool normalise) const
Calculates the orthogonal vector, based on the plane defined by this vector and rhs.
Definition Vector.cc:104
Vector3f maximize(const Vector3f &rhs) const
Returns a vector whose elements are the max of the elements of both vectors.
Definition Vector.cc:73
f32 ps_sqDistance(const Vector3f &rhs) const
Paired-singles impl. of sqDistance.
Definition Vector.cc:97
f32 ps_dot() const
Paired-singles dot product implementation.
Definition Vector.cc:30
Vector3f minimize(const Vector3f &rhs) const
Returns a vector whose elements are the min of the elements of both vectors.
Definition Vector.cc:85