[unity] Port of commits f05a152 and 82ad169: Fixed physics jitter. See #2820.

This commit is contained in:
Harald Csaszar 2025-04-28 17:30:20 +02:00
parent b263728502
commit 6da9ac59b7
2 changed files with 33 additions and 20 deletions

View File

@ -44,10 +44,10 @@ namespace Spine {
bool reset = true; bool reset = true;
float ux, uy, cx, cy, tx, ty; float ux, uy, cx, cy, tx, ty;
float xOffset, xVelocity; float xOffset, xLag, xVelocity;
float yOffset, yVelocity; float yOffset, yLag, yVelocity;
float rotateOffset, rotateVelocity; float rotateOffset, rotateLag, rotateVelocity;
float scaleOffset, scaleVelocity; float scaleOffset, scaleLag, scaleVelocity;
internal bool active; internal bool active;
@ -89,12 +89,16 @@ namespace Spine {
lastTime = skeleton.time; lastTime = skeleton.time;
reset = true; reset = true;
xOffset = 0; xOffset = 0;
xLag = 0;
xVelocity = 0; xVelocity = 0;
yOffset = 0; yOffset = 0;
yLag = 0;
yVelocity = 0; yVelocity = 0;
rotateOffset = 0; rotateOffset = 0;
rotateLag = 0;
rotateVelocity = 0; rotateVelocity = 0;
scaleOffset = 0; scaleOffset = 0;
scaleLag = 0;
scaleVelocity = 0; scaleVelocity = 0;
} }
@ -137,7 +141,7 @@ namespace Spine {
bool x = data.x > 0, y = data.y > 0, rotateOrShearX = data.rotate > 0 || data.shearX > 0, scaleX = data.scaleX > 0; bool x = data.x > 0, y = data.y > 0, rotateOrShearX = data.rotate > 0 || data.shearX > 0, scaleX = data.scaleX > 0;
Bone bone = this.bone; Bone bone = this.bone;
float l = bone.data.length; float l = bone.data.length, t = data.step, z = 0;
switch (physics) { switch (physics) {
case Physics.None: case Physics.None:
@ -157,8 +161,8 @@ namespace Spine {
ux = bx; ux = bx;
uy = by; uy = by;
} else { } else {
float a = remaining, i = inertia, t = data.step, f = skeleton.data.referenceScale, d = -1; float a = remaining, i = inertia, f = skeleton.data.referenceScale, d = -1, qx = data.limit * delta,
float qx = data.limit * delta, qy = qx * Math.Abs(skeleton.ScaleY); qy = qx * Math.Abs(skeleton.ScaleY);
qx *= Math.Abs(skeleton.ScaleX); qx *= Math.Abs(skeleton.ScaleX);
if (x || y) { if (x || y) {
@ -174,7 +178,8 @@ namespace Spine {
} }
if (a >= t) { if (a >= t) {
d = (float)Math.Pow(damping, 60 * t); d = (float)Math.Pow(damping, 60 * t);
float m = massInverse * t, e = strength, w = wind * f * skeleton.ScaleX, g = gravity * f * skeleton.ScaleY; float m = massInverse * t, e = strength, w = wind * f * skeleton.ScaleX,
g = gravity * f * skeleton.ScaleY, xs = xOffset, ys = yOffset;
do { do {
if (x) { if (x) {
xVelocity += (w - xOffset * e) * m; xVelocity += (w - xOffset * e) * m;
@ -188,13 +193,15 @@ namespace Spine {
} }
a -= t; a -= t;
} while (a >= t); } while (a >= t);
xLag = xOffset - xs;
yLag = yOffset - ys;
} }
if (x) bone.worldX += xOffset * mix * data.x; z = Math.Max(0, 1 - a / t);
if (y) bone.worldY += yOffset * mix * data.y; if (x) bone.worldX += (xOffset - xLag * z) * mix * data.x;
if (y) bone.worldY += (yOffset - yLag * z) * mix * data.y;
} }
if (rotateOrShearX || scaleX) { if (rotateOrShearX || scaleX) {
float ca = (float)Math.Atan2(bone.c, bone.a), c, s, mr = 0; float ca = (float)Math.Atan2(bone.c, bone.a), c, s, mr = 0, dx = cx - bone.worldX, dy = cy - bone.worldY;
float dx = cx - bone.worldX, dy = cy - bone.worldY;
if (dx > qx) if (dx > qx)
dx = qx; dx = qx;
else if (dx < -qx) else if (dx < -qx)
@ -203,11 +210,12 @@ namespace Spine {
dy = qy; dy = qy;
else if (dy < -qy) else if (dy < -qy)
dy = -qy; dy = -qy;
a = remaining;
if (rotateOrShearX) { if (rotateOrShearX) {
mr = (data.rotate + data.shearX) * mix; mr = (data.rotate + data.shearX) * mix;
float r = (float)Math.Atan2(dy + ty, dx + tx) - ca - rotateOffset * mr; float rz = rotateLag * Math.Max(0, 1 - a / t), r = (float)Math.Atan2(dy + ty, dx + tx) - ca - (rotateOffset - rz) * mr;
rotateOffset += (r - (float)Math.Ceiling(r * MathUtils.InvPI2 - 0.5f) * MathUtils.PI2) * i; rotateOffset += (r - (float)Math.Ceiling(r * MathUtils.InvPI2 - 0.5f) * MathUtils.PI2) * i;
r = rotateOffset * mr + ca; r = (rotateOffset - rz) * mr + ca;
c = (float)Math.Cos(r); c = (float)Math.Cos(r);
s = (float)Math.Sin(r); s = (float)Math.Sin(r);
if (scaleX) { if (scaleX) {
@ -223,7 +231,8 @@ namespace Spine {
a = remaining; a = remaining;
if (a >= t) { if (a >= t) {
if (d == -1) d = (float)Math.Pow(damping, 60 * t); if (d == -1) d = (float)Math.Pow(damping, 60 * t);
float m = massInverse * t, e = strength, w = wind, g = (Bone.yDown ? -gravity : gravity), h = l / f; float m = massInverse * t, e = strength, w = wind, g = (Bone.yDown ? -gravity : gravity), h = l / f,
rs = rotateOffset, ss = scaleOffset;
while (true) { while (true) {
a -= t; a -= t;
if (scaleX) { if (scaleX) {
@ -242,7 +251,10 @@ namespace Spine {
} else if (a < t) // } else if (a < t) //
break; break;
} }
rotateLag = rotateOffset - rs;
scaleLag = scaleOffset - ss;
} }
z = Math.Max(0, 1 - a / t);
} }
remaining = a; remaining = a;
} }
@ -250,13 +262,14 @@ namespace Spine {
cy = bone.worldY; cy = bone.worldY;
break; break;
case Physics.Pose: case Physics.Pose:
if (x) bone.worldX += xOffset * mix * data.x; z = Math.Max(0, 1 - remaining / t);
if (y) bone.worldY += yOffset * mix * data.y; if (x) bone.worldX += (xOffset - xLag * z) * mix * data.x;
if (y) bone.worldY += (yOffset - yLag * z) * mix * data.y;
break; break;
} }
if (rotateOrShearX) { if (rotateOrShearX) {
float o = rotateOffset * mix, s, c, a; float o = (rotateOffset - rotateLag * z) * mix, s, c, a;
if (data.shearX > 0) { if (data.shearX > 0) {
float r = 0; float r = 0;
if (data.rotate > 0) { if (data.rotate > 0) {
@ -286,7 +299,7 @@ namespace Spine {
} }
} }
if (scaleX) { if (scaleX) {
float s = 1 + scaleOffset * mix * data.scaleX; float s = 1 + (scaleOffset - scaleLag * z) * mix * data.scaleX;
bone.a *= s; bone.a *= s;
bone.c *= s; bone.c *= s;
} }

View File

@ -2,7 +2,7 @@
"name": "com.esotericsoftware.spine.spine-csharp", "name": "com.esotericsoftware.spine.spine-csharp",
"displayName": "spine-csharp Runtime", "displayName": "spine-csharp Runtime",
"description": "This plugin provides the spine-csharp core runtime.", "description": "This plugin provides the spine-csharp core runtime.",
"version": "4.3.1", "version": "4.3.2",
"unity": "2018.3", "unity": "2018.3",
"author": { "author": {
"name": "Esoteric Software", "name": "Esoteric Software",