Added CSceneNodeAnimatorOffsetRotation

A forum to store posts deemed exceptionally wise and useful
Post Reply
Electron
Posts: 874
Joined: Sun Mar 14, 2004 12:05 am
Location: Massachusetts USA

Added CSceneNodeAnimatorOffsetRotation

Post by Electron »

I'm not quite sure how to explain the functionality of this, or how many uses their are for it. Basically it simulates the node having a rotating parent. I made it when I needed to implement weapon switching in my game. I wanted the weapon to dip as if it were being lowered from the elbow. Trouble was I had no elbow node to rotate and I didn't feel as though I should have to make one just for that. Here's the code for the animator

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
and I added this to CSceneManager.cpp to create an animator of this type. Add the function prototypes to CSceneManager.h and ISceneManager.h of course

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; 
}
rotation is the rotation in degrees, as with a regular rotation animator.
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.
You do a lot of programming? Really? I try to get some in, but the debugging keeps me pretty busy.

Crucible of Stars
Post Reply