Best way to add moving platforms?

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
teamAlpha
Posts: 68
Joined: Sun Mar 08, 2009 4:14 pm

Best way to add moving platforms?

Post 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?
Alpha::AppleBoy
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

No direction means no (or random) movement.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

use something like this:

Code: Select all

struct PlatformNode 
{ 
        irrnode *node; 
        vector3df    start,end;
        bool moveForward;
}; 
teamAlpha
Posts: 68
Joined: Sun Mar 08, 2009 4:14 pm

Post 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: ...
Alpha::AppleBoy
Nimrod
Posts: 8
Joined: Sun Feb 22, 2009 8:10 pm

Post 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();
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
teamAlpha
Posts: 68
Joined: Sun Mar 08, 2009 4:14 pm

Post 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.
Alpha::AppleBoy
Post Reply