Skip to content

Commit

Permalink
Finish initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
malleoz authored and vabold committed Jan 30, 2024
1 parent ce9f2b3 commit 2697310
Show file tree
Hide file tree
Showing 38 changed files with 1,731 additions and 78 deletions.
55 changes: 55 additions & 0 deletions source/egg/math/BoundBox.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "BoundBox.hh"

#include "egg/math/Math.hh"

namespace EGG {

BoundBox2f::BoundBox2f() {
resetBound();
}

BoundBox2f::~BoundBox2f() = default;

void BoundBox2f::resetBound() {
mMin.set(std::numeric_limits<f32>::max());
mMax.set(-std::numeric_limits<f32>::max());
}

void BoundBox2f::setDirect(const Vector2f &min, const Vector2f &max) {
mMax = max;
mMin = min;
}

void BoundBox2f::setMin(const Vector2f &min) {
mMin = min;
}

void BoundBox2f::setMax(const Vector2f &max) {
mMax = max;
}

BoundBox3f::BoundBox3f() {
resetBound();
}

BoundBox3f::~BoundBox3f() = default;

void BoundBox3f::resetBound() {
mMin.set(std::numeric_limits<f32>::max());
mMax.set(-std::numeric_limits<f32>::max());
}

void BoundBox3f::setDirect(const Vector3f &min, const Vector3f &max) {
mMax = max;
mMin = min;
}

void BoundBox3f::setMin(const Vector3f &min) {
mMin = min;
}

void BoundBox3f::setMax(const Vector3f &max) {
mMax = max;
}

} // namespace EGG
33 changes: 33 additions & 0 deletions source/egg/math/BoundBox.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "egg/math/Vector.hh"

namespace EGG {

struct BoundBox2f {
BoundBox2f();
~BoundBox2f();

void resetBound();
void setDirect(const Vector2f &max, const Vector2f &min);
void setMin(const Vector2f &min);
void setMax(const Vector2f &max);

Vector2f mMin;
Vector2f mMax;
};

struct BoundBox3f {
BoundBox3f();
~BoundBox3f();

void resetBound();
void setDirect(const Vector3f &max, const Vector3f &min);
void setMin(const Vector3f &min);
void setMax(const Vector3f &max);

Vector3f mMin;
Vector3f mMax;
};

} // namespace EGG
51 changes: 51 additions & 0 deletions source/egg/math/Matrix.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "Matrix.hh"

namespace EGG {

Matrix34f::Matrix34f() {
makeZero();
}

Matrix34f::Matrix34f(f32 _e00, f32 _e01, f32 _e02, f32 _e03, f32 _e10, f32 _e11, f32 _e12, f32 _e13,
f32 _e20, f32 _e21, f32 _e22, f32 _e23) {
mtx[0][0] = _e00;
mtx[0][1] = _e01;
mtx[0][2] = _e02;
mtx[0][3] = _e03;
mtx[1][0] = _e10;
mtx[1][1] = _e11;
mtx[1][2] = _e12;
mtx[1][3] = _e13;
mtx[2][0] = _e20;
mtx[2][1] = _e21;
mtx[2][2] = _e22;
mtx[2][3] = _e23;
}

Matrix34f::~Matrix34f() = default;

void Matrix34f::makeQT(const Quatf &q, const Vector3f &t) {
mtx[0][0] = 1.0f - 2.0f * (q.v.y * q.v.y + q.v.z * q.v.z);
mtx[0][1] = 2.0f * (q.v.x * q.v.y - q.w * q.v.z);
mtx[0][2] = 2.0f * (q.v.x * q.v.z + q.w * q.v.y);
mtx[0][3] = t.x;
mtx[1][0] = 2.0f * (q.v.x * q.v.y + q.w * q.v.y);
mtx[1][1] = 1.0 - 2.0f * (q.v.x * q.v.x + q.v.z * q.v.z);
mtx[1][2] = 2.0f * (q.v.y * q.v.z - q.w * q.v.x);
mtx[1][3] = t.y;
mtx[2][0] = 2.0f * (q.v.x * q.v.z - q.w * q.v.y);
mtx[2][1] = 2.0f * (q.v.y * q.v.z + q.w * q.v.x);
mtx[2][2] = 1.0 - 2.0f * (q.v.x * q.v.x + q.v.y * q.v.y);
mtx[2][3] = t.z;
}

void Matrix34f::makeZero() {
for (auto &n : a) {
n = 0.0f;
}
}

const Matrix34f Matrix34f::ident(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f);

} // namespace EGG
31 changes: 31 additions & 0 deletions source/egg/math/Matrix.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "egg/math/Quat.hh"

