Browse Source

[libgdx] Added skeleton reference scale.

Nathan Sweet 1 year ago
parent
commit
1dbbfda2c8

+ 20 - 20
spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/PhysicsConstraint.java

@@ -150,7 +150,7 @@ public class PhysicsConstraint implements Updatable {
 				ux = bx;
 				uy = by;
 			} else {
-				float remaining = this.remaining, i = inertia, step = data.step;
+				float a = this.remaining, i = inertia, t = data.step, f = skeleton.data.referenceScale;
 				if (x || y) {
 					if (x) {
 						xOffset += (ux - bx) * i;
@@ -160,22 +160,22 @@ public class PhysicsConstraint implements Updatable {
 						yOffset += (uy - by) * i;
 						uy = by;
 					}
-					if (remaining >= step) {
-						float m = massInverse * step, e = strength, w = wind * 100, g = gravity * -100;
-						float d = (float)Math.pow(damping, 60 * step);
+					if (a >= t) {
+						float m = massInverse * t, e = strength, w = wind * f, g = gravity * f;
+						float d = (float)Math.pow(damping, 60 * t);
 						do {
 							if (x) {
 								xVelocity += (w - xOffset * e) * m;
-								xOffset += xVelocity * step;
+								xOffset += xVelocity * t;
 								xVelocity *= d;
 							}
 							if (y) {
-								yVelocity += (g - yOffset * e) * m;
-								yOffset += yVelocity * step;
+								yVelocity -= (g + yOffset * e) * m;
+								yOffset += yVelocity * t;
 								yVelocity *= d;
 							}
-							remaining -= step;
-						} while (remaining >= step);
+							a -= t;
+						} while (a >= t);
 					}
 					if (x) bone.worldX += xOffset * mix * data.x;
 					if (y) bone.worldY += yOffset * mix * data.y;
@@ -199,31 +199,31 @@ public class PhysicsConstraint implements Updatable {
 						float r = l * bone.getWorldScaleX();
 						if (r > 0) scaleOffset += ((cx - bone.worldX) * c + (cy - bone.worldY) * s) * i / r;
 					}
-					remaining = this.remaining;
-					if (remaining >= step) {
-						float m = massInverse * step, e = strength, w = wind, g = gravity;
-						float d = (float)Math.pow(damping, 60 * step);
+					a = this.remaining;
+					if (a >= t) {
+						float m = massInverse * t, e = strength, w = wind, g = gravity;
+						float d = (float)Math.pow(damping, 60 * t), h = l / f;
 						while (true) {
-							remaining -= step;
+							a -= t;
 							if (scaleX) {
 								scaleVelocity += (w * c - g * s - scaleOffset * e) * m;
-								scaleOffset += scaleVelocity * step;
+								scaleOffset += scaleVelocity * t;
 								scaleVelocity *= d;
 							}
 							if (rotateOrShearX) {
-								rotateVelocity += (-0.01f * l * (w * s + g * c) - rotateOffset * e) * m;
-								rotateOffset += rotateVelocity * step;
+								rotateVelocity -= ((w * s + g * c) * h + rotateOffset * e) * m;
+								rotateOffset += rotateVelocity * t;
 								rotateVelocity *= d;
-								if (remaining < step) break;
+								if (a < t) break;
 								float r = rotateOffset * mr + ca;
 								c = cos(r);
 								s = sin(r);
-							} else if (remaining < step) //
+							} else if (a < t) //
 								break;
 						}
 					}
 				}
-				this.remaining = remaining;
+				this.remaining = a;
 			}
 			cx = bone.worldX;
 			cy = bone.worldY;

+ 1 - 3
spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/Skeleton.java

@@ -59,9 +59,7 @@ public class Skeleton {
 	final Array<Updatable> updateCache = new Array();
 	@Null Skin skin;
 	final Color color;
-	float x, y;
-	float scaleX = 1, scaleY = 1;
-	float time;
+	float x, y, scaleX = 1, scaleY = 1, time;
 
 	public Skeleton (SkeletonData data) {
 		if (data == null) throw new IllegalArgumentException("data cannot be null.");

+ 3 - 2
spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonBinary.java

@@ -174,6 +174,7 @@ public class SkeletonBinary extends SkeletonLoader {
 			skeletonData.y = input.readFloat();
 			skeletonData.width = input.readFloat();
 			skeletonData.height = input.readFloat();
+			skeletonData.referenceScale = input.readFloat() * scale;
 
 			boolean nonessential = input.readBoolean();
 			if (nonessential) {
@@ -336,8 +337,8 @@ public class SkeletonBinary extends SkeletonLoader {
 				data.strength = input.readFloat();
 				data.damping = input.readFloat();
 				data.massInverse = input.readFloat();
-				data.wind = input.readFloat() * scale;
-				data.gravity = input.readFloat() * scale;
+				data.wind = input.readFloat();
+				data.gravity = input.readFloat();
 				data.mix = input.readFloat();
 				flags = input.read();
 				if ((flags & 1) != 0) data.inertiaGlobal = true;

+ 11 - 1
spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonData.java

@@ -48,7 +48,7 @@ public class SkeletonData {
 	final Array<TransformConstraintData> transformConstraints = new Array();
 	final Array<PathConstraintData> pathConstraints = new Array();
 	final Array<PhysicsConstraintData> physicsConstraints = new Array();
-	float x, y, width, height;
+	float x, y, width, height, referenceScale = 100;
 	@Null String version, hash;
 
 	// Nonessential.
@@ -283,6 +283,16 @@ public class SkeletonData {
 		this.height = height;
 	}
 
+	/** Baseline scale factor for applying distance-dependent effects on non-scalable properties, such as angle or scale. Default
+	 * is 100. */
+	public float getReferenceScale () {
+		return referenceScale;
+	}
+
+	public void setReferenceScale (float referenceScale) {
+		this.referenceScale = referenceScale;
+	}
+
 	/** The Spine version used to export the skeleton data, or null. */
 	public @Null String getVersion () {
 		return version;

+ 4 - 3
spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonJson.java

@@ -143,6 +143,7 @@ public class SkeletonJson extends SkeletonLoader {
 			skeletonData.y = skeletonMap.getFloat("y", 0);
 			skeletonData.width = skeletonMap.getFloat("width", 0);
 			skeletonData.height = skeletonMap.getFloat("height", 0);
+			skeletonData.referenceScale = skeletonMap.getFloat("referenceScale", 100) * scale;
 			skeletonData.fps = skeletonMap.getFloat("fps", 30);
 			skeletonData.imagesPath = skeletonMap.getString("images", null);
 			skeletonData.audioPath = skeletonMap.getString("audio", null);
@@ -317,9 +318,9 @@ public class SkeletonJson extends SkeletonLoader {
 			data.inertia = constraintMap.getFloat("inertia", 1);
 			data.strength = constraintMap.getFloat("strength", 100);
 			data.damping = constraintMap.getFloat("damping", 1);
-			data.massInverse = 1f / constraintMap.getFloat("mass", 1);
-			data.wind = constraintMap.getFloat("wind", 0) * scale;
-			data.gravity = constraintMap.getFloat("gravity", 0) * scale;
+			data.massInverse = 1 / constraintMap.getFloat("mass", 1);
+			data.wind = constraintMap.getFloat("wind", 0);
+			data.gravity = constraintMap.getFloat("gravity", 0);
 			data.mix = constraintMap.getFloat("mix", 1);
 			data.inertiaGlobal = constraintMap.getBoolean("inertiaGlobal", false);
 			data.strengthGlobal = constraintMap.getBoolean("strengthGlobal", false);