mirror of https://github.com/axmolengine/axmol.git
fix anchor point bug
This commit is contained in:
parent
a05fc8bc8e
commit
2a2330438c
|
@ -1695,48 +1695,53 @@ const Mat4& Node::getNodeToParentTransform() const
|
|||
float x = _position.x;
|
||||
float y = _position.y;
|
||||
float z = _positionZ;
|
||||
|
||||
|
||||
if (_ignoreAnchorPointForPosition)
|
||||
{
|
||||
x += _anchorPointInPoints.x;
|
||||
y += _anchorPointInPoints.y;
|
||||
}
|
||||
|
||||
|
||||
bool needsSkewMatrix = ( _skewX || _skewY );
|
||||
// Rotation values
|
||||
// Change rotation code to handle X and Y
|
||||
// If we skew with the exact same value for both x and y then we're simply just rotating
|
||||
float cx = 1, sx = 0, cy = 1, sy = 0;
|
||||
if (_rotationZ_X != _rotationZ_Y || (! needsSkewMatrix && !_anchorPointInPoints.equals(Vec2::ZERO)))
|
||||
{
|
||||
float radiansX = -CC_DEGREES_TO_RADIANS(_rotationZ_X);
|
||||
float radiansY = -CC_DEGREES_TO_RADIANS(_rotationZ_Y);
|
||||
cx = cosf(radiansX);
|
||||
sx = sinf(radiansX);
|
||||
cy = cosf(radiansY);
|
||||
sy = sinf(radiansY);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Vec2 anchorPoint(_anchorPointInPoints.x * _scaleX, _anchorPointInPoints.y * _scaleY);
|
||||
|
||||
// optimization:
|
||||
// inline anchor point calculation if skew is not needed
|
||||
// Adjusted transform calculation for rotational skew
|
||||
|
||||
// caculate real position
|
||||
if (! needsSkewMatrix && !_anchorPointInPoints.equals(Vec2::ZERO))
|
||||
{
|
||||
x += cy * -anchorPoint.x + -sx * -anchorPoint.y;
|
||||
y += sy * -anchorPoint.x + cx * -anchorPoint.y;
|
||||
x += -anchorPoint.x;
|
||||
y += -anchorPoint.y;
|
||||
}
|
||||
|
||||
// Build Transform Matrix
|
||||
// Adjusted transform calculation for rotational skew
|
||||
|
||||
// Build Transform Matrix = translation * rotation * scale
|
||||
Mat4 translation;
|
||||
//move to anchor point first, then rotate
|
||||
Mat4::createTranslation(x + anchorPoint.x, y + anchorPoint.y, z, &translation);
|
||||
|
||||
Mat4::createRotation(_rotationQuat, &_transform);
|
||||
|
||||
if (_rotationZ_X != _rotationZ_Y)
|
||||
{
|
||||
// Rotation values
|
||||
// Change rotation code to handle X and Y
|
||||
// If we skew with the exact same value for both x and y then we're simply just rotating
|
||||
float radiansX = -CC_DEGREES_TO_RADIANS(_rotationZ_X);
|
||||
float radiansY = -CC_DEGREES_TO_RADIANS(_rotationZ_Y);
|
||||
float cx = cosf(radiansX);
|
||||
float sx = sinf(radiansX);
|
||||
float cy = cosf(radiansY);
|
||||
float sy = sinf(radiansY);
|
||||
|
||||
float m0 = _transform.m[0], m1 = _transform.m[1], m4 = _transform.m[4], m5 = _transform.m[5], m8 = _transform.m[8], m9 = _transform.m[9];
|
||||
_transform.m[0] = cy * m0 - sx * m1, _transform.m[4] = cy * m4 - sx * m5, _transform.m[8] = cy * m8 - sx * m9;
|
||||
_transform.m[1] = sy * m0 + cx * m1, _transform.m[5] = sy * m4 + cx * m5, _transform.m[9] = sy * m8 + cx * m9;
|
||||
}
|
||||
_transform = translation * _transform;
|
||||
//move by (-anchorPoint.x, -anchorPoint.y, 0) after rotation
|
||||
_transform.translate(-anchorPoint.x, -anchorPoint.y, 0);
|
||||
|
||||
|
||||
if (_scaleX != 1.f)
|
||||
{
|
||||
_transform.m[0] *= _scaleX, _transform.m[1] *= _scaleX, _transform.m[2] *= _scaleX;
|
||||
|
@ -1749,7 +1754,6 @@ const Mat4& Node::getNodeToParentTransform() const
|
|||
{
|
||||
_transform.m[8] *= _scaleZ, _transform.m[9] *= _scaleZ, _transform.m[10] *= _scaleZ;
|
||||
}
|
||||
_transform.m[12] = x, _transform.m[13] = y, _transform.m[14] = z;
|
||||
|
||||
// FIXME:: Try to inline skew
|
||||
// If skew is needed, apply skew and then anchor point
|
||||
|
@ -1763,9 +1767,9 @@ const Mat4& Node::getNodeToParentTransform() const
|
|||
0, 0, 0, 1
|
||||
};
|
||||
Mat4 skewMatrix(skewMatArray);
|
||||
|
||||
|
||||
_transform = _transform * skewMatrix;
|
||||
|
||||
|
||||
// adjust anchor point
|
||||
if (!_anchorPointInPoints.equals(Vec2::ZERO))
|
||||
{
|
||||
|
@ -1775,15 +1779,15 @@ const Mat4& Node::getNodeToParentTransform() const
|
|||
_transform.m[13] += _transform.m[1] * -_anchorPointInPoints.x + _transform.m[5] * -_anchorPointInPoints.y;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (_useAdditionalTransform)
|
||||
{
|
||||
_transform = _transform * _additionalTransform;
|
||||
}
|
||||
|
||||
|
||||
_transformDirty = false;
|
||||
}
|
||||
|
||||
|
||||
return _transform;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue