[libgdx] Fixed transform constraint mixing, AnimationState javadocs.

This commit is contained in:
Nathan Sweet 2025-04-03 10:56:42 -04:00
parent ba2c394da7
commit a1326bc6dd
3 changed files with 37 additions and 31 deletions

View File

@ -613,7 +613,7 @@ public class AnimationState {
return addAnimation(trackIndex, animation, loop, delay); return addAnimation(trackIndex, animation, loop, delay);
} }
/** Adds an animation to be played after the current or last queued animation for a track. If the track is empty, it is /** Adds an animation to be played after the current or last queued animation for a track. If the track has no entries, this is
* equivalent to calling {@link #setAnimation(int, Animation, boolean)}. * equivalent to calling {@link #setAnimation(int, Animation, boolean)}.
* @param delay If > 0, sets {@link TrackEntry#getDelay()}. If <= 0, the delay set is the duration of the previous track entry * @param delay If > 0, sets {@link TrackEntry#getDelay()}. If <= 0, the delay set is the duration of the previous track entry
* minus any mix duration (from the {@link AnimationStateData}) plus the specified <code>delay</code> (ie the mix * minus any mix duration (from the {@link AnimationStateData}) plus the specified <code>delay</code> (ie the mix
@ -659,8 +659,10 @@ public class AnimationState {
* {@link #addAnimation(int, Animation, boolean, float)} with the desired delay (an empty animation has a duration of 0) and on * {@link #addAnimation(int, Animation, boolean, float)} with the desired delay (an empty animation has a duration of 0) and on
* the returned track entry, set the {@link TrackEntry#setMixDuration(float)}. Mixing from an empty animation causes the new * the returned track entry, set the {@link TrackEntry#setMixDuration(float)}. Mixing from an empty animation causes the new
* animation to be applied more and more over the mix duration. Properties keyed in the new animation transition from the value * animation to be applied more and more over the mix duration. Properties keyed in the new animation transition from the value
* from lower tracks or from the setup pose value if no lower tracks key the property to the value keyed in the new * from lower tracks or from the setup pose value if no lower tracks key the property to the value keyed in the new animation.
* animation. */ * <p>
* See <a href='https://esotericsoftware.com/spine-applying-animations/#Empty-animations'>Empty animations</a> in the Spine
* Runtimes Guide. */
public TrackEntry setEmptyAnimation (int trackIndex, float mixDuration) { public TrackEntry setEmptyAnimation (int trackIndex, float mixDuration) {
TrackEntry entry = setAnimation(trackIndex, emptyAnimation, false); TrackEntry entry = setAnimation(trackIndex, emptyAnimation, false);
entry.mixDuration = mixDuration; entry.mixDuration = mixDuration;
@ -669,10 +671,12 @@ public class AnimationState {
} }
/** Adds an empty animation to be played after the current or last queued animation for a track, and sets the track entry's /** Adds an empty animation to be played after the current or last queued animation for a track, and sets the track entry's
* {@link TrackEntry#getMixDuration()}. If the track is empty, it is equivalent to calling * {@link TrackEntry#getMixDuration()}. If the track has no entries, it is equivalent to calling
* {@link #setEmptyAnimation(int, float)}. * {@link #setEmptyAnimation(int, float)}.
* <p> * <p>
* See {@link #setEmptyAnimation(int, float)}. * See {@link #setEmptyAnimation(int, float)} and
* <a href='https://esotericsoftware.com/spine-applying-animations/#Empty-animations'>Empty animations</a> in the Spine
* Runtimes Guide.
* @param delay If > 0, sets {@link TrackEntry#getDelay()}. If <= 0, the delay set is the duration of the previous track entry * @param delay If > 0, sets {@link TrackEntry#getDelay()}. If <= 0, the delay set is the duration of the previous track entry
* minus any mix duration plus the specified <code>delay</code> (ie the mix ends at (<code>delay</code> = 0) or * minus any mix duration plus the specified <code>delay</code> (ie the mix ends at (<code>delay</code> = 0) or
* before (<code>delay</code> < 0) the previous track entry duration). If the previous entry is looping, its next * before (<code>delay</code> < 0) the previous track entry duration). If the previous entry is looping, its next
@ -687,8 +691,10 @@ public class AnimationState {
return entry; return entry;
} }
/** Sets an empty animation for every track, discarding any queued animations, and mixes to it over the specified mix /** Sets an empty animation for every track, discarding any queued animations, and mixes to it over the specified mix duration.
* duration. */ * <p>
* See <a href='https://esotericsoftware.com/spine-applying-animations/#Empty-animations'>Empty animations</a> in the Spine
* Runtimes Guide. */
public void setEmptyAnimations (float mixDuration) { public void setEmptyAnimations (float mixDuration) {
boolean oldDrainDisabled = queue.drainDisabled; boolean oldDrainDisabled = queue.drainDisabled;
queue.drainDisabled = true; queue.drainDisabled = true;

View File

@ -118,7 +118,7 @@ public class TransformConstraint implements Updatable {
else else
clamped = clamp(clamped, to.max, to.offset); clamped = clamp(clamped, to.max, to.offset);
} }
to.apply(data, bone, clamped, localTarget, additive); to.apply(this, bone, clamped, localTarget, additive);
} }
} }
} }

