Posted on 2011-04-27 16:54
点点滴滴 阅读(518)
评论(0) 编辑 收藏 引用 所属分类:
08 游戏SDK
void Node::rotate(const Quaternion& q, TransformSpace relativeTo)
{
// Normalise quaternion to avoid drift
Quaternion qnorm = q;
qnorm.normalise();
switch(relativeTo)
{
case TS_PARENT:
// Rotations are normally relative to local axes, transform up
mOrientation = qnorm * mOrientation;
break;
case TS_WORLD:
// Rotations are normally relative to local axes, transform up
mOrientation = mOrientation * _getDerivedOrientation().Inverse()
* qnorm * _getDerivedOrientation();
break;
case TS_LOCAL:
// Note the order of the mult, i.e. q comes after
mOrientation = mOrientation * qnorm;
break;
}
needUpdate();
}
旋转是本地坐标旋转
//-----------------------------------------------------------------------
void Node::translate(const Vector3& d, TransformSpace relativeTo)
{
switch(relativeTo)
{
case TS_LOCAL:
// position is relative to parent so transform downwards
mPosition += mOrientation * d;
break;
case TS_WORLD:
// position is relative to parent so transform upwards
if (mParent)
{
mPosition += (mParent->_getDerivedOrientation().Inverse() * d)
/ mParent->_getDerivedScale();
}
else
{
mPosition += d;
}
break;
case TS_PARENT:
mPosition += d;
break;
}
needUpdate();
}
变换是相对于父坐标变换
//-----------------------------------------------------------------------
void Node::updateFromParentImpl(void) const
{
if (mParent)
{
// Update orientation
const Quaternion& parentOrientation = mParent->_getDerivedOrientation();
if (mInheritOrientation)
{
// Combine orientation with that of parent
mDerivedOrientation = parentOrientation * mOrientation;
}
else
{
// No inheritence
mDerivedOrientation = mOrientation;
}
// Update scale
const Vector3& parentScale = mParent->_getDerivedScale();
if (mInheritScale)
{
// Scale own position by parent scale, NB just combine
// as equivalent axes, no shearing
mDerivedScale = parentScale * mScale;
}
else
{
// No inheritence
mDerivedScale = mScale;
}
// Change position vector based on parent's orientation & scale
mDerivedPosition = parentOrientation * (parentScale * mPosition);
// Add altered position vector to parents
mDerivedPosition += mParent->_getDerivedPosition();
}
else
{
// Root node, no parent
mDerivedOrientation = mOrientation;
mDerivedPosition = mPosition;
mDerivedScale = mScale;
}
mCachedTransformOutOfDate = true;
mNeedParentUpdate = false;
}
//