1 #include "rive/constraints/transform_constraint.hpp"
2 #include "rive/transform_component.hpp"
3 #include "rive/math/mat2d.hpp"
4 #include "rive/math/math_types.hpp"
8 void TransformConstraint::constrain(TransformComponent* component) {
9 if (m_Target == nullptr) {
13 const Mat2D& transformA = component->worldTransform();
14 Mat2D transformB(m_Target->worldTransform());
15 if (sourceSpace() == TransformSpace::local) {
16 const Mat2D& targetParentWorld = getParentWorld(*m_Target);
19 if (!targetParentWorld.invert(&inverse)) {
22 transformB = inverse * transformB;
24 if (destSpace() == TransformSpace::local) {
25 const Mat2D& targetParentWorld = getParentWorld(*component);
26 transformB = targetParentWorld * transformB;
29 m_ComponentsA = transformA.decompose();
30 m_ComponentsB = transformB.decompose();
32 float angleA = std::fmod(m_ComponentsA.rotation(), math::PI * 2);
33 float angleB = std::fmod(m_ComponentsB.rotation(), math::PI * 2);
34 float diff = angleB - angleA;
35 if (diff > math::PI) {
37 } else if (diff < -math::PI) {
44 m_ComponentsB.rotation(angleA + diff * t);
45 m_ComponentsB.x(m_ComponentsA.x() * ti + m_ComponentsB.x() * t);
46 m_ComponentsB.y(m_ComponentsA.y() * ti + m_ComponentsB.y() * t);
47 m_ComponentsB.scaleX(m_ComponentsA.scaleX() * ti + m_ComponentsB.scaleX() * t);
48 m_ComponentsB.scaleY(m_ComponentsA.scaleY() * ti + m_ComponentsB.scaleY() * t);
49 m_ComponentsB.skew(m_ComponentsA.skew() * ti + m_ComponentsB.skew() * t);
51 component->mutableWorldTransform() = Mat2D::compose(m_ComponentsB);