13 : frequencyHz_(frequencyHz), dampingZeta_(dampingZeta), response_(
response) {
18 frequencyHz_ = frequencyHz;
23 dampingZeta_ = dampingZeta;
33 if (
kind ==
"secondOrder") {
34 integrator_ = Integrator::SecondOrder;
35 }
else if (
kind ==
"spring") {
36 integrator_ = Integrator::Spring;
37 }
else if (
kind ==
"pd") {
38 integrator_ = Integrator::Pd;
40 throw Exception(
"ControlAnim::setIntegrator: unknown kind '%s' (expected secondOrder|spring|pd)",
kind.c_str());
45 switch (integrator_) {
46 case Integrator::SecondOrder:
return "secondOrder";
47 case Integrator::Spring:
return "spring";
48 case Integrator::Pd:
return "pd";
53void ControlAnim::refreshCoeffs() {
58ControlAnim::Channel &ControlAnim::ensure(
const std::string &
name) {
59 auto it = channels_.find(
name);
60 if (it != channels_.end())
return it->second;
62 channels_.emplace(
name, ch);
63 order_.push_back(
name);
64 return channels_[
name];
68 Channel &ch = ensure(
name);
79 Channel &ch = ensure(
name);
90 Channel &ch = ensure(
name);
96 Channel &ch = ensure(
name);
97 ch.yd += deltaVelocity;
103 auto it = channels_.find(
name);
104 return it == channels_.end() ? 0.f : it->second.y;
108 auto it = channels_.find(
name);
109 return it == channels_.end() ? 0.f : it->second.yd;
113 auto it = channels_.find(
name);
114 return it == channels_.end() ? 0.f : it->second.x;
123 channels_.erase(
name);
124 order_.erase(std::remove(order_.begin(), order_.end(),
name), order_.end());
128 if (index < 0 || index >=
static_cast<int>(order_.size()))
return {};
129 return order_[
static_cast<size_t>(
index)];
132void ControlAnim::updateUnchecked(
float dt) {
133 if (
dt <= 0.f)
return;
134 const float omega =
hzToOmega(frequencyHz_);
136 for (
auto &kv : channels_) {
137 Channel &ch = kv.second;
141 }
else if (ch.hasPrevX &&
dt > 1e-8f) {
142 xd = (ch.x - ch.xp) /
dt;
147 switch (integrator_) {
148 case Integrator::SecondOrder:
stepSecondOrder(
dt, ch.x, xd, coeffs_, ch.y, ch.yd);
break;
149 case Integrator::Spring:
stepDampedSpring(
dt, ch.x, omega, dampingZeta_, ch.y, ch.yd);
break;
150 case Integrator::Pd:
stepPd(
dt, ch.x, xd, kp_, kd_, ch.y, ch.yd);
break;
158 updateUnchecked(std::move(seconds).takeValue());
159 lastTick_ =
step.tick;
167 step.ignore(
"legacy ControlAnim update");
182 if (skeleton_ &&
n > 0) {
185 readTargetIntoState(&target_,
true);
187 writePoseFromState();
192 frequencyHz_ = frequencyHz;
197 dampingZeta_ = dampingZeta;
207 if (
kind ==
"secondOrder") {
208 integrator_ = Integrator::SecondOrder;
209 }
else if (
kind ==
"spring") {
210 integrator_ = Integrator::Spring;
211 }
else if (
kind ==
"pd") {
212 integrator_ = Integrator::Pd;
214 throw Exception(
"ControlPose::setIntegrator: unknown kind '%s' (expected secondOrder|spring|pd)",
kind.c_str());
219 switch (integrator_) {
220 case Integrator::SecondOrder:
return "secondOrder";
221 case Integrator::Spring:
return "spring";
222 case Integrator::Pd:
return "pd";
224 return "secondOrder";
227void ControlPose::refreshCoeffs() {
232void ControlPose::ensureBones(
int count) {
237 if (
static_cast<int>(bones_.size()) !=
count) {
238 bones_.resize(
static_cast<size_t>(
count));
243 if (boneIndex < 0 || boneIndex >=
static_cast<int>(bones_.size()))
return;
248 if (boneIndex < 0 || boneIndex >=
static_cast<int>(bones_.size()))
return 0.f;
249 return bones_[
static_cast<size_t>(boneIndex)].
weight;
252void ControlPose::readTargetIntoState(
const AnimPose *
target,
bool snap) {
254 const int n =
target->getBoneCount();
256 for (
int i = 0; i <
n; ++i) {
258 BoneState &
b = bones_[
static_cast<size_t>(i)];
260 auto assign = [snap](ScalarState &
s,
float value) {
262 if (!
s.hasPrev || snap) {
275 float tqx =
t.qx, tqy =
t.qy, tqz =
t.qz, tqw =
t.qw;
277 const float dot =
b.qx.y * tqx +
b.qy.y * tqy +
b.qz.y * tqz +
b.qw.y * tqw;
298 throw Exception(
"ControlPose::setTargetPose: target is null");
301 readTargetIntoState(&target_,
false);
303 writePoseFromState();
307 if (!hasTarget_)
return;
308 readTargetIntoState(&target_,
true);
309 writePoseFromState();
313 writePoseFromState();
319void ControlPose::stepScalar(
float dt,
float xd, ScalarState &
s) {
320 const float omega =
hzToOmega(frequencyHz_);
321 switch (integrator_) {
324 case Integrator::Pd:
stepPd(
dt,
s.x, xd, kp_, kd_,
s.y,
s.yd);
break;
328void ControlPose::writePoseFromState() {
329 const int n =
static_cast<int>(bones_.size());
331 for (
int i = 0; i <
n; ++i) {
332 const BoneState &
b = bones_[
static_cast<size_t>(i)];
344 trs.normalizeRotation();
346 if (
b.weight < 1.f - 1e-6f) {
348 TransformTRS hard = target_.
local(i);
350 const float dot = trs.qx * hard.qx + trs.qy * hard.qy + trs.qz * hard.qz + trs.qw * hard.qw;
359 pose_.
local(i) = trs;
363void ControlPose::updateUnchecked(
float dt) {
364 if (
dt <= 0.f || !hasTarget_)
return;
369 for (
int i = 0; i <
n; ++i) {
370 const TransformTRS &
t = target_.
local(i);
371 BoneState &
b = bones_[
static_cast<size_t>(i)];
373 auto prep = [](ScalarState &
s,
float value) {
387 float tqx =
t.qx, tqy =
t.qy, tqz =
t.qz, tqw =
t.qw;
388 const float dot =
b.qx.y * tqx +
b.qy.y * tqy +
b.qz.y * tqz +
b.qw.y * tqw;
404 auto advance = [&](ScalarState &
s) {
406 if (
s.hasPrev &&
dt > 1e-8f) xd = (
s.x -
s.xp) /
dt;
408 stepScalar(
dt, xd,
s);
411 if (
b.weight <= 1e-6f) {
413 auto hard = [](ScalarState &
s) {
438 float len = std::sqrt(
b.qx.y *
b.qx.y +
b.qy.y *
b.qy.y +
b.qz.y *
b.qz.y +
b.qw.y *
b.qw.y);
440 const float inv = 1.f / len;
457 writePoseFromState();
463 updateUnchecked(std::move(seconds).takeValue());
464 lastTick_ =
step.tick;
472 step.ignore(
"legacy ControlPose update");
EVENGINE_API_FOUNDATION public API.
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Evaluated local (and optional world) pose for an AnimSkeleton. Script type: AnimPose.
int getBoneCount() const noexcept
Returns the bone count.
TransformTRS & local(int boneIndex)
Local.
void copyFrom(const AnimPose *other)
Copies from.
void resize(int boneCount)
Resize.
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.
float getVelocity(const std::string &name) const
Returns the velocity.
void remove(const std::string &name)
Removes .
ControlAnim(float frequencyHz=3.f, float dampingZeta=1.f, float response=1.f)
Control anim.
void set(const std::string &name, float value)
Create/update a channel: current value snaps to value, velocity cleared.
void setTarget(const std::string &name, float value)
Move the tracking target (does not snap state). Creates channel if missing.
eve::Result< void > advance(const eve::SimulationStep &step)
Advance all channels by one scheduler-owned deterministic step.
float get(const std::string &name) const
Returns the value.
void setDamping(float dampingZeta)
Sets the damping.
void setFrequency(float frequencyHz)
Sets the frequency.
bool has(const std::string &name) const
True when active.
float getTarget(const std::string &name) const
Returns the target.
void setResponse(float response)
Sets the response.
std::string getIntegrator() const
Returns the integrator.
std::string getPropertyName(int index) const
Returns the property name.
void setIntegrator(const std::string &kind)
Integrator: "secondOrder" | "spring" | "pd". Invalid → exception.
void update(float dt)
Legacy seconds facade; explicitly forwards to advance().
void setTargetVelocity(const std::string &name, float velocity)
Optional explicit target velocity (used by secondOrder / pd).
void impulse(const std::string &name, float deltaVelocity)
Inject an instantaneous velocity impulse (procedural “hit”).
void setBoneWeight(int boneIndex, float weight)
Per-bone blend weight in [0,1]; 1 = full dynamics, 0 = hard snap to target.
void setDamping(float dampingZeta)
Sets the damping.
float getBoneWeight(int boneIndex) const
Returns the bone weight.
void update(float dt)
Legacy seconds facade; explicitly forwards to advance().
void setTargetPose(const AnimPose *target)
Copy target pose. Channels without prior state snap; existing state keeps momentum.
AnimPose * getPose()
Returns the pose.
void snapToTarget()
Snap current state to the last target without changing the target.
ControlPose(AnimSkeleton *skeleton)
Control pose.
std::string getIntegrator() const
Returns the integrator.
void setResponse(float response)
Sets the response.
void setFrequency(float frequencyHz)
Sets the frequency.
eve::Result< void > advance(const eve::SimulationStep &step)
Advance pose dynamics by one scheduler-owned deterministic step.
AnimPose * getTargetPose()
Returns the target pose.
void setIntegrator(const std::string &kind)
Sets the integrator.
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.
TransformTRS blendTRS(const TransformTRS &a, const TransformTRS &b, float t)
Blend trs.
void stepPd(float dt, float target, float targetVel, float kp, float kd, float &y, float &yd)
Semi-implicit Euler PD step (unit mass): a = Kp (target − y) + Kd (targetVel − yd),...
void stepSecondOrder(float dt, float x, float xd, const SecondOrderCoeffs &c, float &y, float &yd)
Stable semi-implicit Euler step for a second-order tracker. Estimates input velocity from consecutive...
void pdGainsFromOmegaZeta(float omega, float zeta, float &kp, float &kd)
Unit-mass PD gains from natural frequency ω (rad/s) and damping ratio ζ.
constexpr float clampf(float v, float lo, float hi) noexcept
Clampf.
SecondOrderCoeffs makeSecondOrderCoeffs(float frequencyHz, float dampingZeta, float response)
Build second-order coefficients from frequency (Hz), damping ζ, response r.
float hzToOmega(float frequencyHz)
ω (rad/s) from frequency in Hz.
void stepDampedSpring(float dt, float target, float angularFrequency, float dampingRatio, float &pos, float &vel)
Advance position/velocity relative to a set-point using closed-form spring step.
double dot(ForceVector a, ForceVector b)
One deterministic fixed-step emitted by SimulationClock.