Page 1 of 1

Best way to add moving platforms?

Posted: Fri Mar 20, 2009 1:23 pm
by teamAlpha
Hey :P wazzup ? !.

I'm "working" on a platform game , and i'm trying to figure out how to implement moving platforms.


I know that i'll need a structure like this:

Code: Select all

struct PlatformNode
{
        irrnode *node;
        vector3df    start,end;
};
But i can't figure out how to move the platform without having to add a direction flag.

Eg. i want to avoid this:

Code: Select all

struct PlatformNode
{
        irrnode *node;
        vector3df    start,end;

        Direction dir;//h8 this
};


add nodes

loop through nodes
 switch node->dir
    if left , keep translating left
if reached end , swap end with start , set dir to right

...So ,do you guys have any ideas?

Posted: Fri Mar 20, 2009 1:32 pm
by Sylence
No direction means no (or random) movement.

Posted: Fri Mar 20, 2009 1:34 pm
by Strong99
use something like this:

Code: Select all

struct PlatformNode 
{ 
        irrnode *node; 
        vector3df    start,end;
        bool moveForward;
}; 

Posted: Fri Mar 20, 2009 1:56 pm
by teamAlpha
I've been smashing my head for a while , and came up with this:

Code: Select all

struct PlatformNode
{
        irrnode      *node;
        vector3df    start,end,trans;
        bool         reached_target,swapped_trans;
}

inline void swapVec(vector3df& a,vector3df& b)
{
    vector3df temp = a;
    a = b;
    b = temp;
}

    foreach node -> n
    {

        if(n->trans+n->node->getScale() >= end && !swapped_trans)
        {
            swapped_trans = true;
            reached_target = true;
            swapVec(n->end,n->start);
        }else if (n->trans+n->node->getScale() <= end && swapped_trans && reached_target)
        {
            swapped_trans = false;
            reached_target = false;
            swapVec(n->start,n->end);
        }


        if(!n->reached_target)
            n->trans += (n->end - start + n->trans)/1000.0f;//hmm movement might be sluggish
        else if(reached_target)
            n->trans -= (n->end - start + n->trans)/1000.0f;

    }
I'm close but damn my math skills aren't that high :cry: ...

Posted: Fri Mar 20, 2009 3:12 pm
by Nimrod
My suggestion is to use a customized animator.
Here's a quick and dirty example that might help you:

Code: Select all

#include "irrlicht.h"
namespace irr{
namespace scene{
	class CSceneNodeBackForwardAnimator : public ISceneNodeAnimator{
	    public:
		CSceneNodeBackForwardAnimator(){start_timeMs=0;};
		virtual void animateNode(ISceneNode* node, u32 timeMs);
		virtual ISceneNodeAnimator* createClone(ISceneNode* node, ISceneManager* newManager=0){};
		u32 start_timeMs;
	};
	
	void CSceneNodeBackForwardAnimator::animateNode(ISceneNode* node, u32 timeMs){
		if(!start_timeMs){
			start_timeMs = timeMs;
		}
		core::vector3df pos = node -> getPosition();
		pos.Z += cos((float)(timeMs-start_timeMs)/1000)/10;
		node -> setPosition(pos);
	}

}} // end irr::scene
if you want an example in action, go to example 4 in the tutorial, include the above codes and change

Code: Select all

smgr->createFlyCircleAnimator(core::vector3df(0,0,30), 20.0f);
to

Code: Select all

new irr::scene::CSceneNodeBackForwardAnimator();

Posted: Fri Mar 20, 2009 4:44 pm
by vitek
I believe you could just use the built-in fly straight animator. The last parameter to createFlyStraightAnimator() tells the animator to go back and forth...

Code: Select all

ISceneNodeAnimator* animator = smgr->createFlyStraightAnimator(start, end, time, false, true);
node->addAnimator(animator);
animator->drop();
Being that it is built-in to Irrlicht, you should be able to use IrrEdit to build your levels without having to add any factory code.

Travis

Posted: Fri Mar 20, 2009 5:35 pm
by teamAlpha
Thanks for the suggestions!

Now im on my own i guess , and i will have to decide what i really want.

The only thing that im sure about it , is , that its an easy task , but can become really tricky(especially when integrating physics).


Thanks again.