Many small fixes to combat behaviour, navigation and perfomance.
This commit is contained in:
parent
858d247893
commit
f673f5cd0a
26 changed files with 1447 additions and 1330 deletions
|
|
@ -14,67 +14,71 @@
|
|||
CR_NAMESPACE_BEGIN
|
||||
|
||||
// 3dmath vector
|
||||
class Vector final {
|
||||
template <typename T> class Vec3D {
|
||||
public:
|
||||
float x = 0.0f, y = 0.0f, z = 0.0f;
|
||||
T x = 0.0f, y = 0.0f, z = 0.0f;
|
||||
|
||||
public:
|
||||
Vector (const float scaler = 0.0f) : x (scaler), y (scaler), z (scaler)
|
||||
Vec3D (const T &scaler = 0.0f) : x (scaler), y (scaler), z (scaler)
|
||||
{ }
|
||||
|
||||
explicit Vector (const float _x, const float _y, const float _z) : x (_x), y (_y), z (_z)
|
||||
Vec3D (const T &x, const T &y, const T &z) : x (x), y (y), z (z)
|
||||
{ }
|
||||
|
||||
Vector (float *rhs) : x (rhs[0]), y (rhs[1]), z (rhs[2])
|
||||
Vec3D (T *rhs) : x (rhs[0]), y (rhs[1]), z (rhs[2])
|
||||
{ }
|
||||
|
||||
Vector (const Vector &) = default;
|
||||
Vec3D (const Vec3D &) = default;
|
||||
|
||||
Vec3D (decltype (nullptr)) {
|
||||
clear ();
|
||||
}
|
||||
|
||||
public:
|
||||
operator float *() {
|
||||
operator T * () {
|
||||
return &x;
|
||||
}
|
||||
|
||||
operator const float * () const {
|
||||
operator const T * () const {
|
||||
return &x;
|
||||
}
|
||||
|
||||
Vector operator + (const Vector &rhs) const {
|
||||
return Vector (x + rhs.x, y + rhs.y, z + rhs.z);
|
||||
Vec3D operator + (const Vec3D &rhs) const {
|
||||
return { x + rhs.x, y + rhs.y, z + rhs.z };
|
||||
}
|
||||
|
||||
Vector operator - (const Vector &rhs) const {
|
||||
return Vector (x - rhs.x, y - rhs.y, z - rhs.z);
|
||||
Vec3D operator - (const Vec3D &rhs) const {
|
||||
return { x - rhs.x, y - rhs.y, z - rhs.z };
|
||||
}
|
||||
|
||||
Vector operator - () const {
|
||||
return Vector (-x, -y, -z);
|
||||
Vec3D operator - () const {
|
||||
return { -x, -y, -z };
|
||||
}
|
||||
|
||||
friend Vector operator * (const float scale, const Vector &rhs) {
|
||||
return Vector (rhs.x * scale, rhs.y * scale, rhs.z * scale);
|
||||
friend Vec3D operator * (const T &scale, const Vec3D &rhs) {
|
||||
return { rhs.x * scale, rhs.y * scale, rhs.z * scale };
|
||||
}
|
||||
|
||||
Vector operator * (const float scale) const {
|
||||
return Vector (scale * x, scale * y, scale * z);
|
||||
Vec3D operator * (const T &scale) const {
|
||||
return { scale * x, scale * y, scale * z };
|
||||
}
|
||||
|
||||
Vector operator / (const float div) const {
|
||||
const float inv = 1 / div;
|
||||
return Vector (inv * x, inv * y, inv * z);
|
||||
Vec3D operator / (const T &rhs) const {
|
||||
const auto inv = 1 / (rhs + kFloatEqualEpsilon);
|
||||
return { inv * x, inv * y, inv * z };
|
||||
}
|
||||
|
||||
// cross product
|
||||
Vector operator ^ (const Vector &rhs) const {
|
||||
return Vector (y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
|
||||
Vec3D operator ^ (const Vec3D &rhs) const {
|
||||
return { y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x };
|
||||
}
|
||||
|
||||
// dot product
|
||||
float operator | (const Vector &rhs) const {
|
||||
T operator | (const Vec3D &rhs) const {
|
||||
return x * rhs.x + y * rhs.y + z * rhs.z;
|
||||
}
|
||||
|
||||
const Vector &operator += (const Vector &rhs) {
|
||||
const Vec3D &operator += (const Vec3D &rhs) {
|
||||
x += rhs.x;
|
||||
y += rhs.y;
|
||||
z += rhs.z;
|
||||
|
|
@ -82,24 +86,24 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
const Vector &operator -= (const Vector &right) {
|
||||
x -= right.x;
|
||||
y -= right.y;
|
||||
z -= right.z;
|
||||
const Vec3D &operator -= (const Vec3D &rhs) {
|
||||
x -= rhs.x;
|
||||
y -= rhs.y;
|
||||
z -= rhs.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const Vector &operator *= (float scale) {
|
||||
x *= scale;
|
||||
y *= scale;
|
||||
z *= scale;
|
||||
const Vec3D &operator *= (const T &rhs) {
|
||||
x *= rhs;
|
||||
y *= rhs;
|
||||
z *= rhs;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const Vector &operator /= (float div) {
|
||||
const float inv = 1 / div;
|
||||
const Vec3D &operator /= (const T &rhs) {
|
||||
const auto inv = 1 / (rhs + kFloatEqualEpsilon);
|
||||
|
||||
x *= inv;
|
||||
y *= inv;
|
||||
|
|
@ -108,67 +112,66 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
bool operator == (const Vector &rhs) const {
|
||||
bool operator == (const Vec3D &rhs) const {
|
||||
return cr::fequal (x, rhs.x) && cr::fequal (y, rhs.y) && cr::fequal (z, rhs.z);
|
||||
}
|
||||
|
||||
bool operator != (const Vector &rhs) const {
|
||||
return !cr::fequal (x, rhs.x) && !cr::fequal (y, rhs.y) && !cr::fequal (z, rhs.z);
|
||||
bool operator != (const Vec3D &rhs) const {
|
||||
return !operator == (rhs);
|
||||
}
|
||||
|
||||
Vector &operator = (const Vector &) = default;
|
||||
void operator = (decltype (nullptr)) {
|
||||
clear ();
|
||||
}
|
||||
|
||||
Vec3D &operator = (const Vec3D &) = default;
|
||||
|
||||
public:
|
||||
float length () const {
|
||||
T length () const {
|
||||
return cr::sqrtf (lengthSq ());
|
||||
}
|
||||
|
||||
float length2d () const {
|
||||
T length2d () const {
|
||||
return cr::sqrtf (x * x + y * y);
|
||||
}
|
||||
|
||||
float lengthSq () const {
|
||||
T lengthSq () const {
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
Vector get2d () const {
|
||||
return Vector (x, y, 0.0f);
|
||||
Vec3D get2d () const {
|
||||
return { x, y, 0.0f };
|
||||
}
|
||||
|
||||
Vector normalize () const {
|
||||
float len = length () + cr::kFloatCmpEpsilon;
|
||||
Vec3D normalize () const {
|
||||
auto len = length () + cr::kFloatCmpEpsilon;
|
||||
|
||||
if (cr::fzero (len)) {
|
||||
return Vector (0.0f, 0.0f, 1.0f);
|
||||
return { 0.0f, 0.0f, 1.0f };
|
||||
}
|
||||
len = 1.0f / len;
|
||||
return Vector (x * len, y * len, z * len);
|
||||
return { x * len, y * len, z * len };
|
||||
}
|
||||
|
||||
Vector normalize2d () const {
|
||||
float len = length2d () + cr::kFloatCmpEpsilon;
|
||||
Vec3D normalize2d () const {
|
||||
auto len = length2d () + cr::kFloatCmpEpsilon;
|
||||
|
||||
if (cr::fzero (len)) {
|
||||
return Vector (0.0f, 1.0f, 0.0f);
|
||||
return { 0.0f, 1.0f, 0.0f };
|
||||
}
|
||||
len = 1.0f / len;
|
||||
return Vector (x * len, y * len, 0.0f);
|
||||
return { x * len, y * len, 0.0f };
|
||||
}
|
||||
|
||||
bool empty () const {
|
||||
return cr::fzero (x) && cr::fzero (y) && cr::fzero (z);
|
||||
}
|
||||
|
||||
static const Vector &null () {
|
||||
static const Vector &s_null {};
|
||||
return s_null;
|
||||
}
|
||||
|
||||
void clear () {
|
||||
x = y = z = 0.0f;
|
||||
}
|
||||
|
||||
Vector clampAngles () {
|
||||
Vec3D clampAngles () {
|
||||
x = cr::normalizeAngles (x);
|
||||
y = cr::normalizeAngles (y);
|
||||
z = 0.0f;
|
||||
|
|
@ -176,78 +179,84 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
float pitch () const {
|
||||
T pitch () const {
|
||||
if (cr::fzero (x) && cr::fzero (y)) {
|
||||
return 0.0f;
|
||||
}
|
||||
return cr::degreesToRadians (cr::atan2f (z, length2d ()));
|
||||
}
|
||||
|
||||
float yaw () const {
|
||||
T yaw () const {
|
||||
if (cr::fzero (x) && cr::fzero (y)) {
|
||||
return 0.0f;
|
||||
}
|
||||
return cr::radiansToDegrees (cr:: atan2f (y, x));
|
||||
}
|
||||
|
||||
Vector angles () const {
|
||||
Vec3D angles () const {
|
||||
if (cr::fzero (x) && cr::fzero (y)) {
|
||||
return Vector (z > 0.0f ? 90.0f : 270.0f, 0.0, 0.0f);
|
||||
return { z > 0.0f ? 90.0f : 270.0f, 0.0, 0.0f };
|
||||
}
|
||||
return Vector (cr::radiansToDegrees (cr::atan2f (z, length2d ())), cr::radiansToDegrees (cr::atan2f (y, x)), 0.0f);
|
||||
return { cr::radiansToDegrees (cr::atan2f (z, length2d ())), cr::radiansToDegrees (cr::atan2f (y, x)), 0.0f };
|
||||
}
|
||||
|
||||
void buildVectors (Vector *forward, Vector *right, Vector *upward) const {
|
||||
void angleVectors (Vec3D *forward, Vec3D *right, Vec3D *upward) const {
|
||||
enum { pitch, yaw, roll, unused, max };
|
||||
|
||||
float sines[max] = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||
float cosines[max] = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||
T sines[max] = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||
T cosines[max] = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||
|
||||
// compute the sine and cosine compontents
|
||||
cr::sincosf (cr::degreesToRadians (x), cr::degreesToRadians (y), cr::degreesToRadians (z), sines, cosines);
|
||||
|
||||
if (forward) {
|
||||
forward->x = cosines[pitch] * cosines[yaw];
|
||||
forward->y = cosines[pitch] * sines[yaw];
|
||||
forward->z = -sines[pitch];
|
||||
*forward = {
|
||||
cosines[pitch] * cosines[yaw],
|
||||
cosines[pitch] * sines[yaw],
|
||||
-sines[pitch]
|
||||
};
|
||||
}
|
||||
|
||||
if (right) {
|
||||
right->x = -sines[roll] * sines[pitch] * cosines[yaw] + cosines[roll] * sines[yaw];
|
||||
right->y = -sines[roll] * sines[pitch] * sines[yaw] - cosines[roll] * cosines[yaw];
|
||||
right->z = -sines[roll] * cosines[pitch];
|
||||
*right = {
|
||||
-sines[roll] * sines[pitch] * cosines[yaw] + cosines[roll] * sines[yaw],
|
||||
-sines[roll] * sines[pitch] * sines[yaw] - cosines[roll] * cosines[yaw],
|
||||
-sines[roll] * cosines[pitch]
|
||||
};
|
||||
}
|
||||
|
||||
if (upward) {
|
||||
upward->x = cosines[roll] * sines[pitch] * cosines[yaw] + sines[roll] * sines[yaw];
|
||||
upward->y = cosines[roll] * sines[pitch] * sines[yaw] - sines[roll] * cosines[yaw];
|
||||
upward->z = cosines[roll] * cosines[pitch];
|
||||
*upward = {
|
||||
cosines[roll] * sines[pitch] * cosines[yaw] + sines[roll] * sines[yaw],
|
||||
upward->y = cosines[roll] * sines[pitch] * sines[yaw] - sines[roll] * cosines[yaw],
|
||||
upward->z = cosines[roll] * cosines[pitch]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const Vector &forward () {
|
||||
static Vector s_fwd {};
|
||||
buildVectors (&s_fwd, nullptr, nullptr);
|
||||
const Vec3D &forward () {
|
||||
static Vec3D s_fwd {};
|
||||
angleVectors (&s_fwd, nullptr, nullptr);
|
||||
|
||||
return s_fwd;
|
||||
}
|
||||
|
||||
const Vector &upward () {
|
||||
static Vector s_up {};
|
||||
buildVectors (nullptr, nullptr, &s_up);
|
||||
const Vec3D &upward () {
|
||||
static Vec3D s_up {};
|
||||
angleVectors (nullptr, nullptr, &s_up);
|
||||
|
||||
return s_up;
|
||||
}
|
||||
|
||||
const Vector &right () {
|
||||
static Vector s_right {};
|
||||
buildVectors (nullptr, &s_right, nullptr);
|
||||
const Vec3D &right () {
|
||||
static Vec3D s_right {};
|
||||
angleVectors (nullptr, &s_right, nullptr);
|
||||
|
||||
return s_right;
|
||||
}
|
||||
};
|
||||
|
||||
// expose global null vector
|
||||
static auto &nullvec = Vector::null ();
|
||||
// default is float
|
||||
using Vector = Vec3D <float>;
|
||||
|
||||
CR_NAMESPACE_END
|
||||
Loading…
Add table
Add a link
Reference in a new issue