mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2025-12-21 17:56:04 +08:00
[libgdx] Physics improvements.
This commit is contained in:
parent
251b368cbf
commit
63202d339b
@ -128,74 +128,84 @@ public class PhysicsConstraint implements Updatable {
|
|||||||
remaining += Math.max(skeleton.time - lastTime, 0);
|
remaining += Math.max(skeleton.time - lastTime, 0);
|
||||||
lastTime = skeleton.time;
|
lastTime = skeleton.time;
|
||||||
|
|
||||||
float step = data.step;
|
float bx = bone.worldX, by = bone.worldY;
|
||||||
if (remaining >= step) {
|
if (reset) {
|
||||||
float bx = bone.worldX, by = bone.worldY, ca = 0, c = 0, s = 0;
|
reset = false;
|
||||||
if (reset) {
|
ux = bx;
|
||||||
reset = false;
|
uy = by;
|
||||||
ux = bx;
|
} else {
|
||||||
uy = by;
|
float remaining = this.remaining, i = this.inertia, step = data.step;
|
||||||
} else {
|
if (x || y) {
|
||||||
float i = this.inertia;
|
|
||||||
if (x) {
|
if (x) {
|
||||||
xOffset += (ux - bx) * i;
|
xOffset += (ux - bx) * i;
|
||||||
ux = bx;
|
ux = bx;
|
||||||
bone.worldX += xOffset * mix;
|
|
||||||
}
|
}
|
||||||
if (y) {
|
if (y) {
|
||||||
yOffset += (uy - by) * i;
|
yOffset += (uy - by) * i;
|
||||||
uy = by;
|
uy = by;
|
||||||
bone.worldY += yOffset * mix;
|
|
||||||
}
|
}
|
||||||
|
if (remaining >= step) {
|
||||||
|
float m = this.mass * step, e = this.strength, w = wind * 100, g = gravity * -100;
|
||||||
|
float d = (float)Math.pow(this.damping, 60 * step);
|
||||||
|
do {
|
||||||
|
if (x) {
|
||||||
|
xVelocity += (w - xOffset * e) * m;
|
||||||
|
xOffset += xVelocity * step;
|
||||||
|
xVelocity *= d;
|
||||||
|
}
|
||||||
|
if (y) {
|
||||||
|
yVelocity += (g - yOffset * e) * m;
|
||||||
|
yOffset += yVelocity * step;
|
||||||
|
yVelocity *= d;
|
||||||
|
}
|
||||||
|
remaining -= step;
|
||||||
|
} while (remaining >= step);
|
||||||
|
}
|
||||||
|
if (x) bone.worldX += xOffset * mix;
|
||||||
|
if (y) bone.worldY += yOffset * mix;
|
||||||
|
}
|
||||||
|
if (rotateOrShearX || scaleX) {
|
||||||
|
float ca = atan2(bone.c, bone.a), c, s;
|
||||||
if (rotateOrShearX) {
|
if (rotateOrShearX) {
|
||||||
ca = atan2(bone.c, bone.a);
|
|
||||||
float dx = cx - bone.worldX, dy = cy - bone.worldY, r = atan2(dy + ty, dx + tx) - ca - rotateOffset * mix;
|
float dx = cx - bone.worldX, dy = cy - bone.worldY, r = atan2(dy + ty, dx + tx) - ca - rotateOffset * mix;
|
||||||
rotateOffset += (r - (float)Math.ceil(r * invPI2 - 0.5f) * PI2) * i;
|
rotateOffset += (r - (float)Math.ceil(r * invPI2 - 0.5f) * PI2) * i;
|
||||||
r = rotateOffset * mix + ca;
|
r = rotateOffset * mix + ca;
|
||||||
c = cos(r);
|
c = cos(r);
|
||||||
s = sin(r);
|
s = sin(r);
|
||||||
if (scaleX) scaleOffset += (dx * c + dy * s) * i / l / bone.getWorldScaleX();
|
if (scaleX) scaleOffset += (dx * c + dy * s) * i / (l * bone.getWorldScaleX());
|
||||||
} else if (scaleX) {
|
} else {
|
||||||
float r = atan2(bone.c, bone.a);
|
c = cos(ca);
|
||||||
c = cos(r);
|
s = sin(ca);
|
||||||
s = sin(r);
|
scaleOffset += ((cx - bone.worldX) * c + (cy - bone.worldY) * s) * i / (l * bone.getWorldScaleX());
|
||||||
scaleOffset += ((cx - bone.worldX) * c + (cy - bone.worldY) * s) * i / l / bone.getWorldScaleX();
|
}
|
||||||
|
remaining = this.remaining;
|
||||||
|
if (remaining >= step) {
|
||||||
|
float m = this.mass * step, e = this.strength, w = wind, g = gravity;
|
||||||
|
float d = (float)Math.pow(this.damping, 60 * step);
|
||||||
|
while (true) {
|
||||||
|
remaining -= step;
|
||||||
|
if (scaleX) {
|
||||||
|
scaleVelocity += (w * c - g * s - scaleOffset * e) * m;
|
||||||
|
scaleOffset += scaleVelocity * step;
|
||||||
|
scaleVelocity *= d;
|
||||||
|
}
|
||||||
|
if (rotateOrShearX) {
|
||||||
|
rotateVelocity += (-0.01f * l * (w * s + g * c) - rotateOffset * e) * m;
|
||||||
|
rotateOffset += rotateVelocity * step;
|
||||||
|
rotateVelocity *= d;
|
||||||
|
if (remaining < step) break;
|
||||||
|
float r = rotateOffset * mix + ca;
|
||||||
|
c = cos(r);
|
||||||
|
s = sin(r);
|
||||||
|
} else if (remaining < step) //
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cx = bone.worldX;
|
this.remaining = remaining;
|
||||||
cy = bone.worldY;
|
|
||||||
|
|
||||||
float strength = this.strength, mass = this.mass * step, wind = this.wind, gravity = this.gravity;
|
|
||||||
float damping = (float)Math.pow(this.damping, 60 * step);
|
|
||||||
while (true) {
|
|
||||||
remaining -= step;
|
|
||||||
if (x) {
|
|
||||||
xVelocity += (100 * wind - xOffset * strength) * mass;
|
|
||||||
xOffset += xVelocity * step;
|
|
||||||
xVelocity *= damping;
|
|
||||||
}
|
|
||||||
if (y) {
|
|
||||||
yVelocity += (-100 * gravity - yOffset * strength) * mass;
|
|
||||||
yOffset += yVelocity * step;
|
|
||||||
yVelocity *= damping;
|
|
||||||
}
|
|
||||||
if (scaleX) {
|
|
||||||
scaleVelocity += (wind * c - gravity * s - scaleOffset * strength) * mass;
|
|
||||||
scaleOffset += scaleVelocity * step;
|
|
||||||
scaleVelocity *= damping;
|
|
||||||
}
|
|
||||||
if (rotateOrShearX) {
|
|
||||||
rotateVelocity += (-0.01f * l * (wind * s + gravity * c) - rotateOffset * strength) * mass;
|
|
||||||
rotateOffset += rotateVelocity * step;
|
|
||||||
rotateVelocity *= damping;
|
|
||||||
if (remaining < step) break;
|
|
||||||
float r = rotateOffset * mix + ca;
|
|
||||||
c = cos(r);
|
|
||||||
s = sin(r);
|
|
||||||
} else if (remaining < step) //
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
cx = bone.worldX;
|
||||||
|
cy = bone.worldY;
|
||||||
break;
|
break;
|
||||||
case pose:
|
case pose:
|
||||||
if (x) bone.worldX += xOffset * mix;
|
if (x) bone.worldX += xOffset * mix;
|
||||||
@ -203,28 +213,28 @@ public class PhysicsConstraint implements Updatable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (rotateOrShearX) {
|
if (rotateOrShearX) {
|
||||||
float r = rotateOffset * mix, ra = bone.a, sin, cos;
|
float r = rotateOffset * mix, a = bone.a, s, c;
|
||||||
if (data.rotate) {
|
if (data.rotate) {
|
||||||
if (data.shearX) {
|
if (data.shearX) {
|
||||||
r *= 0.5f;
|
r *= 0.5f;
|
||||||
sin = sin(r);
|
s = sin(r);
|
||||||
cos = cos(r);
|
c = cos(r);
|
||||||
bone.a = cos * ra - sin * bone.c;
|
bone.a = c * a - s * bone.c;
|
||||||
bone.c = sin * ra + cos * bone.c;
|
bone.c = s * a + c * bone.c;
|
||||||
ra = bone.a;
|
a = bone.a;
|
||||||
} else {
|
} else {
|
||||||
sin = sin(r);
|
s = sin(r);
|
||||||
cos = cos(r);
|
c = cos(r);
|
||||||
}
|
}
|
||||||
float rb = bone.b;
|
float b = bone.b;
|
||||||
bone.b = cos * rb - sin * bone.d;
|
bone.b = c * b - s * bone.d;
|
||||||
bone.d = sin * rb + cos * bone.d;
|
bone.d = s * b + c * bone.d;
|
||||||
} else {
|
} else {
|
||||||
sin = sin(r);
|
s = sin(r);
|
||||||
cos = cos(r);
|
c = cos(r);
|
||||||
}
|
}
|
||||||
bone.a = cos * ra - sin * bone.c;
|
bone.a = c * a - s * bone.c;
|
||||||
bone.c = sin * ra + cos * bone.c;
|
bone.c = s * a + c * bone.c;
|
||||||
}
|
}
|
||||||
if (scaleX) {
|
if (scaleX) {
|
||||||
float s = 1 + scaleOffset * mix;
|
float s = 1 + scaleOffset * mix;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user