1#include "KartDynamics.hh"
3#include <egg/math/Math.hh>
8KartDynamics::KartDynamics() {
16KartDynamics::~KartDynamics() =
default;
22 if (EGG::Mathf::abs(top.
dot(
m_top)) >= 0.9999f) {
32void KartDynamics::init() {
33 m_pos = EGG::Vector3f::zero;
51 m_top = EGG::Vector3f::ey;
56 m_top_ = EGG::Vector3f::ey;
59 m_scale = EGG::Vector3f::unit;
64 constexpr f32 TWELFTH = 1.0f / 12.0f;
67 m_inertiaTensor[0, 0] = TWELFTH * (m.y * m.y + m.z * m.z) + (n.y * n.y + n.z * n.z);
68 m_inertiaTensor[1, 1] = TWELFTH * (m.z * m.z + m.x * m.x) + (n.z * n.z + n.x * n.x);
69 m_inertiaTensor[2, 2] = TWELFTH * (m.x * m.x + m.y * m.y) + (n.x * n.x + n.y * n.y);
92 constexpr f32 TERMINAL_Y_VEL = 120.0f;
110 playerBackHoriz.y = 0.0f;
112 if (playerBackHoriz.
squaredLength() > std::numeric_limits<f32>::epsilon()) {
114 const auto [proj, rej] =
m_extVel.projAndRej(playerBackHoriz);
119 norm = norm > std::numeric_limits<f32>::epsilon() ? EGG::Mathf::sqrt(norm) : 0.0f;
121 m_speedFix = norm * playerBack.
dot(playerBackHoriz);
122 if (speedBack.
dot(playerBackHoriz) < 0.0f) {
123 m_speedFix = -m_speedFix;
133 if (m_scale.z > 1.0f) {
134 maxSpeed *= m_scale.z;
154 if (angVelSum.
squaredLength() > std::numeric_limits<f32>::epsilon()) {
183void KartDynamics::reset() {
230KartDynamicsBike::KartDynamicsBike() =
default;
233KartDynamicsBike::~KartDynamicsBike() =
default;
236void KartDynamicsBike::forceUpright() {
250 if (EGG::Mathf::abs(top.
dot(local_4c)) >= 0.9999f) {
constexpr void inverseTo33(Matrix34f &out) const
Inverts the 3x3 portion of the 3x4 matrix.
constexpr Vector3f multVector33(const Vector3f &vec) const
Multiplies a 3x3 matrix by a vector.
void stabilize() override
Stabilizes the bike by rotating towards the y-axis unit vector.
void setBspParams(f32 rotSpeed, const EGG::Vector3f &m, const EGG::Vector3f &n, bool skipInertia)
On init, takes elements from the kart's BSP and computes the moment of inertia tensor.
EGG::Matrix34f m_inertiaTensor
Resistance to rotational change, as a 3x3 matrix.
void applySuspensionWrench(const EGG::Vector3f &p, const EGG::Vector3f &Flinear, const EGG::Vector3f &Frot, bool ignoreX)
Every frame, computes torque from linear motion and rotation.
f32 m_angVel0YFactor
Scalar for damping angular velocity.
EGG::Quatf m_extraRot
[Unused]
virtual void stabilize()
Stabilizes the kart by rotating towards the y-axis unit vector.
bool m_noGravity
Disables gravity. Relevant when respawning.
EGG::Quatf m_mainRot
Rotation based on the angular velocities.
EGG::Vector3f m_extVel
Velocity induced by collisions.
void applyWrenchScaled(const EGG::Vector3f &p, const EGG::Vector3f &f, f32 scale)
Applies a force linearly and rotationally to the kart.
void calc(f32 dt, f32 maxSpeed, bool air)
Every frame, computes acceleration, velocity, position and rotation of the kart.
EGG::Vector3f m_angVel1
[Unused]
EGG::Vector3f m_velocity
Sum of the linear velocities.
EGG::Vector3f m_intVel
What you typically consider to be the vehicle's speed.
f32 m_angVel0Factor
Scalar for damping angular velocity.
EGG::Vector3f m_totalForce
Basically just gravity.
EGG::Quatf m_fullRot
The combination of the other rotations.
EGG::Vector3f m_movingObjVel
Velocity from things like TF conveyers.
f32 m_speedNorm
Min of the max speed and m_velocity magnitude.
bool m_killExtVelY
Caps external velocity at 0.
EGG::Vector3f m_totalTorque
Torque from linear motion and rotation.
EGG::Vector3f m_angVel0
Angular velocity from m_totalTorque.
EGG::Vector3f m_movingRoadVel
Velocity from Koopa Cape water.
EGG::Vector3f m_top_
Basically m_top biased towards absolute up.
EGG::Vector3f m_top
The unit vector pointing up from the vehicle.
EGG::Vector3f m_angVel2
The main component of angular velocity.
f32 m_stabilizationFactor
Scalar for damping the main rotation.
f32 m_gravity
Usually -1.3f, also affected by KartMove::calcDive.
bool m_forceUpright
Specifies if we should return the vehicle to upwards orientation.
EGG::Vector3f m_pos
The vehicle's position.
EGG::Vector3f m_acceleration
Basically just m_totalForce.
EGG::Quatf m_specialRot
Rotation from trick animations. Copied from KartPhysics.
EGG::Matrix34f m_invInertiaTensor
The inverse of m_inertiaTensor.
Pertains to kart-related functionality.
A quaternion, used to represent 3D rotation.
constexpr Vector3f rotateVector(const Vector3f &vec) const
Rotates a vector based on the quat.
constexpr void normalise()
Scales the quaternion to a unit length.
constexpr void makeVectorRotation(const Vector3f &from, const Vector3f &to)
Captures rotation between two vectors.
constexpr f32 squaredNorm() const
Computes .
constexpr Quatf slerpTo(const Quatf &q1, f32 t) const
Performs spherical linear interpolation.
constexpr Vector3f rotateVectorInv(const Vector3f &vec) const
Rotates a vector on the inverse quat.
constexpr f32 normalise()
Normalizes the vector and returns the original length.
constexpr f32 squaredLength() const
The dot product between the vector and itself.
constexpr f32 dot(const Vector3f &rhs) const
The dot product between two vectors.