CSeneNodeAnimatorRotate.cpp
Code: Select all
#include "CSceneNodeAnimatorRotate.h"
namespace irr
{
namespace scene
{
//! constructor
CSceneNodeAnimatorRotate::CSceneNodeAnimatorRotate(const core::vector3df& startRotation,
const core::vector3df& endRotation, u32 timeForWay,
bool loop, u32 now)
: Start(startRotation), End(endRotation), StartTime(now), TimeForWay(timeForWay), Loop(loop)
{
#ifdef _DEBUG
setDebugName("CSceneNodeAnimatorRotate");
#endif
recalculateImidiateValues();
}
void CSceneNodeAnimatorRotate::recalculateImidiateValues()
{
Vector = End - Start;
WayLength = (f32)Vector.getLength();
Vector.normalize();
TimeFactor = WayLength / TimeForWay;
}
//! destructor
CSceneNodeAnimatorRotate::~CSceneNodeAnimatorRotate()
{
}
//! animates a scene node
void CSceneNodeAnimatorRotate::animateNode(ISceneNode* node, u32 timeMs)
{
if (!node)
return;
u32 t = (timeMs-StartTime);
core::vector3df pos = Start;
if (!Loop && t >= TimeForWay)
pos = End;
else if (t > TimeForWay)
{
StartTime+=TimeForWay;
t-=TimeForWay;
core::vector3df tmp=Start;
Start=End;
End=tmp;
recalculateImidiateValues();
pos = Start;
pos += Vector * (f32)t * TimeFactor;
}
else
pos += Vector * (f32)t * TimeFactor;
node->setRotation(pos);
}
//! Writes attributes of the scene node animator.
void CSceneNodeAnimatorRotate::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options)
{
out->addVector3d("Start", Start);
out->addVector3d("End", End);
out->addInt("TimeForWay", TimeForWay);
out->addBool("Loop", Loop);
}
//! Reads attributes of the scene node animator.
void CSceneNodeAnimatorRotate::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{
Start = in->getAttributeAsVector3d("Start");
End = in->getAttributeAsVector3d("End");
TimeForWay = in->getAttributeAsInt("TimeForWay");
Loop = in->getAttributeAsBool("Loop");
recalculateImidiateValues();
}
} // end namespace scene
} // end namespace irr
Code: Select all
#ifndef __C_SCENE_NODE_ANIMATOR_ROTATE_H_INCLUDED__
#define __C_SCENE_NODE_ANIMATOR_ROTATE_H_INCLUDED__
#include "ISceneNode.h"
namespace irr
{
namespace scene
{
class CSceneNodeAnimatorRotate : public ISceneNodeAnimator
{
public:
//! constructor
CSceneNodeAnimatorRotate(const core::vector3df& startRotation,
const core::vector3df& endRotation, u32 timeForWay,
bool loop, u32 now);
//! destructor
virtual ~CSceneNodeAnimatorRotate();
//! animates a scene node
virtual void animateNode(ISceneNode* node, u32 timeMs);
//! Writes attributes of the scene node animator.
virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0);
//! Reads attributes of the scene node animator.
virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0);
//! Returns type of the scene node animator
virtual ESCENE_NODE_ANIMATOR_TYPE getType() { return ESNAT_ROTATE; }
private:
void recalculateImidiateValues();
core::vector3df Start;
core::vector3df End;
core::vector3df Vector;
f32 WayLength;
f32 TimeFactor;
u32 StartTime;
u32 TimeForWay;
bool Loop;
};
} // end namespace scene
} // end namespace irr
#endif
add to ISceneManager.h
Code: Select all
//! Creates a rotate animator, which lets the attached scene node change rotations smoothly.
/** \param startRotation: Starting rotation.
\param endRotation: Ending rotation.
\param timeForWay: Time in milli seconds how long the node should need to
move from the start rotation to the end rotation.
\param loop: If set to false, the node stops when the end point is reached.
If loop is true, the node begins then starts heading back.
\return Returns the animator. Attach it to a scene node with ISceneNode::addAnimator()
and the animator will animate it.
If you no longer need the animator, you should call ISceneNodeAnimator::drop().
See IUnknown::drop() for more information. */
virtual ISceneNodeAnimator* createRotateAnimator(const core::vector3df& startRotation,
const core::vector3df& endRotation, u32 timeForWay, bool loop=false) = 0;
Code: Select all
//! Creates a rotate animator, which lets the attached scene node
//! rotate from one rotation to the other smoothly.
virtual ISceneNodeAnimator* createRotateAnimator(const core::vector3df& startRotation,
const core::vector3df& endRotation, u32 timeForWay, bool loop=false);
Code: Select all
#include "CSceneNodeAnimatorRotate.h"
Code: Select all
//! Creates a rotate animator, which lets the attached scene node
//! rotate from one rotation to the other smoothly.
ISceneNodeAnimator* CSceneManager::createRotateAnimator(const core::vector3df& startRotation,
const core::vector3df& endRotation, u32 timeForWay, bool loop)
{
ISceneNodeAnimator* anim = new CSceneNodeAnimatorRotate(startRotation,
endRotation, timeForWay, loop, os::Timer::getTime());
return anim;
}