Kale
Loading...
Searching...
No Matches
SkeletalAnimatable.hpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
21
22#include <utility>
23#include <vector>
24#include <tuple>
25#include <array>
26#include <mutex>
27#include <algorithm>
28
29namespace Kale {
30
35 struct Skeleton {
36
72
76 std::vector<Bone> bones;
77
81 Skeleton();
82
88 Skeleton(const std::vector<std::tuple<int, float, float>>& bones, AngleUnit unit);
89
94 Skeleton(const std::vector<Bone>& bones);
95
105 static Bone calculateBone(const std::vector<Bone>& bones, int previousBone, float length, float rotation, AngleUnit unit) {
106 Bone bone;
107 bone.previousBone = previousBone;
108 bone.length = length;
109 bone.rotation = rotation;
110 bone.previousToBone = Transform(length, 0.0f, rotation, 1.0f, 1.0f, unit);
111
112 // If the bone is the root
113 if (previousBone == -1) bone.worldToBone = bone.previousToBone;
114 else bone.worldToBone = bone.previousToBone * bones[previousBone].worldToBone;
116 return bone;
117 }
118
127 static Vector2f transform(const std::vector<Transform>& offsets, Vector2f vert, const std::array<std::pair<int, float>, 4>& weights) {
128 Vector2f transformed = Vector2f::zero();
129 for (const std::pair<int, float> weight : weights) {
130 if (weight.first == -1) break;
131 transformed += offsets[weight.first].transform(vert) * weight.second;
132 }
133 return transformed;
134 }
135
136 };
137
142 class SkeletalAnimatable : public Node, public StateAnimatable<Skeleton> {
143 private:
144
148 std::vector<Transform> skeleton;
149
154
160
164 std::mutex mutex;
165
170 void recalculateSkeleton(float deltaTime);
171
172 protected:
173
180 void update(size_t threadNum, const Scene& scene, float deltaTime) override;
181
188 void preUpdate(size_t threadNum, const Scene& scene, float deltaTime) override;
189
190 public:
191
196
202
207 void setBase(const Skeleton& skeleton);
208
215 const std::vector<Transform>& getSkeleton(float deltaTime);
216
223 const std::vector<Transform>& getSkeletonNoRecalc() const;
224
233 Vector2f transform(Vector2f vert, const std::array<std::pair<int, float>, 4>& weights, float deltaTime);
234
243 Vector2f transformNoRecalc(Vector2f vert, const std::array<std::pair<int, float>, 4>& weights) const;
244
245 };
246
247}
std::enable_if< W==2 &&H==2, Matrix< W, H, T > >::type inverse()
Definition Matrix.hpp:476
Vector2f transform(Vector2f vert, const std::array< std::pair< int, float >, 4 > &weights, float deltaTime)
Vector2f transformNoRecalc(Vector2f vert, const std::array< std::pair< int, float >, 4 > &weights) const
void update(size_t threadNum, const Scene &scene, float deltaTime) override
void setBase(const Skeleton &skeleton)
std::vector< Transform > skeleton
void recalculateSkeleton(float deltaTime)
const std::vector< Transform > & getSkeletonNoRecalc() const
void preUpdate(size_t threadNum, const Scene &scene, float deltaTime) override
const std::vector< Transform > & getSkeleton(float deltaTime)
static Vector2< float > zero()
Definition Vector.hpp:172
AngleUnit
Definition Utils.hpp:30
static Vector2f transform(const std::vector< Transform > &offsets, Vector2f vert, const std::array< std::pair< int, float >, 4 > &weights)
std::vector< Bone > bones
static Bone calculateBone(const std::vector< Bone > &bones, int previousBone, float length, float rotation, AngleUnit unit)