[libgdx] Transform constraint fixes, javadoc.

This commit is contained in:
Nathan Sweet 2025-03-30 14:45:43 -04:00
parent 3b78925ca8
commit d459b040d0
5 changed files with 71 additions and 49 deletions

View File

@ -38,7 +38,7 @@ import com.esotericsoftware.spine.Skeleton.Physics;
* See <a href="https://esotericsoftware.com/spine-physics-constraints">Physics constraints</a> in the Spine User Guide. */ * See <a href="https://esotericsoftware.com/spine-physics-constraints">Physics constraints</a> in the Spine User Guide. */
public class PhysicsConstraint implements Updatable { public class PhysicsConstraint implements Updatable {
final PhysicsConstraintData data; final PhysicsConstraintData data;
public Bone bone; Bone bone;
float inertia, strength, damping, massInverse, wind, gravity, mix; float inertia, strength, damping, massInverse, wind, gravity, mix;
boolean reset = true; boolean reset = true;

View File

@ -287,7 +287,7 @@ public class SkeletonBinary extends SkeletonLoader {
data.localTo = (flags & 4) != 0; data.localTo = (flags & 4) != 0;
data.relative = (flags & 8) != 0; data.relative = (flags & 8) != 0;
data.clamp = (flags & 16) != 0; data.clamp = (flags & 16) != 0;
nn = flags >> 5; Object[] froms = data.properties.setSize(nn = flags >> 5);
for (int ii = 0, tn; ii < nn; ii++) { for (int ii = 0, tn; ii < nn; ii++) {
FromProperty from = switch (input.readByte()) { FromProperty from = switch (input.readByte()) {
case 0 -> new FromRotate(); case 0 -> new FromRotate();
@ -299,7 +299,7 @@ public class SkeletonBinary extends SkeletonLoader {
default -> null; default -> null;
}; };
from.offset = input.readFloat() * scale; from.offset = input.readFloat() * scale;
Object[] properties = from.to.setSize(tn = input.readInt(true)); Object[] tos = from.to.setSize(tn = input.readByte());
for (int t = 0; t < tn; t++) { for (int t = 0; t < tn; t++) {
ToProperty to = switch (input.readByte()) { ToProperty to = switch (input.readByte()) {
case 0 -> new ToRotate(); case 0 -> new ToRotate();
@ -313,8 +313,9 @@ public class SkeletonBinary extends SkeletonLoader {
to.offset = input.readFloat() * scale; to.offset = input.readFloat() * scale;
to.max = input.readFloat() * scale; to.max = input.readFloat() * scale;
to.scale = input.readFloat(); to.scale = input.readFloat();
properties[i] = to; tos[i] = to;
} }
froms[ii] = from;
} }
flags = input.read(); flags = input.read();
if ((flags & 1) != 0) data.mixRotate = input.readFloat(); if ((flags & 1) != 0) data.mixRotate = input.readFloat();

View File

@ -272,8 +272,8 @@ public class SkeletonJson extends SkeletonLoader {
default -> throw new SerializationException("Invalid transform constraint from property: " + fromEntry.name); default -> throw new SerializationException("Invalid transform constraint from property: " + fromEntry.name);
}; };
from.offset = fromEntry.getFloat("offset", 0) * scale; from.offset = fromEntry.getFloat("offset", 0) * scale;
for (JsonValue toEntry = constraintMap.getChild("to"); toEntry != null; toEntry = toEntry.next) { for (JsonValue toEntry = fromEntry.getChild("to"); toEntry != null; toEntry = toEntry.next) {
ToProperty to = switch (fromEntry.name) { ToProperty to = switch (toEntry.name) {
case "rotate" -> new ToRotate(); case "rotate" -> new ToRotate();
case "x" -> new ToX(); case "x" -> new ToX();
case "y" -> new ToY(); case "y" -> new ToY();
@ -287,6 +287,7 @@ public class SkeletonJson extends SkeletonLoader {
to.scale = toEntry.getFloat("scale"); to.scale = toEntry.getFloat("scale");
from.to.add(to); from.to.add(to);
} }
if (from.to.notEmpty()) data.properties.add(from);
} }
data.mixRotate = constraintMap.getFloat("mixRotate", 1); data.mixRotate = constraintMap.getFloat("mixRotate", 1);

View File

@ -105,14 +105,20 @@ public class TransformConstraint implements Updatable {
var bone = (Bone)bones[i]; var bone = (Bone)bones[i];
for (int f = 0; f < fn; f++) { for (int f = 0; f < fn; f++) {
var from = (FromProperty)fromItems[f]; var from = (FromProperty)fromItems[f];
if (from.mix(this) != 0) { float mix = from.mix(this);
if (mix != 0) {
float value = from.value(target, localFrom) - from.offset; float value = from.value(target, localFrom) - from.offset;
Object[] toItems = from.to.items; Object[] toItems = from.to.items;
for (int t = 0, tn = from.to.size; t < tn; t++) { for (int t = 0, tn = from.to.size; t < tn; t++) {
var to = (ToProperty)toItems[t]; var to = (ToProperty)toItems[t];
float clamped = to.offset + value * to.scale; float clamped = to.offset + value * to.scale;
if (clamp) clamped = clamp(clamped, to.offset, to.max); if (clamp) {
to.apply(this, bone, clamped, localTo, relative); if (to.offset < to.max)
clamped = clamp(clamped, to.offset, to.max);
else
clamped = clamp(clamped, to.max, to.offset);
}
to.apply(bone, clamped, localTo, relative, mix);
} }
} }
} }

View File

@ -121,6 +121,7 @@ public class TransformConstraintData extends ConstraintData {
this.mixShearY = mixShearY; this.mixShearY = mixShearY;
} }
/** Reads the target bone's local transform instead of its world transform. */
public boolean getLocalFrom () { public boolean getLocalFrom () {
return localFrom; return localFrom;
} }
@ -129,6 +130,7 @@ public class TransformConstraintData extends ConstraintData {
this.localFrom = localFrom; this.localFrom = localFrom;
} }
/** Sets the constrained bones' local transforms instead of their world transforms. */
public boolean getLocalTo () { public boolean getLocalTo () {
return localTo; return localTo;
} }
@ -137,6 +139,7 @@ public class TransformConstraintData extends ConstraintData {
this.localTo = localTo; this.localTo = localTo;
} }
/** Adds the target bone transform to the constrained bones instead of setting it absolutely. */
public boolean getRelative () { public boolean getRelative () {
return relative; return relative;
} }
@ -145,6 +148,7 @@ public class TransformConstraintData extends ConstraintData {
this.relative = relative; this.relative = relative;
} }
/** Prevents constrained bones from exceeding the ranged defined by {@link ToProperty#offset} and {@link ToProperty#max}. */
public boolean getClamp () { public boolean getClamp () {
return clamp; return clamp;
} }
@ -153,19 +157,34 @@ public class TransformConstraintData extends ConstraintData {
this.clamp = clamp; this.clamp = clamp;
} }
/** Source property for a {@link TransformConstraint}. */
static abstract public class FromProperty { static abstract public class FromProperty {
/** The value of this property that corresponds to {@link ToProperty#offset}. */
public float offset; public float offset;
/** Constrained properties. */
public final Array<ToProperty> to = new Array(); public final Array<ToProperty> to = new Array();
/** Reads this property from the specified bone. */
abstract public float value (Bone target, boolean local); abstract public float value (Bone target, boolean local);
/** Reads the mix for this property from the specified constraint. */
abstract public float mix (TransformConstraint constraint); abstract public float mix (TransformConstraint constraint);
} }
/** Constrained property for a {@link TransformConstraint}. */
static abstract public class ToProperty { static abstract public class ToProperty {
public float offset, max, scale; /** The value of this property that corresponds to {@link FromProperty#offset}. */
public float offset;
abstract public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean relative); /** The maximum value of this property when {@link TransformConstraintData#clamp clamped}. */
public float max;
/** The scale of the {@link FromProperty} value in relation to this property. */
public float scale;
/** Applies the value to this property. */
abstract public void apply (Bone bone, float value, boolean local, boolean relative, float mix);
} }
static public class FromRotate extends FromProperty { static public class FromRotate extends FromProperty {
@ -179,20 +198,20 @@ public class TransformConstraintData extends ConstraintData {
} }
static public class ToRotate extends ToProperty { static public class ToRotate extends ToProperty {
public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean relative) { public void apply (Bone bone, float value, boolean local, boolean relative, float mix) {
if (local) { if (local) {
if (!relative) value -= bone.arotation; if (!relative) value -= bone.arotation;
bone.arotation += value * constraint.mixRotate; bone.arotation += value * mix;
} 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;
float r = value * degRad; value *= degRad;
if (!relative) r -= atan2(c, a); if (!relative) value -= atan2(c, a);
if (r > PI) if (value > PI)
r -= PI2; value -= PI2;
else if (r < -PI) // else if (value < -PI) //
r += PI2; value += PI2;
r *= constraint.mixRotate; value *= mix;
float cos = cos(r), sin = sin(r); 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;
bone.c = sin * a + cos * c; bone.c = sin * a + cos * c;
@ -212,13 +231,13 @@ public class TransformConstraintData extends ConstraintData {
} }
static public class ToX extends ToProperty { static public class ToX extends ToProperty {
public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean relative) { public void apply (Bone bone, float value, boolean local, boolean relative, float mix) {
if (local) { if (local) {
if (!relative) value -= bone.ax; if (!relative) value -= bone.ax;
bone.ax += value * constraint.mixX; bone.ax += value * mix;
} else { } else {
if (!relative) value -= bone.worldX; if (!relative) value -= bone.worldX;
bone.worldX += value * constraint.mixX; bone.worldX += value * mix;
} }
} }
} }
@ -234,20 +253,20 @@ public class TransformConstraintData extends ConstraintData {
} }
static public class ToY extends ToProperty { static public class ToY extends ToProperty {
public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean relative) { public void apply (Bone bone, float value, boolean local, boolean relative, float mix) {
if (local) { if (local) {
if (!relative) value -= bone.ay; if (!relative) value -= bone.ay;
bone.ay += value * constraint.mixY; bone.ay += value * mix;
} else { } else {
if (!relative) value -= bone.worldY; if (!relative) value -= bone.worldY;
bone.worldY += value * constraint.mixY; bone.worldY += value * mix;
} }
} }
} }
static public class FromScaleX extends FromProperty { static public class FromScaleX extends FromProperty {
public float value (Bone target, boolean local) { public float value (Bone target, boolean local) {
return local ? target.arotation : (float)Math.sqrt(target.a * target.a + target.c * target.c); return local ? target.ascaleX : (float)Math.sqrt(target.a * target.a + target.c * target.c);
} }
public float mix (TransformConstraint constraint) { public float mix (TransformConstraint constraint) {
@ -256,19 +275,19 @@ public class TransformConstraintData extends ConstraintData {
} }
static public class ToScaleX extends ToProperty { static public class ToScaleX extends ToProperty {
public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean relative) { public void apply (Bone bone, float value, boolean local, boolean relative, float mix) {
if (local) { if (local) {
if (relative) if (relative)
bone.ascaleX *= 1 + ((value - 1) * constraint.mixScaleX); bone.ascaleX *= 1 + ((value - 1) * mix);
else if (bone.ascaleX != 0) // else if (bone.ascaleX != 0) //
bone.ascaleX = 1 + (value / bone.ascaleX - 1) * constraint.mixScaleX; bone.ascaleX = 1 + (value / bone.ascaleX - 1) * mix;
} else { } else {
float s; float s;
if (relative) if (relative)
s = 1 + (value - 1) * constraint.mixScaleX; s = 1 + (value - 1) * mix;
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) * constraint.mixScaleX; if (s != 0) s = 1 + (value / s - 1) * mix;
} }
bone.a *= s; bone.a *= s;
bone.c *= s; bone.c *= s;
@ -278,7 +297,7 @@ public class TransformConstraintData extends ConstraintData {
static public class FromScaleY extends FromProperty { static public class FromScaleY extends FromProperty {
public float value (Bone target, boolean local) { public float value (Bone target, boolean local) {
return local ? target.arotation : (float)Math.sqrt(target.b * target.b + target.d * target.d); return local ? target.ascaleY : (float)Math.sqrt(target.b * target.b + target.d * target.d);
} }
public float mix (TransformConstraint constraint) { public float mix (TransformConstraint constraint) {
@ -287,19 +306,19 @@ public class TransformConstraintData extends ConstraintData {
} }
static public class ToScaleY extends ToProperty { static public class ToScaleY extends ToProperty {
public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean relative) { public void apply (Bone bone, float value, boolean local, boolean relative, float mix) {
if (local) { if (local) {
if (relative) if (relative)
bone.ascaleY *= 1 + ((value - 1) * constraint.mixScaleY); bone.ascaleY *= 1 + ((value - 1) * mix);
else if (bone.ascaleY != 0) // else if (bone.ascaleY != 0) //
bone.ascaleY = 1 + (value / bone.ascaleY - 1) * constraint.mixScaleY; bone.ascaleY = 1 + (value / bone.ascaleY - 1) * mix;
} else { } else {
float s; float s;
if (relative) if (relative)
s = 1 + (value - 1) * constraint.mixScaleY; s = 1 + (value - 1) * mix;
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) * constraint.mixScaleY; if (s != 0) s = 1 + (value / s - 1) * mix;
} }
bone.b *= s; bone.b *= s;
bone.d *= s; bone.d *= s;
@ -309,13 +328,7 @@ public class TransformConstraintData extends ConstraintData {
static public class FromShearY extends FromProperty { static public class FromShearY extends FromProperty {
public float value (Bone target, boolean local) { public float value (Bone target, boolean local) {
if (local) return target.ashearY; return local ? target.ashearY : (atan2(target.d, target.b) - atan2(target.c, target.a)) * radDeg - 90;
float r = atan2(target.d, target.b) - atan2(target.c, target.a);
if (r > PI)
r -= PI2;
else if (r < -PI) //
r += PI2;
return r;
} }
public float mix (TransformConstraint constraint) { public float mix (TransformConstraint constraint) {
@ -324,12 +337,13 @@ public class TransformConstraintData extends ConstraintData {
} }
static public class ToShearY extends ToProperty { static public class ToShearY extends ToProperty {
public void apply (TransformConstraint constraint, Bone bone, float value, boolean local, boolean relative) { public void apply (Bone bone, float value, boolean local, boolean relative, float mix) {
if (local) { if (local) {
if (!relative) value -= bone.ashearY; if (!relative) value -= bone.ashearY;
bone.ashearY += value * constraint.mixShearY; bone.ashearY += value * mix;
} 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;
if (relative) if (relative)
value -= PI / 2; value -= PI / 2;
else { else {
@ -339,7 +353,7 @@ public class TransformConstraintData extends ConstraintData {
else if (value < -PI) // else if (value < -PI) //
value += PI2; value += PI2;
} }
value = by + value * constraint.mixShearY; value = by + value * mix;
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;