Skip to content

Commit

Permalink
Sync boost and wheelie
Browse files Browse the repository at this point in the history
  • Loading branch information
vabold committed Apr 9, 2024
1 parent 20e03bd commit b5698e2
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 68 deletions.
72 changes: 72 additions & 0 deletions source/game/kart/KartBoost.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "KartBoost.hh"

namespace Kart {

KartBoost::KartBoost() : m_timers{0}, m_active{false} {
m_multiplier = 1.0f;
m_acceleration = 1.0f;
m_speedLimit = -1.0f;
}

KartBoost::~KartBoost() = default;

bool KartBoost::activate(Type type, s16 frames) {
bool activated = false;

size_t t = static_cast<size_t>(type);
if (!m_active[t] || m_timers[t] < frames) {
m_timers[t] = frames;
activated = true;
m_active[t] = true;
}

return activated;
}

bool KartBoost::calc() {
static constexpr std::array<f32, BOOST_TYPE_COUNT> MULTIPLIERS = {{
0.2f,
}};

static constexpr std::array<f32, BOOST_TYPE_COUNT> ACCELERATIONS = {{
3.0f,
}};

static constexpr std::array<f32, BOOST_TYPE_COUNT> LIMITS = {{
-1.0f,
}};

m_multiplier = 1.0f;
m_acceleration = 1.0f;
m_speedLimit = -1.0f;

for (size_t i = 0; i < BOOST_TYPE_COUNT; ++i) {
if (!m_active[i]) {
continue;
}

m_multiplier = 1.0f + MULTIPLIERS[i];
m_acceleration = ACCELERATIONS[i];
m_speedLimit = LIMITS[i];

if (--m_timers[i] <= 0) {
m_active[i] = false;
}
}

return m_multiplier > 1.0f || m_speedLimit > 0.0f;
}

f32 KartBoost::multiplier() const {
return m_multiplier;
}

f32 KartBoost::acceleration() const {
return m_acceleration;
}

f32 KartBoost::speedLimit() const {
return m_speedLimit;
}

} // namespace Kart
35 changes: 35 additions & 0 deletions source/game/kart/KartBoost.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <Common.hh>

namespace Kart {

class KartBoost {
public:
enum class Type {
AllMt,
Max,
};

KartBoost();
~KartBoost();

bool activate(Type type, s16 frames);
bool calc();

f32 multiplier() const;
f32 acceleration() const;
f32 speedLimit() const;

private:
// We need to evaluate this expression early for the array initialization
static constexpr size_t BOOST_TYPE_COUNT = static_cast<size_t>(Type::Max);

std::array<s16, BOOST_TYPE_COUNT> m_timers;
std::array<bool, BOOST_TYPE_COUNT> m_active;
f32 m_multiplier;
f32 m_acceleration;
f32 m_speedLimit;
};

} // namespace Kart
28 changes: 26 additions & 2 deletions source/game/kart/KartDynamics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void KartDynamics::init() {
m_noGravity = false;
m_stabilizationFactor = 0.1f;
m_top_ = EGG::Vector3f::ey;
m_speedFix = 0.0f;
m_angVel0YFactor = 0.0f;
}

Expand Down Expand Up @@ -53,12 +54,27 @@ void KartDynamics::calc(f32 dt, f32 maxSpeed, bool /*air*/) {
m_extVel *= 0.998f;
m_angVel0 *= 0.98f;

EGG::Vector3f playerBackHoriz = m_mainRot.rotateVector(EGG::Vector3f::ez);
EGG::Vector3f playerBack = m_mainRot.rotateVector(EGG::Vector3f::ez);
EGG::Vector3f playerBackHoriz = playerBack;
playerBackHoriz.y = 0.0f;

if (FLT_EPSILON < playerBackHoriz.dot()) {
playerBackHoriz.normalise();
m_extVel = m_extVel.rej(playerBackHoriz);
const auto projAndRej = m_extVel.projAndRej(playerBackHoriz);
const EGG::Vector3f &speedBack = projAndRej.first;
m_extVel = projAndRej.second;

f32 norm = speedBack.dot();
if (FLT_EPSILON < norm) {
norm = EGG::Mathf::sqrt(norm);
} else {
norm = 0.0f;
}

m_speedFix = norm * playerBack.dot(playerBackHoriz);
if (speedBack.dot(playerBackHoriz) < 0.0f) {
m_speedFix = -m_speedFix;
}
}

m_velocity = m_extVel * dt + m_intVel + m_movingObjVel + m_movingRoadVel;
Expand Down Expand Up @@ -157,6 +173,10 @@ const EGG::Vector3f &KartDynamics::angVel2() const {
return m_angVel2;
}

f32 KartDynamics::speedFix() const {
return m_speedFix;
}

void KartDynamics::setPos(const EGG::Vector3f &pos) {
m_pos = pos;
}
Expand All @@ -181,6 +201,10 @@ void KartDynamics::setExtraRot(const EGG::Quatf &q) {
m_extraRot = q;
}

void KartDynamics::setIntVel(const EGG::Vector3f &v) {
m_intVel = v;
}

void KartDynamics::setExtVel(const EGG::Vector3f &v) {
m_extVel = v;
}
Expand Down
3 changes: 3 additions & 0 deletions source/game/kart/KartDynamics.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public:
const EGG::Vector3f &extVel() const;
const EGG::Vector3f &angVel0() const;
const EGG::Vector3f &angVel2() const;
f32 speedFix() const;

void setPos(const EGG::Vector3f &pos);
void setGravity(f32 gravity);
void setMainRot(const EGG::Quatf &q);
void setFullRot(const EGG::Quatf &q);
void setSpecialRot(const EGG::Quatf &q);
void setExtraRot(const EGG::Quatf &q);
void setIntVel(const EGG::Vector3f &v);
void setExtVel(const EGG::Vector3f &v);
void setAngVel0(const EGG::Vector3f &v);
void setAngVel2(const EGG::Vector3f &v);
Expand Down Expand Up @@ -66,6 +68,7 @@ protected:
f32 m_gravity;
EGG::Vector3f m_intVel;
f32 m_stabilizationFactor;
f32 m_speedFix;
EGG::Vector3f m_top_;
f32 m_angVel0YFactor;
bool m_forceUpright;
Expand Down
Loading

0 comments on commit b5698e2

Please sign in to comment.