mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
[cpp] port of constraint application oder, see #1896
This commit is contained in:
parent
4715608108
commit
857b2b64d8
@ -243,10 +243,6 @@ namespace spine {
|
|||||||
/// Returns the magnitide (always positive) of the world scale Y.
|
/// Returns the magnitide (always positive) of the world scale Y.
|
||||||
float getWorldScaleY();
|
float getWorldScaleY();
|
||||||
|
|
||||||
bool isAppliedValid();
|
|
||||||
|
|
||||||
void setAppliedValid(bool valid);
|
|
||||||
|
|
||||||
bool isActive();
|
bool isActive();
|
||||||
|
|
||||||
void setActive(bool inValue);
|
void setActive(bool inValue);
|
||||||
@ -260,7 +256,6 @@ namespace spine {
|
|||||||
Vector<Bone *> _children;
|
Vector<Bone *> _children;
|
||||||
float _x, _y, _rotation, _scaleX, _scaleY, _shearX, _shearY;
|
float _x, _y, _rotation, _scaleX, _scaleY, _shearX, _shearY;
|
||||||
float _ax, _ay, _arotation, _ascaleX, _ascaleY, _ashearX, _ashearY;
|
float _ax, _ay, _arotation, _ascaleX, _ascaleY, _ashearX, _ashearY;
|
||||||
bool _appliedValid;
|
|
||||||
float _a, _b, _worldX;
|
float _a, _b, _worldX;
|
||||||
float _c, _d, _worldY;
|
float _c, _d, _worldY;
|
||||||
bool _sorted;
|
bool _sorted;
|
||||||
|
|||||||
@ -68,7 +68,6 @@ Bone::Bone(BoneData &data, Skeleton &skeleton, Bone *parent) : Updatable(),
|
|||||||
_ascaleY(0),
|
_ascaleY(0),
|
||||||
_ashearX(0),
|
_ashearX(0),
|
||||||
_ashearY(0),
|
_ashearY(0),
|
||||||
_appliedValid(false),
|
|
||||||
_a(1),
|
_a(1),
|
||||||
_b(0),
|
_b(0),
|
||||||
_worldX(0),
|
_worldX(0),
|
||||||
@ -81,7 +80,7 @@ Bone::Bone(BoneData &data, Skeleton &skeleton, Bone *parent) : Updatable(),
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Bone::update() {
|
void Bone::update() {
|
||||||
updateWorldTransform(_x, _y, _rotation, _scaleX, _scaleY, _shearX, _shearY);
|
updateWorldTransform(_ax, _ay, _arotation, _ascaleX, _ascaleY, _ashearX, _ashearY);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bone::updateWorldTransform() {
|
void Bone::updateWorldTransform() {
|
||||||
@ -101,7 +100,6 @@ Bone::updateWorldTransform(float x, float y, float rotation, float scaleX, float
|
|||||||
_ascaleY = scaleY;
|
_ascaleY = scaleY;
|
||||||
_ashearX = shearX;
|
_ashearX = shearX;
|
||||||
_ashearY = shearY;
|
_ashearY = shearY;
|
||||||
_appliedValid = true;
|
|
||||||
|
|
||||||
if (!parent) { /* Root bone. */
|
if (!parent) { /* Root bone. */
|
||||||
float rotationY = rotation + 90 + shearY;
|
float rotationY = rotation + 90 + shearY;
|
||||||
@ -267,8 +265,6 @@ void Bone::rotateWorld(float degrees) {
|
|||||||
_b = cos * b - sin * d;
|
_b = cos * b - sin * d;
|
||||||
_c = sin * a + cos * c;
|
_c = sin * a + cos * c;
|
||||||
_d = sin * b + cos * d;
|
_d = sin * b + cos * d;
|
||||||
|
|
||||||
_appliedValid = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float Bone::getWorldToLocalRotationX() {
|
float Bone::getWorldToLocalRotationX() {
|
||||||
@ -495,17 +491,8 @@ float Bone::getWorldScaleY() {
|
|||||||
return MathUtil::sqrt(_b * _b + _d * _d);
|
return MathUtil::sqrt(_b * _b + _d * _d);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Bone::isAppliedValid() {
|
|
||||||
return _appliedValid;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Bone::setAppliedValid(bool valid) {
|
|
||||||
_appliedValid = valid;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Bone::updateAppliedTransform() {
|
void Bone::updateAppliedTransform() {
|
||||||
Bone *parent = _parent;
|
Bone *parent = _parent;
|
||||||
_appliedValid = 1;
|
|
||||||
if (!parent) {
|
if (!parent) {
|
||||||
_ax = _worldX;
|
_ax = _worldX;
|
||||||
_ay = _worldY;
|
_ay = _worldY;
|
||||||
|
|||||||
@ -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 pa = p->_a, pb = p->_b, pc = p->_c, pd = p->_d;
|
||||||
float rotationIK = -bone._ashearX - bone._arotation;
|
float rotationIK = -bone._ashearX - bone._arotation;
|
||||||
float tx = 0, ty = 0;
|
float tx = 0, ty = 0;
|
||||||
if (!bone._appliedValid) bone.updateAppliedTransform();
|
|
||||||
|
|
||||||
switch (bone._data.getTransformMode()) {
|
switch (bone._data.getTransformMode()) {
|
||||||
case TransformMode_OnlyTranslation:
|
case TransformMode_OnlyTranslation:
|
||||||
@ -105,8 +104,6 @@ IkConstraint::apply(Bone &parent, Bone &child, float targetX, float targetY, int
|
|||||||
Bone *pp = parent.getParent();
|
Bone *pp = parent.getParent();
|
||||||
float tx, ty, dx, dy, dd, l1, l2, a1, a2, r, td, sd, p;
|
float tx, ty, dx, dy, dd, l1, l2, a1, a2, r, td, sd, p;
|
||||||
float id, x, y;
|
float id, x, y;
|
||||||
if (!parent._appliedValid) parent.updateAppliedTransform();
|
|
||||||
if (!child._appliedValid) child.updateAppliedTransform();
|
|
||||||
px = parent._ax;
|
px = parent._ax;
|
||||||
py = parent._ay;
|
py = parent._ay;
|
||||||
psx = parent._ascaleX;
|
psx = parent._ascaleX;
|
||||||
|
|||||||
@ -220,7 +220,7 @@ void PathConstraint::update() {
|
|||||||
bone._d = sin * b + cos * d;
|
bone._d = sin * b + cos * d;
|
||||||
}
|
}
|
||||||
|
|
||||||
bone._appliedValid = false;
|
bone.updateAppliedTransform();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -212,6 +212,17 @@ void Skeleton::printUpdateCache() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Skeleton::updateWorldTransform() {
|
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) {
|
for (size_t i = 0, n = _updateCache.size(); i < n; ++i) {
|
||||||
_updateCache[i]->update();
|
_updateCache[i]->update();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,6 +37,7 @@
|
|||||||
#include <spine/Bone.h>
|
#include <spine/Bone.h>
|
||||||
#include <spine/Skeleton.h>
|
#include <spine/Skeleton.h>
|
||||||
#include <spine/Attachment.h>
|
#include <spine/Attachment.h>
|
||||||
|
#include <spine/VertexAttachment.h>
|
||||||
|
|
||||||
using namespace spine;
|
using namespace spine;
|
||||||
|
|
||||||
@ -99,9 +100,17 @@ void Slot::setAttachment(Attachment *inValue) {
|
|||||||
return;
|
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;
|
_attachment = inValue;
|
||||||
_attachmentTime = _skeleton.getTime();
|
_attachmentTime = _skeleton.getTime();
|
||||||
_deform.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Slot::getAttachmentState() {
|
int Slot::getAttachmentState() {
|
||||||
|
|||||||
@ -213,7 +213,7 @@ void TransformConstraint::applyAbsoluteWorld() {
|
|||||||
bone._d = MathUtil::sin(r) * s;
|
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._d = MathUtil::sin(r) * s;
|
||||||
}
|
}
|
||||||
|
|
||||||
bone._appliedValid = false;
|
bone.updateAppliedTransform();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TransformConstraint::applyAbsoluteLocal() {
|
void TransformConstraint::applyAbsoluteLocal() {
|
||||||
float mixRotate = _mixRotate, mixX = _mixX, mixY = _mixY, mixScaleX = _mixScaleX, mixScaleY = _mixScaleY, mixShearY = _mixShearY;
|
float mixRotate = _mixRotate, mixX = _mixX, mixY = _mixY, mixScaleX = _mixScaleX, mixScaleY = _mixScaleY, mixShearY = _mixShearY;
|
||||||
Bone &target = *_target;
|
Bone &target = *_target;
|
||||||
if (!target._appliedValid) target.updateAppliedTransform();
|
|
||||||
|
|
||||||
for (size_t i = 0; i < _bones.size(); ++i) {
|
for (size_t i = 0; i < _bones.size(); ++i) {
|
||||||
Bone *item = _bones[i];
|
Bone *item = _bones[i];
|
||||||
Bone &bone = *item;
|
Bone &bone = *item;
|
||||||
|
|
||||||
if (!bone._appliedValid) bone.updateAppliedTransform();
|
|
||||||
|
|
||||||
float rotation = bone._arotation;
|
float rotation = bone._arotation;
|
||||||
if (mixRotate != 0) {
|
if (mixRotate != 0) {
|
||||||
float r = target._arotation - rotation + _data._offsetRotation;
|
float r = target._arotation - rotation + _data._offsetRotation;
|
||||||
@ -322,14 +319,11 @@ void TransformConstraint::applyAbsoluteLocal() {
|
|||||||
void TransformConstraint::applyRelativeLocal() {
|
void TransformConstraint::applyRelativeLocal() {
|
||||||
float mixRotate = _mixRotate, mixX = _mixX, mixY = _mixY, mixScaleX = _mixScaleX, mixScaleY = _mixScaleY, mixShearY = _mixShearY;
|
float mixRotate = _mixRotate, mixX = _mixX, mixY = _mixY, mixScaleX = _mixScaleX, mixScaleY = _mixScaleY, mixShearY = _mixShearY;
|
||||||
Bone &target = *_target;
|
Bone &target = *_target;
|
||||||
if (!target._appliedValid) target.updateAppliedTransform();
|
|
||||||
|
|
||||||
for (size_t i = 0; i < _bones.size(); ++i) {
|
for (size_t i = 0; i < _bones.size(); ++i) {
|
||||||
Bone *item = _bones[i];
|
Bone *item = _bones[i];
|
||||||
Bone &bone = *item;
|
Bone &bone = *item;
|
||||||
|
|
||||||
if (!bone._appliedValid) bone.updateAppliedTransform();
|
|
||||||
|
|
||||||
float rotation = bone._arotation + (target._arotation + _data._offsetRotation) * mixRotate;
|
float rotation = bone._arotation + (target._arotation + _data._offsetRotation) * mixRotate;
|
||||||
float x = bone._ax + (target._ax + _data._offsetX) * mixX;
|
float x = bone._ax + (target._ax + _data._offsetX) * mixX;
|
||||||
float y = bone._ay + (target._ay + _data._offsetY) * mixY;
|
float y = bone._ay + (target._ay + _data._offsetY) * mixY;
|
||||||
|
|||||||
@ -252,7 +252,6 @@ void goblins (spSkeletonData* skeletonData, spAtlas* atlas) {
|
|||||||
spSkeleton_updateWorldTransform(skeleton);
|
spSkeleton_updateWorldTransform(skeleton);
|
||||||
|
|
||||||
spAnimationState_setAnimationByName(drawable->state, 0, "walk", true);
|
spAnimationState_setAnimationByName(drawable->state, 0, "walk", true);
|
||||||
drawable->update(0.3f);
|
|
||||||
|
|
||||||
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - goblins");
|
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - goblins");
|
||||||
window.setFramerateLimit(60);
|
window.setFramerateLimit(60);
|
||||||
@ -262,10 +261,10 @@ void goblins (spSkeletonData* skeletonData, spAtlas* atlas) {
|
|||||||
while (window.pollEvent(event))
|
while (window.pollEvent(event))
|
||||||
if (event.type == sf::Event::Closed) window.close();
|
if (event.type == sf::Event::Closed) window.close();
|
||||||
|
|
||||||
// float delta = deltaClock.getElapsedTime().asSeconds();
|
float delta = deltaClock.getElapsedTime().asSeconds();
|
||||||
deltaClock.restart();
|
deltaClock.restart();
|
||||||
|
|
||||||
drawable->update(0);
|
drawable->update(delta);
|
||||||
|
|
||||||
window.clear();
|
window.clear();
|
||||||
window.draw(*drawable);
|
window.draw(*drawable);
|
||||||
|
|||||||
@ -223,7 +223,6 @@ void ikDemo (SkeletonData* skeletonData, Atlas* atlas) {
|
|||||||
crosshair->getParent()->worldToLocal(mouseCoords.x, mouseCoords.y, boneCoordsX, boneCoordsY);
|
crosshair->getParent()->worldToLocal(mouseCoords.x, mouseCoords.y, boneCoordsX, boneCoordsY);
|
||||||
crosshair->setX(boneCoordsX);
|
crosshair->setX(boneCoordsX);
|
||||||
crosshair->setY(boneCoordsY);
|
crosshair->setY(boneCoordsY);
|
||||||
crosshair->setAppliedValid(false);
|
|
||||||
|
|
||||||
// Calculate final world transform with the
|
// Calculate final world transform with the
|
||||||
// crosshair bone set to the mouse cursor
|
// crosshair bone set to the mouse cursor
|
||||||
@ -251,8 +250,6 @@ void goblins (SkeletonData* skeletonData, Atlas* atlas) {
|
|||||||
|
|
||||||
drawable.state->setAnimation(0, "walk", true);
|
drawable.state->setAnimation(0, "walk", true);
|
||||||
|
|
||||||
drawable.update(0.3f);
|
|
||||||
|
|
||||||
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - goblins");
|
sf::RenderWindow window(sf::VideoMode(640, 640), "Spine SFML - goblins");
|
||||||
window.setFramerateLimit(60);
|
window.setFramerateLimit(60);
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
@ -261,10 +258,10 @@ void goblins (SkeletonData* skeletonData, Atlas* atlas) {
|
|||||||
while (window.pollEvent(event))
|
while (window.pollEvent(event))
|
||||||
if (event.type == sf::Event::Closed) window.close();
|
if (event.type == sf::Event::Closed) window.close();
|
||||||
|
|
||||||
// float delta = deltaClock.getElapsedTime().asSeconds();
|
float delta = deltaClock.getElapsedTime().asSeconds();
|
||||||
deltaClock.restart();
|
deltaClock.restart();
|
||||||
|
|
||||||
drawable.update(0);
|
drawable.update(delta);
|
||||||
|
|
||||||
window.clear();
|
window.clear();
|
||||||
window.draw(drawable);
|
window.draw(drawable);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user