A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
KartPhysics.cc
1#include "KartPhysics.hh"
2
3#include <egg/math/Quat.hh>
4
5namespace Kart {
6
8KartPhysics::KartPhysics(bool isBike) {
9 m_pose = EGG::Matrix34f::ident;
10 m_dynamics = isBike ? new KartDynamicsBike : new KartDynamics;
11 m_hitboxGroup = new CollisionGroup;
12 m_fc = 50.0f; // set immediately after in KartPhysics::Create()
13}
14
16KartPhysics::~KartPhysics() {
17 delete m_dynamics;
18 delete m_hitboxGroup;
19}
20
22void KartPhysics::reset() {
23 m_dynamics->init();
24 m_hitboxGroup->reset();
25 m_decayingStuntRot = EGG::Quatf::ident;
26 m_instantaneousStuntRot = EGG::Quatf::ident;
27 m_specialRot = EGG::Quatf::ident;
28 m_decayingExtraRot = EGG::Quatf::ident;
29 m_instantaneousExtraRot = EGG::Quatf::ident;
30 m_extraRot = EGG::Quatf::ident;
31 m_movingObjVel.setZero();
32 m_movingRoadVel.setZero();
33 m_pose = EGG::Matrix34f::ident;
34 m_xAxis = EGG::Vector3f(m_pose[0, 0], m_pose[1, 0], m_pose[2, 0]);
35 m_yAxis = EGG::Vector3f(m_pose[0, 1], m_pose[1, 1], m_pose[2, 1]);
36 m_zAxis = EGG::Vector3f(m_pose[0, 2], m_pose[1, 2], m_pose[2, 2]);
37 m_pos = m_dynamics->pos();
38 m_velocity = m_dynamics->velocity();
39}
40
44 m_pose.makeQT(m_dynamics->fullRot(), m_dynamics->pos());
45 m_xAxis = EGG::Vector3f(m_pose[0, 0], m_pose[1, 0], m_pose[2, 0]);
46 m_yAxis = EGG::Vector3f(m_pose[0, 1], m_pose[1, 1], m_pose[2, 1]);
47 m_zAxis = EGG::Vector3f(m_pose[0, 2], m_pose[1, 2], m_pose[2, 2]);
48}
49
55void KartPhysics::calc(f32 dt, f32 maxSpeed, const EGG::Vector3f &scale, bool air) {
56 m_specialRot = m_instantaneousStuntRot * m_decayingStuntRot;
57 m_extraRot = m_instantaneousExtraRot * m_decayingExtraRot;
58
59 m_dynamics->setSpecialRot(m_specialRot);
60 m_dynamics->setExtraRot(m_extraRot);
61 m_dynamics->setScale(scale);
62
63 m_dynamics->calc(dt, maxSpeed, air);
64
65 m_decayingStuntRot = m_decayingStuntRot.slerpTo(EGG::Quatf::ident, 0.1f);
66 m_decayingExtraRot = m_decayingExtraRot.slerpTo(EGG::Quatf::ident, 0.1f);
67
68 m_instantaneousStuntRot = EGG::Quatf::ident;
69 m_instantaneousExtraRot = EGG::Quatf::ident;
70}
71
73void KartPhysics::shiftDecayMovingRoadVel(const EGG::Vector3f &v, f32 maxPullSpeed) {
74 m_movingRoadVel += v;
75
76 if (m_movingRoadVel.squaredLength() > std::numeric_limits<f32>::epsilon()) {
77 f32 speed = std::min(maxPullSpeed, m_movingRoadVel.normalise());
78 m_movingRoadVel *= speed;
79 m_dynamics->setMovingRoadVel(m_movingRoadVel);
80 }
81}
82
84KartPhysics *KartPhysics::Create(const KartParam &param) {
85 KartPhysics *physics = new KartPhysics(param.isBike());
86
87 const BSP &bsp = param.bsp();
88
89 physics->set_fc(physics->hitboxGroup()->initHitboxes(bsp.hitboxes));
90
91 physics->dynamics()->setBspParams(bsp.angVel0Factor, bsp.cuboids[0], bsp.cuboids[1], false);
92
93 return physics;
94}
95
96} // namespace Kart
void makeQT(const Quatf &q, const Vector3f &t)
Sets matrix from rotation and position.
Definition Matrix.cc:11
void calc(f32 dt, f32 maxSpeed, bool air)
Every frame, computes acceleration, velocity, position and rotation of the kart.
void updatePose()
Constructs a transformation matrix from rotation and position.
void calc(f32 dt, f32 maxSpeed, const EGG::Vector3f &scale, bool air)
Computes trick rotation and calls to KartDynamics::calc().
EGG::Quatf m_decayingExtraRot
Rotation that occurs when landing from a trick.
EGG::Vector3f m_zAxis
The third column of the pose.
EGG::Vector3f m_xAxis
The first column of the pose.
EGG::Vector3f m_velocity
Copied from KartDynamics.
EGG::Vector3f m_yAxis
The second column of the pose.
EGG::Matrix34f m_pose
The kart's current rotation and position.
Pertains to kart-related functionality.
Quatf slerpTo(const Quatf &q2, f32 t) const
Performs spherical linear interpolation.
Definition Quat.cc:84
A 3D float vector.
Definition Vector.hh:88
f32 normalise()
Normalizes the vector and returns the original length.
Definition Vector.cc:52
f32 squaredLength() const
The dot product between the vector and itself.
Definition Vector.hh:182