Skip to content

Commit

Permalink
[cpp] Port: Scale physics constraint limits with skeleton scale. See #…
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Jul 24, 2024
1 parent 92b5b8a commit a5e51af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
28 changes: 15 additions & 13 deletions spine-cpp/spine-cpp/src/spine/PhysicsConstraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,22 +334,24 @@ void PhysicsConstraint::update(Physics physics) {
_ux = bx;
_uy = by;
} 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) {
float u = (_ux - bx) * i;
_xOffset += u > q ? q : u < -q ? -q
_xOffset += u > qx ? qx : u < -qx ? -qx
: u;
_ux = bx;
}
if (y) {
float u = (_uy - by) * i;
_yOffset += u > q ? q : u < -q ? -q
_yOffset += u > qy ? qy : u < -qy ? -qy
: u;
_uy = by;
}
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);
do {
if (x) {
Expand All @@ -372,14 +374,14 @@ void PhysicsConstraint::update(Physics physics) {
if (rotateOrShearX || scaleX) {
float ca = MathUtil::atan2(bone->_c, bone->_a), c, s, mr = 0;
float dx = _cx - bone->_worldX, dy = _cy - bone->_worldY;
if (dx > q)
dx = q;
else if (dx < -q)//
dx = -q;
if (dy > q)
dy = q;
else if (dy < -q)//
dy = -q;
if (dx > qx)
dx = qx;
else if (dx < -qx)//
dx = -qx;
if (dy > qy)
dy = qy;
else if (dy < -qy)//
dy = -qy;
if (rotateOrShearX) {
mr = (_data._rotate + _data._shearX) * mix;
float r = MathUtil::atan2(dy + _ty, dx + _tx) - ca - _rotateOffset * mr;
Expand All @@ -399,8 +401,8 @@ void PhysicsConstraint::update(Physics physics) {
}
a = _remaining;
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 d = MathUtil::pow(_damping, 60 * t);
while (true) {
a -= t;
if (scaleX) {
Expand Down
4 changes: 3 additions & 1 deletion spine-sfml/cpp/example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ void celestialCircus(SkeletonData *skeletonData, Atlas *atlas) {

Skeleton *skeleton = drawable.skeleton;
skeleton->setPosition(320, 480);
skeleton->setScaleX(0.2);
skeleton->setScaleY(0.2);
skeleton->updateWorldTransform(Physics_Update);

drawable.state->setAnimation(0, "swing", true);
Expand Down Expand Up @@ -778,10 +780,10 @@ extern spine::SkeletonRenderer *skeletonRenderer;
int main() {
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(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(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(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);
Expand Down

0 comments on commit a5e51af

Please sign in to comment.