namespace EGG {

class Matrix34f {
public:
Matrix34f();
Matrix34f(f32 _e00, f32 _e01, f32 _e02, f32 _e03, f32 _e10, f32 _e11, f32 _e12, f32 _e13,
f32 _e20, f32 _e21, f32 _e22, f32 _e23);
~Matrix34f();

f32 &operator()(int i, int j) {
return mtx[i][j];
}

// Q for Quaternion, T for translation
void makeQT(const Quatf &q, const Vector3f &t);
void makeZero();

static const Matrix34f ident;

private:
union {
std::array<std::array<f32, 4>, 3> mtx;
std::array<f32, 12> a;
};
};

} // namespace EGG
7 changes: 7 additions & 0 deletions source/egg/math/Quat.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ struct Quatf {
Quatf(f32 w_, const Vector3f &v_);
~Quatf();

Quatf &operator=(const Quatf &q) {
w = q.w;
v = q.v;

return *this;
}

Quatf operator*(const Vector3f &vec) const {
Vector3f cross = v.cross(vec);
Vector3f scale = vec * w;
Expand Down
37 changes: 33 additions & 4 deletions source/egg/math/Vector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,22 @@ Vector3f::Vector3f() = default;

Vector3f::~Vector3f() = default;

Vector3f Vector3f::cross(const Vector3f &rhs) const {
return Vector3f(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
f32 Vector3f::dot() const {
return x * x + y * y + z * z;
}

f32 Vector3f::dot(const Vector3f &rhs) const {
return x * rhs.x + y * rhs.y + z * rhs.z;
}

f32 Vector3f::dot() const {
return x * x + y * y + z * z;
f32 Vector3f::ps_dot(const Vector3f &rhs) const {
f32 y_ = y * rhs.y;
f32 xy = static_cast<f32>(static_cast<f64>(x) * static_cast<f64>(rhs.x) + static_cast<f64>(y_));
return xy + z * rhs.z;
}

Vector3f Vector3f::cross(const Vector3f &rhs) const {
return Vector3f(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
}

f32 Vector3f::length() const {
Expand All @@ -66,6 +72,26 @@ f32 Vector3f::normalise() {
return len;
}

Vector3f Vector3f::maximize(const Vector3f &rhs) const {
Vector3f out;

out.x = x > rhs.x ? x : rhs.x;
out.y = y > rhs.y ? y : rhs.y;
out.z = z > rhs.z ? z : rhs.z;

return out;
}

Vector3f Vector3f::minimize(const Vector3f &rhs) const {
Vector3f out;

out.x = x < rhs.x ? x : rhs.x;
out.y = y < rhs.y ? y : rhs.y;
out.z = z < rhs.z ? z : rhs.z;

return out;
}

void Vector3f::read(Stream &stream) {
x = stream.read_f32();
y = stream.read_f32();
Expand All @@ -81,4 +107,7 @@ const Vector3f Vector3f::ex = Vector3f(1.0f, 0.0f, 0.0f);
const Vector3f Vector3f::ey = Vector3f(0.0f, 1.0f, 0.0f);
const Vector3f Vector3f::ez = Vector3f(0.0f, 0.0f, 1.0f);

const Vector3f Vector3f::inf = Vector3f(std::numeric_limits<f32>::infinity(),
std::numeric_limits<f32>::infinity(), std::numeric_limits<f32>::infinity());

} // namespace EGG
32 changes: 30 additions & 2 deletions source/egg/math/Vector.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ struct Vector2f {
Vector2f();
~Vector2f();

inline void set(f32 val) {
x = y = val;
}

Vector2f operator-() const {
return Vector2f(-x, -y);
}
Expand Down Expand Up @@ -43,6 +47,14 @@ struct Vector3f {
Vector3f();
~Vector3f();

inline void setZero() {
set(0.0f);
}

inline void set(f32 val) {
x = y = z = val;
}

Vector3f operator-() const {
return Vector3f(-x, -y, -z);
}
Expand All @@ -55,15 +67,30 @@ struct Vector3f {
return Vector3f(x + rhs.x, y + rhs.y, z + rhs.z);
}

Vector3f operator*(float scalar) const {
Vector3f &operator+=(const Vector3f &rhs) {
return *this = *this + rhs;
}

Vector3f operator*(f32 scalar) const {
return Vector3f(x * scalar, y * scalar, z * scalar);
}

Vector3f &operator*=(f32 scalar) {
return *this = *this * scalar;
}

Vector3f operator/(f32 scalar) const {
return Vector3f(x / scalar, y / scalar, z / scalar);
}

Vector3f cross(const EGG::Vector3f &rhs) const;
f32 dot(const EGG::Vector3f &rhs) const;
f32 dot() const;
f32 dot(const EGG::Vector3f &rhs) const;
f32 ps_dot(const EGG::Vector3f &rhs) const;
f32 length() const;
f32 normalise();
Vector3f maximize(const Vector3f &rhs) const;
Vector3f minimize(const Vector3f &rhs) const;

void read(Stream &stream);

Expand All @@ -73,6 +100,7 @@ struct Vector3f {

static const Vector3f zero;
static const Vector3f ex, ey, ez;
static const Vector3f inf;
};

} // namespace EGG
80 changes: 80 additions & 0 deletions source/game/field/CollisionDirector.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "CollisionDirector.hh"

namespace Field {

bool CollisionDirector::checkSphereFullPush(f32 radius, const EGG::Vector3f &v0,
const EGG::Vector3f &v1, KCLTypeMask flags, CourseColMgr::CollisionInfo *pInfo,
KCLTypeMask *pFlagsOut, u32 /*param_8*/) {
if (pInfo) {
pInfo->m_bbox.mMin = EGG::Vector3f::zero;
pInfo->m_bbox.mMax = EGG::Vector3f::zero;
pInfo->_50 = -std::numeric_limits<f32>::min();
pInfo->m_wallDist = -std::numeric_limits<f32>::min();
pInfo->m_floorDist = -std::numeric_limits<f32>::min();
pInfo->m_perpendicularity = 0.0f;
}

if (pFlagsOut) {
resetCollisionEntries(pFlagsOut);
}

bool colliding = false;

if (flags &&
CourseColMgr::Instance()->checkSphereFullPush(1.0f, radius, nullptr, v0, v1, flags,
pInfo, pFlagsOut)) {
colliding = true;
}

if (colliding && pInfo) {
pInfo->m_minPlusMax = pInfo->m_bbox.mMin + pInfo->m_bbox.mMax;
}

return colliding;
}

void CollisionDirector::resetCollisionEntries(KCLTypeMask *ptr) {
*ptr = 0;
m_collisionEntryCount = 0;
m_closestCollisionEntry = nullptr;
}

void CollisionDirector::pushCollisionEntry(f32 dist, KCLTypeMask *typeMask, KCLTypeMask kclTypeBit,
u16 attribute) {
*typeMask = *typeMask | kclTypeBit;
if (m_collisionEntryCount >= m_entries.size()) {
m_collisionEntryCount = m_entries.size() - 1;
}

m_entries[m_collisionEntryCount++] = CollisionEntry(kclTypeBit, attribute, dist);
}

CollisionDirector *CollisionDirector::CreateInstance() {
assert(!s_instance);
s_instance = new CollisionDirector;
return s_instance;
}

CollisionDirector *CollisionDirector::Instance() {
return s_instance;
}

void CollisionDirector::DestroyInstance() {
assert(s_instance);
delete s_instance;
s_instance = nullptr;
}

CollisionDirector::CollisionDirector() {
m_collisionEntryCount = 0;
m_closestCollisionEntry = nullptr;
CourseColMgr::CreateInstance()->init();
}

CollisionDirector::~CollisionDirector() {
CourseColMgr::DestroyInstance();
}

CollisionDirector *CollisionDirector::s_instance = nullptr;

} // namespace Field
Loading

0 comments on commit 2697310

Please sign in to comment.