[libgdx] Transform constraint relative->additive and mix applies on "to" side.

This commit is contained in:
Nathan Sweet 2025-04-02 15:25:02 -04:00
parent 7a366cd6f5
commit 764eafa212
4 changed files with 60 additions and 60 deletions

View File

@ -285,7 +285,7 @@ public class SkeletonBinary extends SkeletonLoader {
data.skinRequired = (flags & 1) != 0;
data.localSource = (flags & 2) != 0;
data.localTarget = (flags & 4) != 0;
data.relative = (flags & 8) != 0;
data.additive = (flags & 8) != 0;
data.clamp = (flags & 16) != 0;
Object[] froms = data.properties.setSize(nn = flags >> 5);
for (int ii = 0, tn; ii < nn; ii++) {

View File

@ -258,7 +258,7 @@ public class SkeletonJson extends SkeletonLoader {
data.localSource = constraintMap.getBoolean("localSource", false);
data.localTarget = constraintMap.getBoolean("localTarget", false);
data.relative = constraintMap.getBoolean("relative", false);
data.additive = constraintMap.getBoolean("additive", false);
data.clamp = constraintMap.getBoolean("clamp", false);
for (JsonValue fromEntry = constraintMap.getChild("properties"); fromEntry != null; fromEntry = fromEntry.next) {

View File

@ -97,7 +97,7 @@ public class TransformConstraint implements Updatable {
if (mixRotate == 0 && mixX == 0 && mixY == 0 && mixScaleX == 0 && mixScaleY == 0 && mixShearY == 0) return;
TransformConstraintData data = this.data;
boolean localFrom = data.localSource, localTarget = data.localTarget, relative = data.relative, clamp = data.clamp;
boolean localFrom = data.localSource, localTarget = data.localTarget, additive = data.additive, clamp = data.clamp;
Bone source = this.source;
Object[] fromItems = data.properties.items;
int fn = data.properties.size;
@ -106,11 +106,11 @@ public class TransformConstraint implements Updatable {
var bone = (Bone)bones[i];
for (int f = 0; f < fn; f++) {
var from = (FromProperty)fromItems[f];
if (from.mix(this) != 0) {
float value = from.value(data, source, localFrom) - from.offset;
Object[] toItems = from.to.items;
for (int t = 0, tn = from.to.size; t < tn; t++) {
var to = (ToProperty)toItems[t];
float value = from.value(data, source, localFrom) - from.offset;
Object[] toItems = from.to.items;
for (int t = 0, tn = from.to.size; t < tn; t++) {
var to = (ToProperty)toItems[t];
if (to.mix(this) != 0) {
float clamped = to.offset + value * to.scale;
if (clamp) {
if (to.offset < to.max)
@ -118,7 +118,7 @@ public class TransformConstraint implements Updatable {
else
clamped = clamp(clamped, to.max, to.offset);
}
to.apply(data, bone, clamped, localTarget, relative);
to.apply(data, bone, clamped, localTarget, additive);
}
}
}

View File

@ -40,7 +40,7 @@ public class TransformConstraintData extends ConstraintData {
final Array<BoneData> bones = new Array();
BoneData source;
float offsetX, offsetY, mixRotate, mixX, mixY, mixScaleX, mixScaleY, mixShearY;
boolean localSource, localTarget, relative, clamp;
boolean localSource, localTarget, additive, clamp;
final Array<FromProperty> properties = new Array();
public TransformConstraintData (String name) {
@ -158,12 +158,12 @@ public class TransformConstraintData extends ConstraintData {
}
/** Adds the source bone transform to the constrained bones instead of setting it absolutely. */
public boolean getRelative () {
return relative;
public boolean getAdditive () {
return additive;
}
public void setRelative (boolean relative) {
this.relative = relative;
public void setAdditive (boolean additive) {
this.additive = additive;
}
/** Prevents constrained bones from exceeding the ranged defined by {@link ToProperty#offset} and {@link ToProperty#max}. */
@ -183,9 +183,6 @@ public class TransformConstraintData extends ConstraintData {
/** Constrained properties. */
public final Array<ToProperty> to = new Array();
/** Reads the mix for this property from the specified constraint. */
abstract public float mix (TransformConstraint constraint);
/** Reads this property from the specified bone. */
abstract public float value (TransformConstraintData data, Bone source, boolean local);
}
@ -201,29 +198,32 @@ public class TransformConstraintData extends ConstraintData {
/** The scale of the {@link FromProperty} value in relation to this property. */
public float scale;
/** Reads the mix for this property from the specified constraint. */
abstract public float mix (TransformConstraint constraint);
/** Applies the value to this property. */
abstract public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean relative);
abstract public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive);
}
static public class FromRotate extends FromProperty {
public float mix (TransformConstraint constraint) {
return constraint.mixRotate;
}
public float value (TransformConstraintData data, Bone source, boolean local) {
return local ? source.arotation : atan2(source.c, source.a) * radDeg;
}
}
static public class ToRotate extends ToProperty {
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean relative) {
public float mix (TransformConstraint constraint) {
return constraint.mixRotate;
}
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) {
if (local) {
if (!relative) value -= bone.arotation;
if (!additive) value -= bone.arotation;
bone.arotation += value * data.mixRotate;
} else {
float a = bone.a, b = bone.b, c = bone.c, d = bone.d;
value *= degRad;
if (!relative) value -= atan2(c, a);
if (!additive) value -= atan2(c, a);
if (value > PI)
value -= PI2;
else if (value < -PI) //
@ -239,69 +239,69 @@ public class TransformConstraintData extends ConstraintData {
}
static public class FromX extends FromProperty {
public float mix (TransformConstraint constraint) {
return constraint.mixX;
}
public float value (TransformConstraintData data, Bone source, boolean local) {
return local ? source.ax + data.offsetX : data.offsetX * source.a + data.offsetY * source.b + source.worldX;
}
}
static public class ToX extends ToProperty {
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean relative) {
public float mix (TransformConstraint constraint) {
return constraint.mixX;
}
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) {
if (local) {
if (!relative) value -= bone.ax;
if (!additive) value -= bone.ax;
bone.ax += value * data.mixX;
} else {
if (!relative) value -= bone.worldX;
if (!additive) value -= bone.worldX;
bone.worldX += value * data.mixX;
}
}
}
static public class FromY extends FromProperty {
public float mix (TransformConstraint constraint) {
return constraint.mixY;
}
public float value (TransformConstraintData data, Bone source, boolean local) {
return local ? source.ay + data.offsetY : data.offsetX * source.c + data.offsetY * source.d + source.worldY;
}
}
static public class ToY extends ToProperty {
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean relative) {
public float mix (TransformConstraint constraint) {
return constraint.mixY;
}
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) {
if (local) {
if (!relative) value -= bone.ay;
if (!additive) value -= bone.ay;
bone.ay += value * data.mixY;
} else {
if (!relative) value -= bone.worldY;
if (!additive) value -= bone.worldY;
bone.worldY += value * data.mixY;
}
}
}
static public class FromScaleX extends FromProperty {
public float mix (TransformConstraint constraint) {
return constraint.mixScaleX;
}
public float value (TransformConstraintData data, Bone source, boolean local) {
return local ? source.ascaleX : (float)Math.sqrt(source.a * source.a + source.c * source.c);
}
}
static public class ToScaleX extends ToProperty {
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean relative) {
public float mix (TransformConstraint constraint) {
return constraint.mixScaleX;
}
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) {
if (local) {
if (relative)
if (additive)
bone.ascaleX *= 1 + ((value - 1) * data.mixScaleX);
else if (bone.ascaleX != 0) //
bone.ascaleX = 1 + (value / bone.ascaleX - 1) * data.mixScaleX;
} else {
float s;
if (relative)
if (additive)
s = 1 + (value - 1) * data.mixScaleX;
else {
s = (float)Math.sqrt(bone.a * bone.a + bone.c * bone.c);
@ -314,25 +314,25 @@ public class TransformConstraintData extends ConstraintData {
}
static public class FromScaleY extends FromProperty {
public float mix (TransformConstraint constraint) {
return constraint.mixScaleY;
}
public float value (TransformConstraintData data, Bone source, boolean local) {
return local ? source.ascaleY : (float)Math.sqrt(source.b * source.b + source.d * source.d);
}
}
static public class ToScaleY extends ToProperty {
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean relative) {
public float mix (TransformConstraint constraint) {
return constraint.mixScaleY;
}
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) {
if (local) {
if (relative)
if (additive)
bone.ascaleY *= 1 + ((value - 1) * data.mixScaleY);
else if (bone.ascaleY != 0) //
bone.ascaleY = 1 + (value / bone.ascaleY - 1) * data.mixScaleY;
} else {
float s;
if (relative)
if (additive)
s = 1 + (value - 1) * data.mixScaleY;
else {
s = (float)Math.sqrt(bone.b * bone.b + bone.d * bone.d);
@ -345,24 +345,24 @@ public class TransformConstraintData extends ConstraintData {
}
static public class FromShearY extends FromProperty {
public float mix (TransformConstraint constraint) {
return constraint.mixShearY;
}
public float value (TransformConstraintData data, Bone source, boolean local) {
return local ? source.ashearY : (atan2(source.d, source.b) - atan2(source.c, source.a)) * radDeg - 90;
}
}
static public class ToShearY extends ToProperty {
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean relative) {
public float mix (TransformConstraint constraint) {
return constraint.mixShearY;
}
public void apply (TransformConstraintData data, Bone bone, float value, boolean local, boolean additive) {
if (local) {
if (!relative) value -= bone.ashearY;
if (!additive) value -= bone.ashearY;
bone.ashearY += value * data.mixShearY;
} else {
float b = bone.b, d = bone.d, by = atan2(d, b);
value = (value + 90) * degRad;
if (relative)
if (additive)
value -= PI / 2;
else {
value -= by - atan2(bone.c, bone.a);