载入中...
搜索中...
未找到
PhysicalBalancePose.cpp
浏览该文件的文档.
2#include <cmath>
3#include <functional>
4#include <simplesquirrel/simplesquirrel.hpp>
5#include <string>
12#include "common/Exception.h"
14
15// PhysicalBalancePose.cpp
16namespace eve::animation {
17namespace {
18
19constexpr float kTwoPi = 6.283185307179586f;
20constexpr float kImpulseEps = 1e-8f;
21
22void quatMul(float ax, float ay, float az, float aw, float bx, float by, float bz, float bw, float& ox, float& oy,
23 float& oz, float& ow) {
24 multiplyQuat(ax, ay, az, aw, bx, by, bz, bw, ox, oy, oz, ow);
25}
26
27void quatFromAxisAngle(float ax, float ay, float az, float angle, float& qx, float& qy, float& qz, float& qw) {
28 const float half = 0.5f * angle;
29 const float s = std::sin(half);
30 qx = ax * s;
31 qy = ay * s;
32 qz = az * s;
33 qw = std::cos(half);
34}
35
36void applyParentRotation(TransformTRS& local, float qx, float qy, float qz, float qw) {
37 float ox = 0.f, oy = 0.f, oz = 0.f, ow = 1.f;
38 quatMul(qx, qy, qz, qw, local.qx, local.qy, local.qz, local.qw, ox, oy, oz, ow);
39 local.qx = ox;
40 local.qy = oy;
41 local.qz = oz;
42 local.qw = ow;
43 local.normalizeRotation();
44}
45
46void stepTowardZero(float dt, float omega, float zeta, float& y, float& yd) {
47 if (dt <= 0.f || omega <= 0.f) return;
48 const float kp = omega * omega;
49 const float kd = 2.f * zeta * omega;
50 yd += dt * (-kp * y - kd * yd);
51 y += dt * yd;
52}
53
54} // namespace
55
57 if (!skeleton_) throw Exception("PhysicalBalancePose: skeleton is null");
58 ensureBones();
59 skeleton_->applyBindPose(&pose_);
60 skeleton_->applyBindPose(&target_);
61 hasTarget_ = true;
62 writeOverlayPose();
63}
64
65void PhysicalBalancePose::ensureBones() {
66 const int n = skeleton_->getBoneCount();
67 if (pose_.getBoneCount() != n) pose_.resize(n);
68 if (target_.getBoneCount() != n) target_.resize(n);
69 if (static_cast<int>(masses_.size()) != n) {
70 masses_.assign(static_cast<size_t>(n), 1.f);
71 recoils_.assign(static_cast<size_t>(n), Recoil{});
72 }
73 if (n <= 0) {
74 supportBone_ = 0;
75 balanceBone_ = 0;
76 return;
77 }
78 if (!boneInRange(supportBone_)) supportBone_ = 0;
79 if (!boneInRange(balanceBone_)) balanceBone_ = n > 1 ? 1 : 0;
80}
81
82bool PhysicalBalancePose::boneInRange(int boneIndex) const {
83 return boneIndex >= 0 && boneIndex < skeleton_->getBoneCount();
84}
85
86eve::Result<void> PhysicalBalancePose::requireFinitePositive(const char* name, float value, bool allowZero) {
87 if (!std::isfinite(value) || value < 0.f || (!allowZero && value <= 0.f))
89 eve::DiagnosticCode::InvalidArgument, std::string("physical balance ") + name + " must be finite and " +
90 (allowZero ? "non-negative" : "positive")));
92}
93
95 if (!boneInRange(boneIndex))
97 "physical balance support bone is out of range"));
98 supportBone_ = boneIndex;
100}
101
103 if (!boneInRange(boneIndex))
105 "physical balance balance bone is out of range"));
106 balanceBone_ = boneIndex;
108}
109
111 if (!boneInRange(boneIndex))
113 "physical balance bone mass index is out of range"));
114 auto ok = requireFinitePositive("bone mass", mass, true);
115 if (!ok) return ok;
116 masses_[static_cast<size_t>(boneIndex)] = mass;
118}
119
120float PhysicalBalancePose::getBoneMass(int boneIndex) const {
121 if (!boneInRange(boneIndex)) return 0.f;
122 return masses_[static_cast<size_t>(boneIndex)];
123}
124
125eve::Result<void> PhysicalBalancePose::setRecovery(float frequencyHz, float dampingZeta) {
126 auto freq = requireFinitePositive("recovery frequency", frequencyHz, false);
127 if (!freq) return freq;
128 if (!std::isfinite(dampingZeta) || dampingZeta < 0.f)
130 eve::DiagnosticCode::InvalidArgument, "physical balance recovery damping must be finite and non-negative"));
131 const float omega = kTwoPi * frequencyHz;
132 const float height = std::max(pendulumHeight_, 1e-4f);
133 if (omega * omega <= gravity_ / height)
136 "physical balance recovery frequency is too low to overcome gravity at the pendulum height"));
137 recoveryHz_ = frequencyHz;
138 recoveryZeta_ = dampingZeta;
140}
141
142eve::Result<void> PhysicalBalancePose::setRecoil(float frequencyHz, float dampingZeta) {
143 auto freq = requireFinitePositive("recoil frequency", frequencyHz, false);
144 if (!freq) return freq;
145 if (!std::isfinite(dampingZeta) || dampingZeta < 0.f)
147 eve::DiagnosticCode::InvalidArgument, "physical balance recoil damping must be finite and non-negative"));
148 recoilHz_ = frequencyHz;
149 recoilZeta_ = dampingZeta;
151}
152
154 auto ok = requireFinitePositive("gravity", metersPerSecondSquared, true);
155 if (!ok) return ok;
156 const float omega = kTwoPi * recoveryHz_;
157 const float height = std::max(pendulumHeight_, 1e-4f);
158 if (omega * omega <= metersPerSecondSquared / height)
161 "physical balance gravity would overcome the current recovery frequency"));
162 gravity_ = metersPerSecondSquared;
164}
165
167 auto ok = requireFinitePositive("pendulum height", meters, false);
168 if (!ok) return ok;
169 const float omega = kTwoPi * recoveryHz_;
170 if (omega * omega <= gravity_ / meters)
173 "physical balance pendulum height would make gravity stronger than recovery"));
174 pendulumHeight_ = meters;
176}
177
179 auto ok = requireFinitePositive("inertia", inertia, false);
180 if (!ok) return ok;
181 inertia_ = inertia;
183}
184
186 auto ok = requireFinitePositive("recoil inertia", inertia, false);
187 if (!ok) return ok;
188 recoilInertia_ = inertia;
190}
191
193 auto ok = requireFinitePositive("max lean", radians, false);
194 if (!ok) return ok;
195 maxLean_ = radians;
196 leanX_ = clampf(leanX_, -maxLean_, maxLean_);
197 leanZ_ = clampf(leanZ_, -maxLean_, maxLean_);
199}
200
202 if (!target)
204 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "physical balance target pose is null"));
205 if (target->getBoneCount() != skeleton_->getBoneCount())
208 "physical balance target pose bone count does not match the skeleton"));
209 target_.copyFrom(target);
210 hasTarget_ = true;
212}
213
214void PhysicalBalancePose::resetDynamics() {
215 leanX_ = leanZ_ = 0.f;
216 leanVelX_ = leanVelZ_ = 0.f;
217 pendingLeanTx_ = pendingLeanTz_ = 0.f;
218 for (Recoil& recoil : recoils_) recoil = Recoil{};
219}
220
222 if (!hasTarget_)
224 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "physical balance has no target pose to snap to"));
225 resetDynamics();
226 writeOverlayPose();
228}
229
230eve::Result<void> PhysicalBalancePose::applyImpulse(int boneIndex, float impulseX, float impulseY, float impulseZ,
231 float pointX, float pointY, float pointZ) {
232 if (!boneInRange(boneIndex))
234 "physical balance impulse bone is out of range"));
235 if (!std::isfinite(impulseX) || !std::isfinite(impulseY) || !std::isfinite(impulseZ) || !std::isfinite(pointX) ||
236 !std::isfinite(pointY) || !std::isfinite(pointZ))
238 "physical balance impulse values must be finite"));
239 const float mag2 = impulseX * impulseX + impulseY * impulseY + impulseZ * impulseZ;
241
242 pose_.computeWorld(skeleton_);
243 const TransformTRS& support = pose_.world(supportBone_);
244 const float rx = pointX - support.px;
245 const float ry = pointY - support.py;
246 const float rz = pointZ - support.pz;
247 pendingLeanTx_ += ry * impulseZ - rz * impulseY;
248 pendingLeanTz_ += rx * impulseY - ry * impulseX;
249
250 const TransformTRS& bone = pose_.world(boneIndex);
251 Recoil& rec = recoils_[static_cast<size_t>(boneIndex)];
252 const float brx = pointX - bone.px;
253 const float bry = pointY - bone.py;
254 const float brz = pointZ - bone.pz;
255 rec.pendingTx += bry * impulseZ - brz * impulseY;
256 rec.pendingTy += brz * impulseX - brx * impulseZ;
257 rec.pendingTz += brx * impulseY - bry * impulseX;
259}
260
261void PhysicalBalancePose::writeOverlayPose() {
262 pose_.copyFrom(&target_);
263 const int n = pose_.getBoneCount();
264 for (int i = 0; i < n; ++i) {
265 const Recoil& rec = recoils_[static_cast<size_t>(i)];
266 if (std::fabs(rec.x) < 1e-8f && std::fabs(rec.y) < 1e-8f && std::fabs(rec.z) < 1e-8f) continue;
267 float qx = 0.f, qy = 0.f, qz = 0.f, qw = 1.f;
268 float rx = 0.f, ry = 0.f, rz = 0.f, rw = 1.f;
269 float tx = 0.f, ty = 0.f, tz = 0.f, tw = 1.f;
270 quatFromAxisAngle(1.f, 0.f, 0.f, rec.x, qx, qy, qz, qw);
271 quatFromAxisAngle(0.f, 1.f, 0.f, rec.y, rx, ry, rz, rw);
272 quatMul(rx, ry, rz, rw, qx, qy, qz, qw, tx, ty, tz, tw);
273 quatFromAxisAngle(0.f, 0.f, 1.f, rec.z, qx, qy, qz, qw);
274 float ox = 0.f, oy = 0.f, oz = 0.f, ow = 1.f;
275 quatMul(qx, qy, qz, qw, tx, ty, tz, tw, ox, oy, oz, ow);
276 applyParentRotation(pose_.local(i), ox, oy, oz, ow);
277 }
278 if (boneInRange(balanceBone_) && (std::fabs(leanX_) > 1e-8f || std::fabs(leanZ_) > 1e-8f)) {
279 float qx = 0.f, qy = 0.f, qz = 0.f, qw = 1.f;
280 float rx = 0.f, ry = 0.f, rz = 0.f, rw = 1.f;
281 quatFromAxisAngle(1.f, 0.f, 0.f, leanX_, qx, qy, qz, qw);
282 quatFromAxisAngle(0.f, 0.f, 1.f, leanZ_, rx, ry, rz, rw);
283 float ox = 0.f, oy = 0.f, oz = 0.f, ow = 1.f;
284 quatMul(qx, qy, qz, qw, rx, ry, rz, rw, ox, oy, oz, ow);
285 applyParentRotation(pose_.local(balanceBone_), ox, oy, oz, ow);
286 }
287 refreshDiagnostics();
288}
289
290void PhysicalBalancePose::refreshDiagnostics() {
291 pose_.computeWorld(skeleton_);
292 const int n = pose_.getBoneCount();
293 float massSum = 0.f;
294 comX_ = comY_ = comZ_ = 0.f;
295 for (int i = 0; i < n; ++i) {
296 const float mass = masses_[static_cast<size_t>(i)];
297 if (mass <= 0.f) continue;
298 const TransformTRS& world = pose_.world(i);
299 comX_ += world.px * mass;
300 comY_ += world.py * mass;
301 comZ_ += world.pz * mass;
302 massSum += mass;
303 }
304 if (massSum > 1e-8f) {
305 const float inv = 1.f / massSum;
306 comX_ *= inv;
307 comY_ *= inv;
308 comZ_ *= inv;
309 }
310 if (boneInRange(supportBone_)) {
311 const TransformTRS& support = pose_.world(supportBone_);
312 supportX_ = support.px;
313 supportY_ = support.py;
314 supportZ_ = support.pz;
315 }
316}
317
318void PhysicalBalancePose::updateUnchecked(float dt) {
319 if (dt <= 0.f || !hasTarget_) return;
320 ensureBones();
321
322 leanVelX_ += pendingLeanTx_ / inertia_;
323 leanVelZ_ += pendingLeanTz_ / inertia_;
324 pendingLeanTx_ = pendingLeanTz_ = 0.f;
325
326 const float omega = kTwoPi * recoveryHz_;
327 const float height = std::max(pendulumHeight_, 1e-4f);
328 const float grav = gravity_ / height;
329 const float kp = std::max(omega * omega - grav, 1e-4f);
330 const float kd = 2.f * recoveryZeta_ * omega;
331 leanVelX_ += dt * (-kp * leanX_ - kd * leanVelX_);
332 leanVelZ_ += dt * (-kp * leanZ_ - kd * leanVelZ_);
333 leanX_ += dt * leanVelX_;
334 leanZ_ += dt * leanVelZ_;
335 const float clampedX = clampf(leanX_, -maxLean_, maxLean_);
336 const float clampedZ = clampf(leanZ_, -maxLean_, maxLean_);
337 if (clampedX != leanX_) leanVelX_ = 0.f;
338 if (clampedZ != leanZ_) leanVelZ_ = 0.f;
339 leanX_ = clampedX;
340 leanZ_ = clampedZ;
341
342 const float recoilOmega = kTwoPi * recoilHz_;
343 for (Recoil& rec : recoils_) {
344 rec.vx += rec.pendingTx / recoilInertia_;
345 rec.vy += rec.pendingTy / recoilInertia_;
346 rec.vz += rec.pendingTz / recoilInertia_;
347 rec.pendingTx = rec.pendingTy = rec.pendingTz = 0.f;
348 stepTowardZero(dt, recoilOmega, recoilZeta_, rec.x, rec.vx);
349 stepTowardZero(dt, recoilOmega, recoilZeta_, rec.y, rec.vy);
350 stepTowardZero(dt, recoilOmega, recoilZeta_, rec.z, rec.vz);
351 rec.x = clampf(rec.x, -maxLean_, maxLean_);
352 rec.y = clampf(rec.y, -maxLean_, maxLean_);
353 rec.z = clampf(rec.z, -maxLean_, maxLean_);
354 }
355
356 writeOverlayPose();
357}
358
360 auto seconds = detail::secondsForStep(step, hasLastTick_, lastTick_, "PhysicalBalancePose");
361 if (!seconds) return eve::Result<void>::failure(seconds.status());
362 updateUnchecked(std::move(seconds).takeValue());
363 lastTick_ = step.tick;
364 hasLastTick_ = true;
366}
367
369 auto step = detail::legacyStep(dt, hasLastTick_, lastTick_, "PhysicalBalancePose");
370 if (!step) {
371 step.ignore("legacy PhysicalBalancePose update");
372 return;
373 }
374 advance(std::move(step).takeValue()).ignore("legacy PhysicalBalancePose update");
375}
376
379
380AnimSkeleton* PhysicalBalancePose::getSkeleton() const { return skeleton_; }
381int PhysicalBalancePose::getSupportBone() const { return supportBone_; }
382int PhysicalBalancePose::getBalanceBone() const { return balanceBone_; }
383float PhysicalBalancePose::getRecoveryFrequency() const { return recoveryHz_; }
384float PhysicalBalancePose::getRecoveryDamping() const { return recoveryZeta_; }
385float PhysicalBalancePose::getRecoilFrequency() const { return recoilHz_; }
386float PhysicalBalancePose::getRecoilDamping() const { return recoilZeta_; }
387float PhysicalBalancePose::getGravity() const { return gravity_; }
388float PhysicalBalancePose::getPendulumHeight() const { return pendulumHeight_; }
389float PhysicalBalancePose::getInertia() const { return inertia_; }
390float PhysicalBalancePose::getRecoilInertia() const { return recoilInertia_; }
391float PhysicalBalancePose::getMaxLean() const { return maxLean_; }
392float PhysicalBalancePose::getLeanX() const { return leanX_; }
393float PhysicalBalancePose::getLeanZ() const { return leanZ_; }
394float PhysicalBalancePose::getLeanVelocityX() const { return leanVelX_; }
395float PhysicalBalancePose::getLeanVelocityZ() const { return leanVelZ_; }
396float PhysicalBalancePose::getCenterOfMassX() const { return comX_; }
397float PhysicalBalancePose::getCenterOfMassY() const { return comY_; }
398float PhysicalBalancePose::getCenterOfMassZ() const { return comZ_; }
399float PhysicalBalancePose::getSupportX() const { return supportX_; }
400float PhysicalBalancePose::getSupportY() const { return supportY_; }
401float PhysicalBalancePose::getSupportZ() const { return supportZ_; }
402
403} // namespace eve::animation
404
405// PhysicalBalancePoseBindings.cpp
406namespace eve::animation {
407
408void exposePhysicalBalancePoseBindings(ssq::Table& table) {
409 auto pose = table.addClass<PhysicalBalancePose>(
410 "PhysicalBalancePose", std::function<PhysicalBalancePose*()>([]() -> PhysicalBalancePose* { return nullptr; }),
411 true);
412 pose.addFunc("getSkeleton", &PhysicalBalancePose::getSkeleton);
413 pose.addFunc("setSupportBone", [vm = table.getHandle()](PhysicalBalancePose* self, int bone) {
414 return script::projectResult(vm, self->setSupportBone(bone));
415 });
416 pose.addFunc("getSupportBone", &PhysicalBalancePose::getSupportBone);
417 pose.addFunc("setBalanceBone", [vm = table.getHandle()](PhysicalBalancePose* self, int bone) {
418 return script::projectResult(vm, self->setBalanceBone(bone));
419 });
420 pose.addFunc("getBalanceBone", &PhysicalBalancePose::getBalanceBone);
421 pose.addFunc("setBoneMass", [vm = table.getHandle()](PhysicalBalancePose* self, int bone, float mass) {
422 return script::projectResult(vm, self->setBoneMass(bone, mass));
423 });
424 pose.addFunc("getBoneMass", &PhysicalBalancePose::getBoneMass);
425 pose.addFunc("setRecovery",
426 [vm = table.getHandle()](PhysicalBalancePose* self, float frequencyHz, float dampingZeta) {
427 return script::projectResult(vm, self->setRecovery(frequencyHz, dampingZeta));
428 });
429 pose.addFunc("getRecoveryFrequency", &PhysicalBalancePose::getRecoveryFrequency);
430 pose.addFunc("getRecoveryDamping", &PhysicalBalancePose::getRecoveryDamping);
431 pose.addFunc("setRecoil",
432 [vm = table.getHandle()](PhysicalBalancePose* self, float frequencyHz, float dampingZeta) {
433 return script::projectResult(vm, self->setRecoil(frequencyHz, dampingZeta));
434 });
435 pose.addFunc("getRecoilFrequency", &PhysicalBalancePose::getRecoilFrequency);
436 pose.addFunc("getRecoilDamping", &PhysicalBalancePose::getRecoilDamping);
437 pose.addFunc("setGravity", [vm = table.getHandle()](PhysicalBalancePose* self, float gravity) {
438 return script::projectResult(vm, self->setGravity(gravity));
439 });
440 pose.addFunc("getGravity", &PhysicalBalancePose::getGravity);
441 pose.addFunc("setPendulumHeight", [vm = table.getHandle()](PhysicalBalancePose* self, float height) {
442 return script::projectResult(vm, self->setPendulumHeight(height));
443 });
444 pose.addFunc("getPendulumHeight", &PhysicalBalancePose::getPendulumHeight);
445 pose.addFunc("setInertia", [vm = table.getHandle()](PhysicalBalancePose* self, float inertia) {
446 return script::projectResult(vm, self->setInertia(inertia));
447 });
448 pose.addFunc("getInertia", &PhysicalBalancePose::getInertia);
449 pose.addFunc("setRecoilInertia", [vm = table.getHandle()](PhysicalBalancePose* self, float inertia) {
450 return script::projectResult(vm, self->setRecoilInertia(inertia));
451 });
452 pose.addFunc("getRecoilInertia", &PhysicalBalancePose::getRecoilInertia);
453 pose.addFunc("setMaxLean", [vm = table.getHandle()](PhysicalBalancePose* self, float radians) {
454 return script::projectResult(vm, self->setMaxLean(radians));
455 });
456 pose.addFunc("getMaxLean", &PhysicalBalancePose::getMaxLean);
457 pose.addFunc("setTargetPose", [vm = table.getHandle()](PhysicalBalancePose* self, AnimPose* target) {
458 return script::projectResult(vm, self->setTargetPose(target));
459 });
460 pose.addFunc("snapToTarget", [vm = table.getHandle()](PhysicalBalancePose* self) {
461 return script::projectResult(vm, self->snapToTarget());
462 });
463 pose.addFunc("applyImpulse", [vm = table.getHandle()](PhysicalBalancePose* self, int bone, float ix, float iy,
464 float iz, float px, float py, float pz) {
465 return script::projectResult(vm, self->applyImpulse(bone, ix, iy, iz, px, py, pz));
466 });
467 pose.addFunc("update", &PhysicalBalancePose::update);
468 pose.addFunc("getPose", &PhysicalBalancePose::getPose);
469 pose.addFunc("getTargetPose", &PhysicalBalancePose::getTargetPose);
470 pose.addFunc("getLeanX", &PhysicalBalancePose::getLeanX);
471 pose.addFunc("getLeanZ", &PhysicalBalancePose::getLeanZ);
472 pose.addFunc("getLeanVelocityX", &PhysicalBalancePose::getLeanVelocityX);
473 pose.addFunc("getLeanVelocityZ", &PhysicalBalancePose::getLeanVelocityZ);
474 pose.addFunc("getCenterOfMassX", &PhysicalBalancePose::getCenterOfMassX);
475 pose.addFunc("getCenterOfMassY", &PhysicalBalancePose::getCenterOfMassY);
476 pose.addFunc("getCenterOfMassZ", &PhysicalBalancePose::getCenterOfMassZ);
477 pose.addFunc("getSupportX", &PhysicalBalancePose::getSupportX);
478 pose.addFunc("getSupportY", &PhysicalBalancePose::getSupportY);
479 pose.addFunc("getSupportZ", &PhysicalBalancePose::getSupportZ);
480}
481
482} // namespace eve::animation
double y
LogicalId target
eve::EntitySpatialPose pose
const std::string & s
HSQUIRRELVM vm
Definition ECS.cpp:38
std::string name
Definition ECS.cpp:154
float inertia
ScriptForceVector gravity
glm::vec3 n
Definition Grass.cpp:63
std::vector< Colorf > px
std::uint32_t height
std::string local
std::uint32_t bone
double dt
Object::Ptr value
The single Squirrel projection for common Result, Status and Value.
float step
Definition TreeMesh.cpp:451
double oy
double ox
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
Definition Result.h:571
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
Evaluated local (and optional world) pose for an AnimSkeleton. Script type: AnimPose.
Definition AnimPose.h:17
int getBoneCount() const noexcept
Returns the bone count.
Definition AnimPose.h:36
const TransformTRS & world(int boneIndex) const
World.
Definition AnimPose.cpp:371
TransformTRS & local(int boneIndex)
Local.
Definition AnimPose.cpp:130
void copyFrom(const AnimPose *other)
Copies from.
Definition AnimPose.cpp:95
void computeWorld(const AnimSkeleton *skeleton)
Compute world transforms from local pose + skeleton hierarchy. World values readable via getWorld* af...
Definition AnimPose.cpp:207
void resize(int boneCount)
Resize.
Definition AnimPose.cpp:82
3D bone hierarchy + bind-pose local TRS for skeletal animation. Independent of ik::Skeleton3D (FABRIK...
int getBoneCount() const
Returns the bone count.
void applyBindPose(class AnimPose *pose) const
Fill pose locals with bind pose.
Authored-pose overlay that wobbles from world impulses and recovers balance.
void update(float dt)
Legacy seconds facade; forwards to advance() and ignores its Result.
eve::Result< void > setInertia(float inertia)
Whole-body rotational inertia for lean (kg·m²).
eve::Result< void > setRecoilInertia(float inertia)
Per-bone recoil inertia (kg·m²).
eve::Result< void > advance(const eve::SimulationStep &step)
Advance the pendulum and recoil, then write the overlay pose.
eve::Result< void > snapToTarget()
Zero lean, recoil and pending impulses; pose snaps to the last target.
eve::Result< void > setSupportBone(int boneIndex)
Bone whose world XZ is the support point (typically pelvis/root).
eve::Result< void > setMaxLean(float radians)
Clamp for lean angle in radians.
eve::Result< void > setBalanceBone(int boneIndex)
Bone that receives whole-body lean (typically spine/chest).
eve::Result< void > setPendulumHeight(float meters)
Pendulum length used as g/h (meters).
eve::Result< void > setTargetPose(const AnimPose *target)
Copy the authored pose that recovery tracks.
eve::Result< void > setBoneMass(int boneIndex, float mass)
Mass used for center-of-mass; zero excludes the bone.
eve::Result< void > setRecovery(float frequencyHz, float dampingZeta)
Recovery frequency (Hz) and damping ζ for the balance pendulum.
AnimPose * getPose()
Overlay pose after the last successful advance (or snap). @ownership Borrowed; this object retains ow...
eve::Result< void > applyImpulse(int boneIndex, float impulseX, float impulseY, float impulseZ, float pointX, float pointY, float pointZ)
Queue a world-space linear impulse at a world point.
AnimPose * getTargetPose()
Last authored target, or bind pose before the first setTargetPose. @ownership Borrowed; this object r...
eve::Result< void > setGravity(float metersPerSecondSquared)
Gravity magnitude (m/s², Y-up). Zero disables the fall-away term.
PhysicalBalancePose(AnimSkeleton *skeleton)
Construct for a borrowed skeleton.
eve::Result< void > setRecoil(float frequencyHz, float dampingZeta)
Local-bone recoil frequency and damping toward the authored rotation.
AnimSkeleton * getSkeleton() const
Borrowed skeleton. @ownership Borrowed; ownership remains with the animation source....
eve::Result< eve::SimulationStep > legacyStep(float seconds, bool hasLastTick, eve::SimulationTick lastTick, const char *owner)
Convert a legacy seconds call to the next local scheduler step.
eve::Result< float > secondsForStep(const eve::SimulationStep &step, bool hasLastTick, eve::SimulationTick lastTick, const char *owner)
Validate a scheduler step before an animation object mutates state.
void multiplyQuat(float ax, float ay, float az, float aw, float bx, float by, float bz, float bw, float &ox, float &oy, float &oz, float &ow)
Hamilton product a * b for unit quaternions (xyzw).
Definition AnimMath.h:94
void exposePhysicalBalancePoseBindings(ssq::Table &table)
Register the impulse/balance pose overlay Squirrel surface.
constexpr float clampf(float v, float lo, float hi) noexcept
Clampf.
Definition AnimMath.h:34
One deterministic fixed-step emitted by SimulationClock.
Definition Time.h:158
Local TRS used by skeletal animation (quaternion xyzw).
Definition AnimMath.h:9