createFollowSplineAnimator() - slows at each point

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
markanderson
Posts: 30
Joined: Thu Mar 16, 2006 6:21 pm

createFollowSplineAnimator() - slows at each point

Post by markanderson »

Hello All -

I'm moving nodes around using createFollowSplineAnimator(), some splines have only a few points, other splines have dozens of points. The nodes slow down at each spline point rather than staying at a constant speed.

Can anyone tell me if it's possible to set createFollowSplineAnimator() so that the movement stays at a constant speed?

Thanks,

Mark.
markanderson
Posts: 30
Joined: Thu Mar 16, 2006 6:21 pm

Maybe Spline is the wrong word

Post by markanderson »

I've had a think about this problem. createFollowSplineAnimator() follows a spline which by its nature tends to be curved and curves mean slowing the node as it moves.

But the way I want to use it is to propel nodes in straight lines with occasional branches off. I use a tightness value of 0 because that makes the node actually hit the points and stick to them. But maybe there is a better api function I could use - instead of createFollowSplineAnimator() is there maybe a createFollowPolyAnimator()?

Thanks.
markanderson
Posts: 30
Joined: Thu Mar 16, 2006 6:21 pm

Post by markanderson »

Or better idea - is there a way to chain createFlyStraightAnimators() so that one the node flys straight to the first point, then straight to the second and so on?
markanderson
Posts: 30
Joined: Thu Mar 16, 2006 6:21 pm

Post by markanderson »

So far I'm having a really interesting conversation with myself ;)

I now believe a custom scene animator is the way to go here. The animator should accept an array of points to move through in a waypath sort of way and it should either loop when it reaches the final point or delete itself based on a passed bool.

I've found a suitable template animator on the forums which I think I can work from. This is a step up the irrlicht tree of difficulty for me so wish me luck ;) I'll post my results for the brave souls who follow.
markanderson
Posts: 30
Joined: Thu Mar 16, 2006 6:21 pm

IWaypointAnimator()

Post by markanderson »

Hazzah! That wasn't as hard as I thought it would be. This is a waypoint animator that moves a node through several points. It's not constant speed but at least it only slows when changing angle, not when moving in a straight line through points like a spline animator.

The animator destroys itself using vitek's function from this thread. It will also remove the node when it reaches its destination if you want ;]

Enjoy...

Code: Select all


extern void ISceneNodeAnimator_addToDeletionQueue(ISceneNodeAnimator* anim, ISceneNode* node); 

class IWaypointAnimator : public ISceneNodeAnimator
{
public:
	IWaypointAnimator(IrrlichtDevice *device, core::array < core::vector3df > points, bool DeleteNodeAtEnd);
	virtual ~IWaypointAnimator ();
	virtual void animateNode (scene::ISceneNode *node, u32 timeMs);
private:
	bool mDelNode;
	int mCurrPoint;
	IrrlichtDevice *mDevice;
	core::array < core::vector3df > mPoints;
};

IWaypointAnimator::IWaypointAnimator(IrrlichtDevice *device, core::array < core::vector3df > points, bool DeleteNodeAtEnd)
{
	mPoints = points;
	mCurrPoint = 0;
	mDelNode = DeleteNodeAtEnd;
	mDevice = device;
}

IWaypointAnimator::~IWaypointAnimator ()
{

}

void IWaypointAnimator::animateNode (scene::ISceneNode *node, u32 timeMs)
{
	int fly = 0;
	if (mCurrPoint == 0)
		node->setPosition(mPoints[mCurrPoint]);

	//there must be a better way to compare vector3df's
	if (node->getPosition().X == mPoints[mCurrPoint].X &&
		node->getPosition().Y == mPoints[mCurrPoint].Y &&
		node->getPosition().Z == mPoints[mCurrPoint].Z) 
	{
		mCurrPoint++;
		fly++;
	}

	//i suspect this cast is the sign of a c++ n00b
	if (mCurrPoint >= (long) mPoints.size()-1)
	{
		ISceneNodeAnimator_addToDeletionQueue(this, node);
		
		if (mDelNode)
		{
			scene::ISceneNodeAnimator *delanim = mDevice->getSceneManager()->createDeleteAnimator(100);
			node->addAnimator(delanim);
			delanim->drop();
		}
	}

	if (fly) 
	{
		scene::ISceneNodeAnimator *anim = mDevice->getSceneManager()->createFlyStraightAnimator(node->getPosition(), mPoints[mCurrPoint], 200);
		node->addAnimator(anim);
		anim->drop();
	}
} 
Usage is simple:

Code: Select all

	IWaypointAnimator *anim = new IWaypointAnimator(device, points, TRUE);
	node->addAnimator((scene::ISceneNodeAnimator*)anim); 
	anim->node();
If anyone can spot any thing that could be done better - please let me know.
Sundar
Posts: 84
Joined: Mon Jun 05, 2006 11:05 am

Post by Sundar »

HI markanderson Thanx for your code. it worked like a dream for me. thanx a lot
Post Reply