[cpp] Port: Scale physics constraint limits with skeleton scale. See #2576

This commit is contained in:
Mario Zechner 2024-07-24 14:32:13 +02:00
parent 92b5b8a1ee
commit a5e51afb57
2 changed files with 18 additions and 14 deletions

View File

@ -334,22 +334,24 @@ void PhysicsConstraint::update(Physics physics) {
_ux = bx; _ux = bx;
_uy = by; _uy = by;
} else { } else {
float a = _remaining, i = _inertia, q = _data._limit * delta, t = _data._step, f = _skeleton.getData()->getReferenceScale(), d = -1; float a = _remaining, i = _inertia, t = _data._step, f = _skeleton.getData()->getReferenceScale();
float qx = _data._limit * delta, qy = qx * MathUtil::abs(_skeleton.getScaleX());
qx *= MathUtil::abs(_skeleton.getScaleY());
if (x || y) { if (x || y) {
if (x) { if (x) {
float u = (_ux - bx) * i; float u = (_ux - bx) * i;
_xOffset += u > q ? q : u < -q ? -q _xOffset += u > qx ? qx : u < -qx ? -qx
: u; : u;
_ux = bx; _ux = bx;
} }
if (y) { if (y) {
float u = (_uy - by) * i; float u = (_uy - by) * i;
_yOffset += u > q ? q : u < -q ? -q _yOffset += u > qy ? qy : u < -qy ? -qy
: u; : u;
_uy = by; _uy = by;
} }
if (a >= t) { if (a >= t) {
d = MathUtil::pow(_damping, 60 * t); float d = MathUtil::pow(_damping, 60 * t);
float m = _massInverse * t, e = _strength, w = _wind * f, g = _gravity * f * (Bone::yDown ? -1 : 1); float m = _massInverse * t, e = _strength, w = _wind * f, g = _gravity * f * (Bone::yDown ? -1 : 1);
do { do {
if (x) { if (x) {
@ -372,14 +374,14 @@ void PhysicsConstraint::update(Physics physics) {
if (rotateOrShearX || scaleX) { if (rotateOrShearX || scaleX) {
float ca = MathUtil::atan2(bone->_c, bone->_a), c, s, mr = 0; float ca = MathUtil::atan2(bone->_c, bone->_a), c, s, mr = 0;
float dx = _cx - bone->_worldX, dy = _cy - bone->_worldY; float dx = _cx - bone->_worldX, dy = _cy - bone->_worldY;
if (dx > q) if (dx > qx)
dx = q; dx = qx;
else if (dx < -q)// else if (dx < -qx)//
dx = -q; dx = -qx;
if (dy > q) if (dy > qy)
dy = q; dy = qy;
else if (dy < -q)// else if (dy < -qy)//
dy = -q; dy = -qy;
if (rotateOrShearX) { if (rotateOrShearX) {
mr = (_data._rotate + _data._shearX) * mix; mr = (_data._rotate + _data._shearX) * mix;
float r = MathUtil::atan2(dy + _ty, dx + _tx) - ca - _rotateOffset * mr; float r = MathUtil::atan2(dy + _ty, dx + _tx) - ca - _rotateOffset * mr;
@ -399,8 +401,8 @@ void PhysicsConstraint::update(Physics physics) {
} }
a = _remaining; a = _remaining;
if (a >= t) { if (a >= t) {
if (d == -1) d = MathUtil::pow(_damping, 60 * t);
float m = _massInverse * t, e = _strength, w = _wind, g = _gravity * (Bone::yDown ? -1 : 1), h = l / f; float m = _massInverse * t, e = _strength, w = _wind, g = _gravity * (Bone::yDown ? -1 : 1), h = l / f;
float d = MathUtil::pow(_damping, 60 * t);
while (true) { while (true) {
a -= t; a -= t;
if (scaleX) { if (scaleX) {

View File

@ -626,6 +626,8 @@ void celestialCircus(SkeletonData *skeletonData, Atlas *atlas) {
Skeleton *skeleton = drawable.skeleton; Skeleton *skeleton = drawable.skeleton;
skeleton->setPosition(320, 480); skeleton->setPosition(320, 480);
skeleton->setScaleX(0.2);
skeleton->setScaleY(0.2);
skeleton->updateWorldTransform(Physics_Update); skeleton->updateWorldTransform(Physics_Update);
drawable.state->setAnimation(0, "swing", true); drawable.state->setAnimation(0, "swing", true);
@ -778,10 +780,10 @@ extern spine::SkeletonRenderer *skeletonRenderer;
int main() { int main() {
SpineExtension::setInstance(&dbgExtension); SpineExtension::setInstance(&dbgExtension);
testcase(celestialCircus, "data/celestial-circus-pro.json", "data/celestial-circus-pro.skel", "data/celestial-circus-pma.atlas", 1);
testcase(mixAndMatch, "data/mix-and-match-pro.json", "data/mix-and-match-pro.skel", "data/mix-and-match-pma.atlas", 0.5f); testcase(mixAndMatch, "data/mix-and-match-pro.json", "data/mix-and-match-pro.skel", "data/mix-and-match-pma.atlas", 0.5f);
testcase(cloudpot, "data/cloud-pot.json", "data/cloud-pot.skel", "data/cloud-pot-pma.atlas", 0.2); testcase(cloudpot, "data/cloud-pot.json", "data/cloud-pot.skel", "data/cloud-pot-pma.atlas", 0.2);
testcase(sack, "data/sack-pro.json", "data/sack-pro.skel", "data/sack-pma.atlas", 0.2f); testcase(sack, "data/sack-pro.json", "data/sack-pro.skel", "data/sack-pma.atlas", 0.2f);
testcase(celestialCircus, "data/celestial-circus-pro.json", "data/celestial-circus-pro.skel", "data/celestial-circus-pma.atlas", 0.2f);
testcase(snowglobe, "data/snowglobe-pro.json", "data/snowglobe-pro.skel", "data/snowglobe-pma.atlas", 0.2f); testcase(snowglobe, "data/snowglobe-pro.json", "data/snowglobe-pro.skel", "data/snowglobe-pma.atlas", 0.2f);
testcase(dragon, "data/dragon-ess.json", "data/dragon-ess.skel", "data/dragon-pma.atlas", 0.6f); testcase(dragon, "data/dragon-ess.json", "data/dragon-ess.skel", "data/dragon-pma.atlas", 0.6f);
testcase(spineboy, "data/spineboy-pro.json", "data/spineboy-pro.skel", "data/spineboy-pma.atlas", 0.62f); testcase(spineboy, "data/spineboy-pro.json", "data/spineboy-pro.skel", "data/spineboy-pma.atlas", 0.62f);