Migrated code to Matrix4

This commit is contained in:
Ricardo Quesada 2013-12-10 11:07:15 -08:00
parent 0222c6544e
commit 23222923bf
14 changed files with 123 additions and 52 deletions

View File

@ -1245,12 +1245,12 @@ const kmMat4& Node::getNodeToParentTransform() const
// If skew is needed, apply skew and then anchor point
if (needsSkewMatrix)
{
kmMat4 skewMtrix = { 1, tanf(CC_DEGREES_TO_RADIANS(_skewY)), 0, 0,
kmMat4 skewMatrix = { 1, tanf(CC_DEGREES_TO_RADIANS(_skewY)), 0, 0,
tanf(CC_DEGREES_TO_RADIANS(_skewX)),1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
kmMat4Multiply(&_transform, &skewMtrix, &_transform);
kmMat4Multiply(&_transform, &skewMatrix, &_transform);
// adjust anchor point
if (!_anchorPointInPoints.equals(Point::ZERO))

View File

@ -149,6 +149,14 @@ AffineTransform AffineTransformConcat(const AffineTransform& t1, const AffineTra
t1.tx * t2.b + t1.ty * t2.d + t2.ty); //ty
}
kmMat4 TransformConcat(const kmMat4& t1, const kmMat4& t2)
{
kmMat4 ret;
kmMat4Multiply(&ret, &t1, &t2);
return ret;
}
/* Return true if `t1' and `t2' are equal, false otherwise. */
bool AffineTransformEqualToTransform(const AffineTransform& t1, const AffineTransform& t2)
{

View File

@ -60,6 +60,8 @@ CC_DLL AffineTransform AffineTransformConcat(const AffineTransform& t1, const Af
CC_DLL bool AffineTransformEqualToTransform(const AffineTransform& t1, const AffineTransform& t2);
CC_DLL AffineTransform AffineTransformInvert(const AffineTransform& t);
kmMat4 TransformConcat(const kmMat4& t1, const kmMat4& t2);
extern CC_DLL const AffineTransform AffineTransformIdentity;
NS_CC_END

View File

@ -340,7 +340,7 @@ Dictionary *Armature::getBoneDic() const
return _boneDic;
}
const AffineTransform& Armature::getNodeToParentTransform() const
const kmMat4& Armature::getNodeToParentTransform() const
{
if (_transformDirty)
{
@ -388,29 +388,33 @@ const AffineTransform& Armature::getNodeToParentTransform() const
// Build Transform Matrix
// Adjusted transform calculation for rotational skew
_transform = AffineTransformMake( cy * _scaleX, sy * _scaleX,
-sx * _scaleY, cx * _scaleY,
x, y );
_transform = { cy * _scaleX, sy * _scaleX, 0, 0,
-sx * _scaleY, cx * _scaleY, 0, 0,
0, 0, 1, 0,
x, y, 0, 1 };
// XXX: Try to inline skew
// If skew is needed, apply skew and then anchor point
if (needsSkewMatrix)
{
AffineTransform skewMatrix = AffineTransformMake(1.0f, tanf(CC_DEGREES_TO_RADIANS(_skewY)),
tanf(CC_DEGREES_TO_RADIANS(_skewX)), 1.0f,
0.0f, 0.0f );
_transform = AffineTransformConcat(skewMatrix, _transform);
kmMat4 skewMatrix = { 1, tanf(CC_DEGREES_TO_RADIANS(_skewY)), 0, 0,
tanf(CC_DEGREES_TO_RADIANS(_skewX)),1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
_transform = TransformConcat(skewMatrix, _transform);
// adjust anchor point
if (!_anchorPointInPoints.equals(Point::ZERO))
{
_transform = AffineTransformTranslate(_transform, -_anchorPointInPoints.x, -_anchorPointInPoints.y);
// XXX: Argh, kmMat needs a "translate" method
_transform.mat[12] += -_anchorPointInPoints.x;
_transform.mat[13] += -_anchorPointInPoints.y;
}
}
if (_additionalTransformDirty)
{
_transform = AffineTransformConcat(_transform, _additionalTransform);
_transform = TransformConcat(_transform, _additionalTransform);
_additionalTransformDirty = false;
}
@ -667,7 +671,7 @@ Rect Armature::getBoundingBox() const
}
}
return RectApplyAffineTransform(boundingBox, getNodeToParentTransform());
return RectApplyTransform(boundingBox, getNodeToParentTransform());
}
Bone *Armature::getBoneAtPoint(float x, float y) const

View File

@ -161,7 +161,7 @@ public:
virtual void update(float dt) override;
virtual void draw() override;
virtual const cocos2d::AffineTransform& getNodeToParentTransform() const override;
virtual const kmMat4& getNodeToParentTransform() const override;
/**
* @js NA
* @lua NA

View File

@ -71,7 +71,8 @@ Bone::Bone()
_tween = nullptr;
_displayManager = nullptr;
_ignoreMovementBoneData = false;
_worldTransform = AffineTransformMake(1, 0, 0, 1, 0, 0);
// _worldTransform = AffineTransformMake(1, 0, 0, 1, 0, 0);
kmMat4Identity(&_worldTransform);
_boneTransformDirty = true;
_blendType = BLEND_NORMAL;
_worldInfo = nullptr;
@ -221,7 +222,7 @@ void Bone::update(float delta)
if (_armatureParentBone)
{
_worldTransform = AffineTransformConcat(_worldTransform, _armature->getNodeToParentTransform());
_worldTransform = TransformConcat(_worldTransform, _armature->getNodeToParentTransform());
}
}
@ -239,8 +240,8 @@ void Bone::applyParentTransform(Bone *parent)
{
float x = _worldInfo->x;
float y = _worldInfo->y;
_worldInfo->x = x * parent->_worldTransform.a + y * parent->_worldTransform.c + parent->_worldInfo->x;
_worldInfo->y = x * parent->_worldTransform.b + y * parent->_worldTransform.d + parent->_worldInfo->y;
_worldInfo->x = x * parent->_worldTransform.mat[0] + y * parent->_worldTransform.mat[4] + parent->_worldInfo->x;
_worldInfo->y = x * parent->_worldTransform.mat[1] + y * parent->_worldTransform.mat[5] + parent->_worldInfo->y;
_worldInfo->scaleX = _worldInfo->scaleX * parent->_worldInfo->scaleX;
_worldInfo->scaleY = _worldInfo->scaleY * parent->_worldInfo->scaleY;
_worldInfo->skewX = _worldInfo->skewX + parent->_worldInfo->skewX;
@ -387,14 +388,14 @@ void Bone::setZOrder(int zOrder)
Node::setZOrder(zOrder);
}
AffineTransform Bone::getNodeToArmatureTransform() const
kmMat4 Bone::getNodeToArmatureTransform() const
{
return _worldTransform;
}
AffineTransform Bone::getNodeToWorldTransform() const
kmMat4 Bone::getNodeToWorldTransform() const
{
return AffineTransformConcat(_worldTransform, _armature->getNodeToWorldTransform());
return TransformConcat(_worldTransform, _armature->getNodeToWorldTransform());
}
Node *Bone::getDisplayRenderNode()

View File

@ -152,8 +152,8 @@ public:
virtual void setTransformDirty(bool dirty) { _boneTransformDirty = dirty; }
virtual bool isTransformDirty() { return _boneTransformDirty; }
virtual cocos2d::AffineTransform getNodeToArmatureTransform() const;
virtual cocos2d::AffineTransform getNodeToWorldTransform() const override;
virtual kmMat4 getNodeToArmatureTransform() const;
virtual kmMat4 getNodeToWorldTransform() const override;
Node *getDisplayRenderNode();
DisplayType getDisplayRenderNodeType();
@ -233,7 +233,7 @@ protected:
bool _boneTransformDirty; //! Whether or not transform dirty
//! self Transform, use this to change display's state
cocos2d::AffineTransform _worldTransform;
kmMat4 _worldTransform;
BaseData *_worldInfo;

View File

@ -321,7 +321,7 @@ ColliderFilter *ColliderDetector::getColliderFilter()
Point helpPoint;
void ColliderDetector::updateTransform(AffineTransform &t)
void ColliderDetector::updateTransform(kmMat4 &t)
{
if (!_active)
{
@ -357,7 +357,7 @@ void ColliderDetector::updateTransform(AffineTransform &t)
for (int i = 0; i < num; i++)
{
helpPoint.setPoint( vs[i]->x, vs[i]->y);
helpPoint = PointApplyAffineTransform(helpPoint, t);
helpPoint = PointApplyTransform(helpPoint, t);
#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX

View File

@ -155,7 +155,7 @@ public:
void removeContourData(ContourData *contourData);
void removeAll();
void updateTransform(cocos2d::AffineTransform &t);
void updateTransform(kmMat4 &t);
void setActive(bool active);
bool getActive();

View File

@ -86,12 +86,12 @@ void DisplayFactory::updateDisplay(Bone *bone, float dt, bool dirty)
{
CC_BREAK_IF(!detector->getBody());
AffineTransform displayTransform = display->getNodeToParentTransform();
kmMat4 displayTransform = display->getNodeToParentTransform();
Point anchorPoint = display->getAnchorPointInPoints();
anchorPoint = PointApplyAffineTransform(anchorPoint, displayTransform);
displayTransform.tx = anchorPoint.x;
displayTransform.ty = anchorPoint.y;
AffineTransform t = AffineTransformConcat(displayTransform, bone->getArmature()->getNodeToParentTransform());
anchorPoint = PointApplyTransform(anchorPoint, displayTransform);
displayTransform.mat[12] = anchorPoint.x;
displayTransform.mat[13] = anchorPoint.y;
kmMat4 t = TransformConcat(displayTransform, bone->getArmature()->getNodeToParentTransform());
detector->updateTransform(t);
}
while (0);

View File

@ -80,7 +80,7 @@ Skin::Skin()
, _armature(nullptr)
, _displayName("")
{
_skinTransform = AffineTransformIdentity;
kmMat4Identity(&_skinTransform);
}
bool Skin::initWithSpriteFrameName(const std::string& spriteFrameName)
@ -135,10 +135,10 @@ const BaseData &Skin::getSkinData() const
void Skin::updateArmatureTransform()
{
_transform = AffineTransformConcat(_skinTransform, _bone->getNodeToArmatureTransform());
_transform = TransformConcat(_skinTransform, _bone->getNodeToArmatureTransform());
if(_armature && _armature->getBatchNode())
{
_transform = AffineTransformConcat(_transform, _armature->getNodeToParentTransform());
_transform = TransformConcat(_transform, _armature->getNodeToParentTransform());
}
}
@ -163,13 +163,13 @@ void Skin::updateTransform()
float x2 = x1 + size.width;
float y2 = y1 + size.height;
float x = _transform.tx;
float y = _transform.ty;
float x = _transform.mat[12];
float y = _transform.mat[13];
float cr = _transform.a;
float sr = _transform.b;
float cr2 = _transform.d;
float sr2 = -_transform.c;
float cr = _transform.mat[0];
float sr = _transform.mat[1];
float cr2 = _transform.mat[5];
float sr2 = -_transform.mat[4];
float ax = x1 * cr - y1 * sr2 + x;
float ay = x1 * sr + y1 * cr2 + y;
@ -195,22 +195,22 @@ void Skin::updateTransform()
}
}
AffineTransform Skin::getNodeToWorldTransform() const
kmMat4 Skin::getNodeToWorldTransform() const
{
return AffineTransformConcat(_transform, _bone->getArmature()->getNodeToWorldTransform());
return TransformConcat(_transform, _bone->getArmature()->getNodeToWorldTransform());
}
AffineTransform Skin::getNodeToWorldTransformAR() const
kmMat4 Skin::getNodeToWorldTransformAR() const
{
AffineTransform displayTransform = _transform;
kmMat4 displayTransform = _transform;
Point anchorPoint = _anchorPointInPoints;
anchorPoint = PointApplyAffineTransform(anchorPoint, displayTransform);
anchorPoint = PointApplyTransform(anchorPoint, displayTransform);
displayTransform.tx = anchorPoint.x;
displayTransform.ty = anchorPoint.y;
displayTransform.mat[12] = anchorPoint.x;
displayTransform.mat[13] = anchorPoint.y;
return AffineTransformConcat(displayTransform, _bone->getArmature()->getNodeToWorldTransform());
return TransformConcat(displayTransform, _bone->getArmature()->getNodeToWorldTransform());
}
void Skin::setBone(Bone *bone)

View File

@ -48,8 +48,8 @@ public:
void updateArmatureTransform();
void updateTransform() override;
cocos2d::AffineTransform getNodeToWorldTransform() const override;
cocos2d::AffineTransform getNodeToWorldTransformAR() const;
kmMat4 getNodeToWorldTransform() const override;
kmMat4 getNodeToWorldTransformAR() const;
/**
* @js NA
* @lua NA
@ -69,7 +69,7 @@ protected:
BaseData _skinData;
Bone *_bone;
Armature *_armature;
cocos2d::AffineTransform _skinTransform;
kmMat4 _skinTransform;
std::string _displayName;
};

View File

@ -118,6 +118,33 @@ void TransformHelp::nodeToMatrix(const BaseData &node, AffineTransform &matrix)
matrix.ty = node.y;
}
void TransformHelp::nodeToMatrix(const BaseData &node, kmMat4 &matrix)
{
kmMat4Identity(&matrix);
if (node.skewX == -node.skewY)
{
double sine = sin(node.skewX);
double cosine = cos(node.skewX);
matrix.mat[0] = node.scaleX * cosine;
matrix.mat[1] = node.scaleX * -sine;
matrix.mat[4] = node.scaleY * sine;
matrix.mat[5] = node.scaleY * cosine;
}
else
{
matrix.mat[0] = node.scaleX * cos(node.skewY);
matrix.mat[1] = node.scaleX * sin(node.skewY);
matrix.mat[4] = node.scaleY * sin(node.skewX);
matrix.mat[5] = node.scaleY * cos(node.skewX);
}
matrix.mat[12] = node.x;
matrix.mat[13] = node.y;
}
void TransformHelp::matrixToNode(const AffineTransform &matrix, BaseData &node)
{
/*
@ -144,6 +171,33 @@ void TransformHelp::matrixToNode(const AffineTransform &matrix, BaseData &node)
node.y = matrix.ty;
}
void TransformHelp::matrixToNode(const kmMat4 &matrix, BaseData &node)
{
/*
* In as3 language, there is a function called "deltaTransformPoint", it calculate a point used give Transform
* but not used the tx, ty value. we simulate the function here
*/
helpPoint1.x = 0;
helpPoint1.y = 1;
helpPoint1 = PointApplyTransform(helpPoint1, matrix);
helpPoint1.x -= matrix.mat[12];
helpPoint1.y -= matrix.mat[13];
helpPoint2.x = 1;
helpPoint2.y = 0;
helpPoint2 = PointApplyTransform(helpPoint2, matrix);
helpPoint2.x -= matrix.mat[12];
helpPoint2.y -= matrix.mat[13];
node.skewX = -(atan2f(helpPoint1.y, helpPoint1.x) - 1.5707964f);
node.skewY = atan2f(helpPoint2.y, helpPoint2.x);
node.scaleX = sqrt(matrix.mat[0] * matrix.mat[0] + matrix.mat[1] * matrix.mat[1]);
node.scaleY = sqrt(matrix.mat[4] * matrix.mat[4] + matrix.mat[5] * matrix.mat[5]);
node.x = matrix.mat[12];
node.y = matrix.mat[13];
}
void TransformHelp::nodeConcat(BaseData &target, BaseData &source)
{
target.x += source.x;

View File

@ -47,7 +47,9 @@ public:
static void transformToParentWithoutScale(BaseData &node, const BaseData &parentNode);
static void nodeToMatrix(const BaseData &_node, cocos2d::AffineTransform &_matrix);
static void nodeToMatrix(const BaseData &node, kmMat4 &matrix);
static void matrixToNode(const cocos2d::AffineTransform &_matrix, BaseData &_node);
static void matrixToNode(const kmMat4 &_matrix, BaseData &_node);
static void nodeConcat(BaseData &target, BaseData &source);
static void nodeSub(BaseData &target, BaseData &source);