nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
moves.cc
Go to the documentation of this file.
1#include "client/moves.h"
2
3#include <raymath.h>
4
5#include <cstring>
6#include <vector>
7
8#include "content/skills.h"
9
10namespace nanommo {
11
12namespace {
13
14// The key's turns, by joint, in degrees: none for a joint it leaves alone.
15std::array<Vector3, kJointBones.size()> Angles(const skills::Key& key) {
16 std::array<Vector3, kJointBones.size()> angles{};
17 for (const skills::Turn& turn : key.turns()) {
18 if (turn.joint() >= 0 && turn.joint() < std::ssize(angles)) {
19 angles[turn.joint()] = {turn.pitch(), turn.yaw(), turn.roll()};
20 }
21 }
22 return angles;
23}
24
25Matrix ToMatrix(const Transform& t) {
26 return MatrixMultiply(
27 MatrixMultiply(MatrixScale(t.scale.x, t.scale.y, t.scale.z),
28 QuaternionToMatrix(t.rotation)),
29 MatrixTranslate(t.translation.x, t.translation.y, t.translation.z));
30}
31
32} // namespace
33
34Joints FindJoints(const Model& model) {
35 Joints joints;
36 joints.fill(-1);
37 for (std::size_t joint = 0; joint < kJointBones.size(); ++joint) {
38 for (int bone = 0; bone < static_cast<int>(model.skeleton.boneCount);
39 ++bone) {
40 if (std::strcmp(model.skeleton.bones[bone].name, kJointBones[joint]) ==
41 0) {
42 joints[joint] = bone;
43 }
44 }
45 }
46 return joints;
47}
48
49Bend BendAt(const skills::Move& move, float ms) {
50 // Before the first key, it's the clip's own pose: a key with no turns.
51 skills::Key from;
52 skills::Key to;
53 float t = 1.0f;
54 for (const skills::Key& key : move.keys()) {
55 to = key;
56 if (ms < key.at_ms()) {
57 const float span = static_cast<float>(key.at_ms() - from.at_ms());
58 t = span > 0.0f ? Eased(key.ease(), (ms - from.at_ms()) / span) : 1.0f;
59 break;
60 }
61 from = key;
62 }
63 const auto before = Angles(from);
64 const auto after = Angles(to);
65 Bend bend;
66 for (std::size_t joint = 0; joint < bend.turns.size(); ++joint) {
67 const Vector3 angle =
68 Vector3Scale(Vector3Lerp(before[joint], after[joint], t), DEG2RAD);
69 bend.turns[joint] = QuaternionFromEuler(angle.x, angle.y, angle.z);
70 }
71 bend.shift = {0.0f, Lerp(from.lift(), to.lift(), t),
72 Lerp(from.ahead(), to.ahead(), t)};
73 return bend;
74}
75
76void Turn(const Bend& bend, const Joints& joints, float metre,
77 const ModelSkeleton& skeleton, Transform* pose) {
78 const int count = static_cast<int>(skeleton.boneCount);
79 std::vector<Quaternion> turn(count, QuaternionIdentity());
80 for (std::size_t joint = 0; joint < joints.size(); ++joint) {
81 if (joints[joint] >= 0) {
82 turn[joints[joint]] = bend.turns[joint];
83 }
84 }
85 const std::vector<Transform> clip(pose, pose + count);
86 // Each bone comes after the one it hangs from, so by the time we get to a
87 // bone, its parent's in place, and turned by every joint above it.
88 std::vector<Quaternion> turned(count, QuaternionIdentity());
89 for (int bone = 0; bone < count; ++bone) {
90 const int parent = skeleton.bones[bone].parent;
91 if (parent < 0) {
92 pose[bone].translation =
93 Vector3Add(clip[bone].translation, Vector3Scale(bend.shift, metre));
94 turned[bone] = turn[bone];
95 } else {
96 const Vector3 reach =
97 Vector3Subtract(clip[bone].translation, clip[parent].translation);
98 pose[bone].translation =
99 Vector3Add(pose[parent].translation,
100 Vector3RotateByQuaternion(reach, turned[parent]));
101 turned[bone] = QuaternionMultiply(turned[parent], turn[bone]);
102 }
103 pose[bone].rotation = QuaternionMultiply(turned[bone], clip[bone].rotation);
104 }
105}
106
107void Apply(const Bend& bend, const Joints& joints, float metre, Model& model) {
108 Turn(bend, joints, metre, model.skeleton, model.currentPose);
109 for (int bone = 0; bone < static_cast<int>(model.skeleton.boneCount);
110 ++bone) {
111 // What the GPU skins with: from where the bone was bound to where it is.
112 model.boneMatrices[bone] =
113 MatrixMultiply(MatrixInvert(ToMatrix(model.skeleton.bindPose[bone])),
114 ToMatrix(model.currentPose[bone]));
115 }
116}
117
118} // namespace nanommo
Definition chat.cc:9
void Turn(const Bend &bend, const Joints &joints, float metre, const ModelSkeleton &skeleton, Transform *pose)
Definition moves.cc:76
void Apply(const Bend &bend, const Joints &joints, float metre, Model &model)
Definition moves.cc:107
float Eased(skills::Ease ease, float t)
Definition skills.cc:104
constexpr std::array< const char *, 13 > kJointBones
Each joint's bone in the rig, in the order of the joints.
Definition moves.h:13
Joints FindJoints(const Model &model)
Definition moves.cc:34
Bend BendAt(const skills::Move &move, float ms)
The move, ms into it, between the keys either side.
Definition moves.cc:49
std::array< int, kJointBones.size()> Joints
Where a model's bones are, by joint, or -1 for one it doesn't have.
Definition moves.h:19
std::array< Quaternion, kJointBones.size()> turns
Definition moves.h:24
Vector3 shift
Definition moves.h:25