View File

@ -202,7 +202,7 @@ public class TransformConstraintData extends ConstraintData {
abstract public float mix (TransformConstraint constraint); abstract public float mix (TransformConstraint constraint);
/** Applies the value to this property. */ /** Applies the value to this property. */
abstract public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive); abstract public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean additive);
} }
static public class FromRotate extends FromProperty { static public class FromRotate extends FromProperty {
@ -216,10 +216,10 @@ public class TransformConstraintData extends ConstraintData {
return constraint.mixRotate; return constraint.mixRotate;
} }
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) { public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean additive) {
if (local) { if (local) {
if (!additive) value -= bone.arotation; if (!additive) value -= bone.arotation;
bone.arotation += value * data.mixRotate; bone.arotation += value * constraint.mixRotate;
} else { } else {
float a = bone.a, b = bone.b, c = bone.c, d = bone.d; float a = bone.a, b = bone.b, c = bone.c, d = bone.d;
value *= degRad; value *= degRad;
@ -228,7 +228,7 @@ public class TransformConstraintData extends ConstraintData {
value -= PI2; value -= PI2;
else if (value < -PI) // else if (value < -PI) //
value += PI2; value += PI2;
value *= data.mixRotate; value *= constraint.mixRotate;
float cos = cos(value), sin = sin(value); float cos = cos(value), sin = sin(value);
bone.a = cos * a - sin * c; bone.a = cos * a - sin * c;
bone.b = cos * b - sin * d; bone.b = cos * b - sin * d;
@ -249,13 +249,13 @@ public class TransformConstraintData extends ConstraintData {
return constraint.mixX; return constraint.mixX;
} }
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) { public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean additive) {
if (local) { if (local) {
if (!additive) value -= bone.ax; if (!additive) value -= bone.ax;
bone.ax += value * data.mixX; bone.ax += value * constraint.mixX;
} else { } else {
if (!additive) value -= bone.worldX; if (!additive) value -= bone.worldX;
bone.worldX += value * data.mixX; bone.worldX += value * constraint.mixX;
} }
} }
} }
@ -271,13 +271,13 @@ public class TransformConstraintData extends ConstraintData {
return constraint.mixY; return constraint.mixY;
} }
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) { public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean additive) {
if (local) { if (local) {
if (!additive) value -= bone.ay; if (!additive) value -= bone.ay;
bone.ay += value * data.mixY; bone.ay += value * constraint.mixY;
} else { } else {
if (!additive) value -= bone.worldY; if (!additive) value -= bone.worldY;
bone.worldY += value * data.mixY; bone.worldY += value * constraint.mixY;
} }
} }
} }
@ -293,19 +293,19 @@ public class TransformConstraintData extends ConstraintData {
return constraint.mixScaleX; return constraint.mixScaleX;
} }
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) { public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean additive) {
if (local) { if (local) {
if (additive) if (additive)
bone.ascaleX *= 1 + ((value - 1) * data.mixScaleX); bone.ascaleX *= 1 + ((value - 1) * constraint.mixScaleX);
else if (bone.ascaleX != 0) // else if (bone.ascaleX != 0) //
bone.ascaleX = 1 + (value / bone.ascaleX - 1) * data.mixScaleX; bone.ascaleX = 1 + (value / bone.ascaleX - 1) * constraint.mixScaleX;
} else { } else {
float s; float s;
if (additive) if (additive)
s = 1 + (value - 1) * data.mixScaleX; s = 1 + (value - 1) * constraint.mixScaleX;
else { else {
s = (float)Math.sqrt(bone.a * bone.a + bone.c * bone.c); s = (float)Math.sqrt(bone.a * bone.a + bone.c * bone.c);
if (s != 0) s = 1 + (value / s - 1) * data.mixScaleX; if (s != 0) s = 1 + (value / s - 1) * constraint.mixScaleX;
} }
bone.a *= s; bone.a *= s;
bone.c *= s; bone.c *= s;
@ -324,19 +324,19 @@ public class TransformConstraintData extends ConstraintData {
return constraint.mixScaleY; return constraint.mixScaleY;
} }
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) { public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean additive) {
if (local) { if (local) {
if (additive) if (additive)
bone.ascaleY *= 1 + ((value - 1) * data.mixScaleY); bone.ascaleY *= 1 + ((value - 1) * constraint.mixScaleY);
else if (bone.ascaleY != 0) // else if (bone.ascaleY != 0) //
bone.ascaleY = 1 + (value / bone.ascaleY - 1) * data.mixScaleY; bone.ascaleY = 1 + (value / bone.ascaleY - 1) * constraint.mixScaleY;
} else { } else {
float s; float s;
if (additive) if (additive)
s = 1 + (value - 1) * data.mixScaleY; s = 1 + (value - 1) * constraint.mixScaleY;
else { else {
s = (float)Math.sqrt(bone.b * bone.b + bone.d * bone.d); s = (float)Math.sqrt(bone.b * bone.b + bone.d * bone.d);
if (s != 0) s = 1 + (value / s - 1) * data.mixScaleY; if (s != 0) s = 1 + (value / s - 1) * constraint.mixScaleY;
} }
bone.b *= s; bone.b *= s;
bone.d *= s; bone.d *= s;
@ -355,10 +355,10 @@ public class TransformConstraintData extends ConstraintData {
return constraint.mixShearY; return constraint.mixShearY;
} }
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) { public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean additive) {
if (local) { if (local) {
if (!additive) value -= bone.ashearY; if (!additive) value -= bone.ashearY;
bone.ashearY += value * data.mixShearY; bone.ashearY += value * constraint.mixShearY;
} else { } else {
float b = bone.b, d = bone.d, by = atan2(d, b); float b = bone.b, d = bone.d, by = atan2(d, b);
value = (value + 90) * degRad; value = (value + 90) * degRad;
@ -371,7 +371,7 @@ public class TransformConstraintData extends ConstraintData {
else if (value < -PI) // else if (value < -PI) //
value += PI2; value += PI2;
} }
value = by + value * data.mixShearY; value = by + value * constraint.mixShearY;
float s = (float)Math.sqrt(b * b + d * d); float s = (float)Math.sqrt(b * b + d * d);
bone.b = cos(value) * s; bone.b = cos(value) * s;
bone.d = sin(value) * s; bone.d = sin(value) * s;