Added local and relative to transform constraint.

This commit is contained in:
NathanSweet 2016-12-14 03:14:52 +01:00
parent 98b4f3870d
commit e80b5bc7c9
2 changed files with 165 additions and 4 deletions

View File

@ -81,6 +81,21 @@ public class TransformConstraint implements Constraint {
}
public void update () {
if (data.local) {
if (data.relative)
applyRelativeLocal();
else
applyAbsoluteLocal();
} else {
if (data.relative)
applyRelativeWorld();
else
applyAbsoluteWorld();
}
}
private void applyAbsoluteWorld () {
float rotateMix = this.rotateMix, translateMix = this.translateMix, scaleMix = this.scaleMix, shearMix = this.shearMix;
Bone target = this.target;
float ta = target.a, tb = target.b, tc = target.c, td = target.d;
@ -116,13 +131,11 @@ public class TransformConstraint implements Constraint {
if (scaleMix > 0) {
float s = (float)Math.sqrt(bone.a * bone.a + bone.c * bone.c);
float ts = (float)Math.sqrt(ta * ta + tc * tc);
if (s > 0.00001f) s = (s + (ts - s + data.offsetScaleX) * scaleMix) / s;
if (s > 0.00001f) s = (s + ((float)Math.sqrt(ta * ta + tc * tc) - s + data.offsetScaleX) * scaleMix) / s;
bone.a *= s;
bone.c *= s;
s = (float)Math.sqrt(bone.b * bone.b + bone.d * bone.d);
ts = (float)Math.sqrt(tb * tb + td * td);
if (s > 0.00001f) s = (s + (ts - s + data.offsetScaleY) * scaleMix) / s;
if (s > 0.00001f) s = (s + ((float)Math.sqrt(tb * tb + td * td) - s + data.offsetScaleY) * scaleMix) / s;
bone.b *= s;
bone.d *= s;
modified = true;
@ -146,6 +159,137 @@ public class TransformConstraint implements Constraint {
}
}
private void applyRelativeWorld () {
float rotateMix = this.rotateMix, translateMix = this.translateMix, scaleMix = this.scaleMix, shearMix = this.shearMix;
Bone target = this.target;
float ta = target.a, tb = target.b, tc = target.c, td = target.d;
float degRadReflect = ta * td - tb * tc > 0 ? degRad : -degRad;
float offsetRotation = data.offsetRotation * degRadReflect, offsetShearY = data.offsetShearY * degRadReflect;
Array<Bone> bones = this.bones;
for (int i = 0, n = bones.size; i < n; i++) {
Bone bone = bones.get(i);
boolean modified = false;
if (rotateMix != 0) {
float a = bone.a, b = bone.b, c = bone.c, d = bone.d;
float r = atan2(tc, ta) + offsetRotation;
if (r > PI)
r -= PI2;
else if (r < -PI) r += PI2;
r *= rotateMix;
float cos = cos(r), sin = sin(r);
bone.a = cos * a - sin * c;
bone.b = cos * b - sin * d;
bone.c = sin * a + cos * c;
bone.d = sin * b + cos * d;
modified = true;
}
if (translateMix != 0) {
Vector2 temp = this.temp;
target.localToWorld(temp.set(data.offsetX, data.offsetY));
bone.worldX += temp.x * translateMix;
bone.worldY += temp.y * translateMix;
modified = true;
}
if (scaleMix > 0) {
float s = ((float)Math.sqrt(ta * ta + tc * tc) - 1 + data.offsetScaleX) * scaleMix + 1;
bone.a *= s;
bone.c *= s;
s = ((float)Math.sqrt(tb * tb + td * td) - 1 + data.offsetScaleY) * scaleMix + 1;
bone.b *= s;
bone.d *= s;
modified = true;
}
if (shearMix > 0) {
float r = atan2(td, tb) - atan2(tc, ta);
if (r > PI)
r -= PI2;
else if (r < -PI) r += PI2;
float b = bone.b, d = bone.d;
r = atan2(d, b) + (r - PI / 2 + offsetShearY) * shearMix;
float s = (float)Math.sqrt(b * b + d * d);
bone.b = cos(r) * s;
bone.d = sin(r) * s;
modified = true;
}
if (modified) bone.appliedValid = false;
}
}
private void applyAbsoluteLocal () {
float rotateMix = this.rotateMix, translateMix = this.translateMix, scaleMix = this.scaleMix, shearMix = this.shearMix;
Bone target = this.target;
if (!target.appliedValid) target.updateAppliedTransform();
Array<Bone> bones = this.bones;
for (int i = 0, n = bones.size; i < n; i++) {
Bone bone = bones.get(i);
if (!bone.appliedValid) bone.updateAppliedTransform();
float rotation = bone.arotation;
if (rotateMix != 0) {
float r = target.arotation - rotation + data.offsetRotation;
r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360;
rotation += r * rotateMix;
}
float x = bone.ax, y = bone.ay;
if (translateMix != 0) {
x += (target.ax - x + data.offsetX) * translateMix;
y += (target.ay - y + data.offsetY) * translateMix;
}
float scaleX = bone.ascaleX, scaleY = bone.ascaleY;
if (scaleMix > 0) {
if (scaleX > 0.00001f) scaleX = (scaleX + (target.ascaleX - scaleX + data.offsetScaleX) * scaleMix) / scaleX;
if (scaleY > 0.00001f) scaleY = (scaleY + (target.ascaleY - scaleY + data.offsetScaleY) * scaleMix) / scaleY;
}
float shearY = bone.ashearY;
if (shearMix > 0) {
float r = target.ashearY - shearY + data.offsetShearY;
r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360;
bone.shearY += r * shearMix;
}
bone.updateWorldTransform(x, y, rotation, scaleX, scaleY, bone.ashearX, shearY);
}
}
private void applyRelativeLocal () {
float rotateMix = this.rotateMix, translateMix = this.translateMix, scaleMix = this.scaleMix, shearMix = this.shearMix;
Bone target = this.target;
if (!target.appliedValid) target.updateAppliedTransform();
Array<Bone> bones = this.bones;
for (int i = 0, n = bones.size; i < n; i++) {
Bone bone = bones.get(i);
if (!bone.appliedValid) bone.updateAppliedTransform();
float rotation = bone.arotation;
if (rotateMix != 0) rotation += (target.arotation + data.offsetRotation) * rotateMix;
float x = bone.ax, y = bone.ay;
if (translateMix != 0) {
x += (target.ax + data.offsetX) * translateMix;
y += (target.ay + data.offsetY) * translateMix;
}
float scaleX = bone.ascaleX, scaleY = bone.ascaleY;
if (scaleMix > 0) {
if (scaleX > 0.00001f) scaleX *= ((target.ascaleX - 1 + data.offsetScaleX) * scaleMix) + 1;
if (scaleY > 0.00001f) scaleY *= ((target.ascaleY - 1 + data.offsetScaleY) * scaleMix) + 1;
}
float shearY = bone.ashearY;
if (shearMix > 0) shearY += (target.ashearY + data.offsetShearY) * shearMix;
bone.updateWorldTransform(x, y, rotation, scaleX, scaleY, bone.ashearX, shearY);
}
}
public int getOrder () {
return data.order;
}

View File

@ -42,6 +42,7 @@ public class TransformConstraintData {
BoneData target;
float rotateMix, translateMix, scaleMix, shearMix;
float offsetRotation, offsetX, offsetY, offsetScaleX, offsetScaleY, offsetShearY;
boolean relative, local;
public TransformConstraintData (String name) {
if (name == null) throw new IllegalArgumentException("name cannot be null.");
@ -167,6 +168,22 @@ public class TransformConstraintData {
this.offsetShearY = offsetShearY;
}
public boolean getRelative () {
return relative;
}
public void setRelative (boolean relative) {
this.relative = relative;
}
public boolean getLocal () {
return local;
}
public void setLocal (boolean local) {
this.local = local;
}
public String toString () {
return name;
}