fix anchor point bug

This commit is contained in:
yangxiao 2015-01-20 14:10:11 +08:00
parent a05fc8bc8e
commit 2a2330438c
1 changed files with 35 additions and 31 deletions

View File

@ -1703,40 +1703,45 @@ const Mat4& Node::getNodeToParentTransform() const
}
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