Improved non-constant speed paths.

This commit is contained in:
NathanSweet 2016-06-05 01:36:32 +02:00
parent 633432647b
commit 7729d423f7
2 changed files with 42 additions and 25 deletions

View File

@ -17,7 +17,8 @@ public class PathConstraint implements Updatable {
final Array<Bone> bones; final Array<Bone> bones;
Slot target; Slot target;
float position, rotateMix, translateMix, scaleMix; float position, rotateMix, translateMix, scaleMix;
final FloatArray lengths = new FloatArray(), positions = new FloatArray(), temp = new FloatArray();
final FloatArray spacing = new FloatArray(), positions = new FloatArray(), temp = new FloatArray();
public PathConstraint (PathConstraintData data, Skeleton skeleton) { public PathConstraint (PathConstraintData data, Skeleton skeleton) {
if (data == null) throw new IllegalArgumentException("data cannot be null."); if (data == null) throw new IllegalArgumentException("data cannot be null.");
@ -61,9 +62,9 @@ public class PathConstraint implements Updatable {
if (!translate && !rotate && !scale) return; if (!translate && !rotate && !scale) return;
PathAttachment path = (PathAttachment)attachment; PathAttachment path = (PathAttachment)attachment;
FloatArray lengths = this.lengths; FloatArray spacing = this.spacing;
lengths.clear(); spacing.clear();
lengths.add(0); spacing.add(0);
Array<Bone> bones = this.bones; Array<Bone> bones = this.bones;
int boneCount = bones.size; int boneCount = bones.size;
@ -91,7 +92,7 @@ public class PathConstraint implements Updatable {
for (int i = 0; i < boneCount; i++) { for (int i = 0; i < boneCount; i++) {
Bone bone = bones.get(i); Bone bone = bones.get(i);
float length = bone.data.length, x = length * bone.a, y = length * bone.c; float length = bone.data.length, x = length * bone.a, y = length * bone.c;
lengths.add((float)Math.sqrt(x * x + y * y)); spacing.add((float)Math.sqrt(x * x + y * y));
} }
float[] positions = computeWorldPositions(path, false); float[] positions = computeWorldPositions(path, false);
@ -102,9 +103,9 @@ public class PathConstraint implements Updatable {
bone.worldY += (boneY - bone.worldY) * translateMix; bone.worldY += (boneY - bone.worldY) * translateMix;
float x = positions[p], y = positions[p + 1], dx = x - boneX, dy = y - boneY; float x = positions[p], y = positions[p + 1], dx = x - boneX, dy = y - boneY;
if (scale) { if (scale) {
float length = lengths.get(i + 1); float space = spacing.get(i + 1);
if (length != 0) { if (space != 0) {
float s = ((float)Math.sqrt(dx * dx + dy * dy) / length - 1) * scaleMix + 1; float s = ((float)Math.sqrt(dx * dx + dy * dy) / space - 1) * scaleMix + 1;
bone.a *= s; bone.a *= s;
bone.c *= s; bone.c *= s;
} }
@ -143,8 +144,8 @@ public class PathConstraint implements Updatable {
private float[] computeWorldPositions (PathAttachment path, boolean tangents) { private float[] computeWorldPositions (PathAttachment path, boolean tangents) {
Slot target = this.target; Slot target = this.target;
float position = this.position; float position = this.position;
int lengthCount = lengths.size; int spacingCount = spacing.size;
float[] lengths = this.lengths.items; float[] spacing = this.spacing.items;
FloatArray positions = this.positions; FloatArray positions = this.positions;
positions.clear(); positions.clear();
boolean closed = path.getClosed(); boolean closed = path.getClosed();
@ -152,31 +153,49 @@ public class PathConstraint implements Updatable {
float[] temp; float[] temp;
int lastCurve = NONE; int lastCurve = NONE;
// New.
if (!path.getConstantSpeed()) { if (!path.getConstantSpeed()) {
if (!closed) curves--; if (!closed) curves--;
float[] curveLengths = path.getCurveLengths().items;
float pathLength = path.getTotalLength(); float pathLength = path.getTotalLength();
position *= pathLength;
temp = this.temp.setSize(8); temp = this.temp.setSize(8);
for (int i = 0; i < lengthCount; i++) { for (int i = 0, curve = 0; i < spacingCount; i++) {
position += lengths[i] / pathLength; position += spacing[i];
float p = position;
if (closed) { if (closed) {
position %= 1; p %= 1;
if (position < 0) position += 1; if (p < 0) p += 1;
} else if (position < 0) { } else if (p < 0) {
if (lastCurve != BEFORE) { if (lastCurve != BEFORE) {
lastCurve = BEFORE; lastCurve = BEFORE;
path.computeWorldVertices(target, 2, 4, temp, 0); path.computeWorldVertices(target, 2, 4, temp, 0);
} }
addBeforePosition(position * pathLength, temp, 0, positions, tangents); addBeforePosition(p, temp, 0, positions, tangents);
continue; continue;
} else if (position > 1) { } else if (p > pathLength) {
if (lastCurve != AFTER) { if (lastCurve != AFTER) {
lastCurve = AFTER; lastCurve = AFTER;
path.computeWorldVertices(target, verticesLength - 6, 4, temp, 0); path.computeWorldVertices(target, verticesLength - 6, 4, temp, 0);
} }
addAfterPosition((position - 1) * pathLength, temp, 0, positions, tangents); addAfterPosition(p - pathLength, temp, 0, positions, tangents);
continue; continue;
} }
int curve = position < 1 ? (int)(curves * position) : curves - 1;
// Determine curve containing position.
for (;; curve++) {
float length = curveLengths[curve];
if (p > length) continue;
if (curve == 0)
p /= length;
else {
float prev = curveLengths[curve - 1];
p = (p - prev) / (length - prev);
}
break;
}
if (curve != lastCurve) { if (curve != lastCurve) {
lastCurve = curve; lastCurve = curve;
if (closed && curve == curves - 1) { if (closed && curve == curves - 1) {
@ -185,8 +204,7 @@ public class PathConstraint implements Updatable {
} else } else
path.computeWorldVertices(target, curve * 6 + 2, 8, temp, 0); path.computeWorldVertices(target, curve * 6 + 2, 8, temp, 0);
} }
addCurvePosition((position - curve / (float)curves) * curves, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], addCurvePosition(p, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], positions, tangents);
temp[6], temp[7], positions, tangents);
} }
return positions.items; return positions.items;
} }
@ -245,9 +263,8 @@ public class PathConstraint implements Updatable {
position *= pathLength; position *= pathLength;
float curveLength = 0; float curveLength = 0;
int curve = 10, segment = 0; for (int i = 0, curve = 10, segment = 0; i < spacingCount; i++) {
for (int i = 0; i < lengthCount; i++) { position += spacing[i];
position += lengths[i];
float p = position; float p = position;
if (closed) { if (closed) {

View File

@ -81,7 +81,7 @@ public class PathAttachment extends VertexAttachment {
} }
/** Returns the length of each curve in the setup pose. */ /** Returns the length of each curve in the setup pose. */
public FloatArray getLengths () { public FloatArray getCurveLengths () {
return lengths; return lengths;
} }