[cpp] port of constraint application oder, see #1896

This commit is contained in:
Mario Zechner 2021-06-07 16:45:14 +02:00
parent 4715608108
commit 857b2b64d8
9 changed files with 29 additions and 40 deletions

View File

@ -243,10 +243,6 @@ namespace spine {
/// Returns the magnitide (always positive) of the world scale Y.
float getWorldScaleY();
bool isAppliedValid();
void setAppliedValid(bool valid);
bool isActive();
void setActive(bool inValue);
@ -260,7 +256,6 @@ namespace spine {
Vector<Bone *> _children;
float _x, _y, _rotation, _scaleX, _scaleY, _shearX, _shearY;
float _ax, _ay, _arotation, _ascaleX, _ascaleY, _ashearX, _ashearY;
bool _appliedValid;
float _a, _b, _worldX;
float _c, _d, _worldY;
bool _sorted;

View File

@ -68,7 +68,6 @@ Bone::Bone(BoneData &data, Skeleton &skeleton, Bone *parent) : Updatable(),
_ascaleY(0),
_ashearX(0),
_ashearY(0),
_appliedValid(false),
_a(1),
_b(0),
_worldX(0),
@ -81,7 +80,7 @@ Bone::Bone(BoneData &data, Skeleton &skeleton, Bone *parent) : Updatable(),
}
void Bone::update() {
updateWorldTransform(_x, _y, _rotation, _scaleX, _scaleY, _shearX, _shearY);
updateWorldTransform(_ax, _ay, _arotation, _ascaleX, _ascaleY, _ashearX, _ashearY);
}
void Bone::updateWorldTransform() {
@ -101,7 +100,6 @@ Bone::updateWorldTransform(float x, float y, float rotation, float scaleX, float
_ascaleY = scaleY;
_ashearX = shearX;
_ashearY = shearY;
_appliedValid = true;
if (!parent) { /* Root bone. */
float rotationY = rotation + 90 + shearY;
@ -267,8 +265,6 @@ void Bone::rotateWorld(float degrees) {
_b = cos * b - sin * d;
_c = sin * a + cos * c;
_d = sin * b + cos * d;
_appliedValid = false;
}
float Bone::getWorldToLocalRotationX() {
@ -495,17 +491,8 @@ float Bone::getWorldScaleY() {
return MathUtil::sqrt(_b * _b + _d * _d);
}
bool Bone::isAppliedValid() {
return _appliedValid;
}
void Bone::setAppliedValid(bool valid) {
_appliedValid = valid;
}
void Bone::updateAppliedTransform() {
Bone *parent = _parent;
_appliedValid = 1;
if (!parent) {
_ax = _worldX;
_ay = _worldY;

View File

@ -49,7 +49,6 @@ IkConstraint::apply(Bone &bone, float targetX, float targetY, bool compress, boo
float pa = p->_a, pb = p->_b, pc = p->_c, pd = p->_d;
float rotationIK = -bone._ashearX - bone._arotation;
float tx = 0, ty = 0;
if (!bone._appliedValid) bone.updateAppliedTransform();
switch (bone._data.getTransformMode()) {
case TransformMode_OnlyTranslation:
@ -105,8 +104,6 @@ IkConstraint::apply(Bone &parent, Bone &child, float targetX, float targetY, int
Bone *pp = parent.getParent();
float tx, ty, dx, dy, dd, l1, l2, a1, a2, r, td, sd, p;
float id, x, y;
if (!parent._appliedValid) parent.updateAppliedTransform();
if (!child._appliedValid) child.updateAppliedTransform();
px = parent._ax;
py = parent._ay;
psx = parent._ascaleX;

View File

@ -220,7 +220,7 @@ void PathConstraint::update() {
bone._d = sin * b + cos * d;
}
bone._appliedValid = false;
bone.updateAppliedTransform();
}
}

View File

@ -212,6 +212,17 @@ void Skeleton::printUpdateCache() {
}
void Skeleton::updateWorldTransform() {
for (size_t i = 0, n = _bones.size(); i < n; i++) {
Bone *bone = _bones[i];
bone->_ax = bone->_x;
bone->_ay = bone->_y;
bone->_arotation = bone->_rotation;
bone->_ascaleX = bone->_scaleX;
bone->_ascaleY = bone->_scaleY;
bone->_ashearX = bone->_shearX;
bone->_ashearY = bone->_shearY;
}
for (size_t i = 0, n = _updateCache.size(); i < n; ++i) {
_updateCache[i]->update();
}

View File

@ -37,6 +37,7 @@
#include <spine/Bone.h>
#include <spine/Skeleton.h>
#include <spine/Attachment.h>
#include <spine/VertexAttachment.h>
using namespace spine;
@ -99,9 +100,17 @@ void Slot::setAttachment(Attachment *inValue) {
return;
}
if (inValue && _attachment) {
if (!(inValue->getRTTI().instanceOf(VertexAttachment::rtti)) ||
!(_attachment->getRTTI().instanceOf(VertexAttachment::rtti))
|| (static_cast<VertexAttachment *>(inValue)->getDeformAttachment() !=
(static_cast<VertexAttachment *>(_attachment)->getDeformAttachment()))) {
_deform.clear();
}
}
_attachment = inValue;
_attachmentTime = _skeleton.getTime();
_deform.clear();
}
int Slot::getAttachmentState() {

View File

@ -213,7 +213,7 @@ void TransformConstraint::applyAbsoluteWorld() {
bone._d = MathUtil::sin(r) * s;
}
bone._appliedValid = false;
bone.updateAppliedTransform();
}
}
@ -276,21 +276,18 @@ void TransformConstraint::applyRelativeWorld() {
bone._d = MathUtil::sin(r) * s;
}
bone._appliedValid = false;
bone.updateAppliedTransform();
}
}
void TransformConstraint::applyAbsoluteLocal() {
float mixRotate = _mixRotate, mixX = _mixX, mixY = _mixY, mixScaleX = _mixScaleX, mixScaleY = _mixScaleY, mixShearY = _mixShearY;
Bone &target = *_target;
if (!target._appliedValid) target.updateAppliedTransform();
for (size_t i = 0; i < _bones.size(); ++i) {
Bone *item = _bones[i];
Bone &bone = *item;
if (!bone._appliedValid) bone.updateAppliedTransform();
float rotation = bone._arotation;
if (mixRotate != 0) {
float r = target._arotation - rotation + _data._offsetRotation;
@ -322,14 +319,11 @@ void TransformConstraint::applyAbsoluteLocal() {
void TransformConstraint::applyRelativeLocal() {
float mixRotate = _mixRotate, mixX = _mixX, mixY = _mixY, mixScaleX = _mixScaleX, mixScaleY = _mixScaleY, mixShearY = _mixShearY;
Bone &target = *_target;
if (!target._appliedValid) target.updateAppliedTransform();
for (size_t i = 0; i < _bones.size(); ++i) {
Bone *item = _bones[i];
Bone &bone = *item;
if (!bone._appliedValid) bone.updateAppliedTransform();
float rotation = bone._arotation + (target._arotation + _data._offsetRotation) * mixRotate;
float x = bone._ax + (target._ax + _data._offsetX) * mixX;
float y = bone._ay + (target._ay + _data._offsetY) * mixY;

View File

@ -252,7 +252,6 @@ void goblins (spSkeletonData* skeletonData, spAtlas* atlas) {
spSkeleton_updateWorldTransform(skeleton);
spAnimationState_setAnimationByName(drawable->state, 0, "walk", true);
drawable->update(0.3f);
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - goblins");
window.setFramerateLimit(60);
@ -262,10 +261,10 @@ void goblins (spSkeletonData* skeletonData, spAtlas* atlas) {
while (window.pollEvent(event))
if (event.type == sf::Event::Closed) window.close();
// float delta = deltaClock.getElapsedTime().asSeconds();
float delta = deltaClock.getElapsedTime().asSeconds();
deltaClock.restart();
drawable->update(0);
drawable->update(delta);
window.clear();
window.draw(*drawable);

View File

@ -223,7 +223,6 @@ void ikDemo (SkeletonData* skeletonData, Atlas* atlas) {
crosshair->getParent()->worldToLocal(mouseCoords.x, mouseCoords.y, boneCoordsX, boneCoordsY);
crosshair->setX(boneCoordsX);
crosshair->setY(boneCoordsY);
crosshair->setAppliedValid(false);
// Calculate final world transform with the
// crosshair bone set to the mouse cursor
@ -251,8 +250,6 @@ void goblins (SkeletonData* skeletonData, Atlas* atlas) {
drawable.state->setAnimation(0, "walk", true);
drawable.update(0.3f);
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - goblins");
window.setFramerateLimit(60);
sf::Event event;
@ -261,10 +258,10 @@ void goblins (SkeletonData* skeletonData, Atlas* atlas) {
while (window.pollEvent(event))
if (event.type == sf::Event::Closed) window.close();
// float delta = deltaClock.getElapsedTime().asSeconds();
float delta = deltaClock.getElapsedTime().asSeconds();
deltaClock.restart();
drawable.update(0);
drawable.update(delta);
window.clear();
window.draw(drawable);