Code: Select all
#ifndef CSceneNodeAnimatorOffsetRotation_h
#define CSceneNodeAnimatorOffsetRotation_h
#include "ISceneNodeAnimator.h"
namespace irr
{
namespace scene
{
class CSceneNodeAnimatorOffsetRotation : public ISceneNodeAnimator
{
public:
CSceneNodeAnimatorOffsetRotation(core::vector3df _offset,
core::vector3df _rotation,u32 _timeForRotation,
bool _loop, u32 _startTime) : offset(_offset),rotation(_rotation),
timeForRotation(_timeForRotation), loop(_loop), startTime(_startTime)
{
}
virtual void animateNode(ISceneNode* node, u32 timeMs)
{
f32 portion=(f32)(timeMs - startTime)/(f32)timeForRotation;
if(portion>1.0f)
{
if(false==loop){return;}
else {portion=0;startTime=timeMs;}
}
core::matrix4 mat, mat2;
mat2.setTranslation(node->getPosition() + offset);
mat2.setRotationDegrees(rotation * portion);
mat.setTranslation(offset * -1.0f);
mat=mat2 * mat;
node->setPosition(mat.getTranslation());
node->setRotation(mat.getRotationDegrees());
}
protected:
core::vector3df offset;
core::vector3df rotation;
u32 timeForRotation;
u32 startTime;
bool loop;
};
} // end namespace scene
} // end namespace irr
#endif
Code: Select all
ISceneNodeAnimator* CSceneManager::createOffsetRotationAnimator(
const core::vector3df& rotation,
const core::vector3df& offset,
u32 timeForRotate,bool loop)
{
ISceneNodeAnimator* anim = new CSceneNodeAnimatorOffsetRotation(
rotation, offset, timeForRotate,loop, os::Timer::getTime());
return anim;
}
Offset is where the "simulated parent" should be placed in relation to the animated node. timeForRotate is how long the rotation should take nd loop is whether or not it should loop.
Hope someone finds this useful.