Page 1 of 1

How can i go up stairs?

Posted: Mon Nov 22, 2010 2:43 pm
by gantz0
Image


Hello, I wanna how can i make character going up stairs.
If you know, let me know plz.

Code: Select all

enum
{
	ID_IsNotPickable = 0,
	IDFlag_IsPickable = 1 << 0,
	IDFlag_IsHighlightable = 1 << 1
};

scene::IAnimatedMeshSceneNode* node;
scene::ICameraSceneNode* camera;

class MyEventReceiver : public irr::IEventReceiver
{ 
 public:
	int State;
	int Roll;

	MyEventReceiver ()
	{
		State = Roll = 0;
	}
	~MyEventReceiver() {}


	virtual bool OnEvent(const irr::SEvent& event)
	{
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
		{
			switch(event.KeyInput.Key)
			{
			case irr::KEY_KEY_W:
			case irr::KEY_KEY_S:
				 if (event.KeyInput.PressedDown)
				 {
					 if (event.KeyInput.Key == irr::KEY_KEY_S)
					 {
						State = -1;       	

					 }
						else 
                            State = 1;                         
                 } 
				 else
					State = 0; 
				 break;
			
			case irr::KEY_KEY_A:
			case irr::KEY_KEY_D:
					if (event.KeyInput.PressedDown) 
					{
						if (event.KeyInput.Key == irr::KEY_KEY_A)
							Roll = -1;                         
						else
							Roll = 1;                    
                    }
					else 
                        Roll = 0; 				
				break;
			default:
				break;
			}
		}		
		return true;
	}
};


int main()
{
	IrrlichtDevice *device =
		createDevice(irr::video::EDT_OPENGL, core::dimension2d<u32>(640, 480), 16, false);

	if (device == 0)
		return 1; // could not create selected driver.

	
	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	irr::gui::IGUIEnvironment *pGuiEnv = device->getGUIEnvironment();

	device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3");
	scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");


	MyEventReceiver Receiver;
	scene::IMeshSceneNode* q3node = 0;
	node = 0;
	irr::scene::ISceneNode* TargetGirlnode;
	irr::gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();

	
	device->setEventReceiver(& Receiver);	

	
	if (q3levelmesh)
		q3node = smgr->addOctreeSceneNode(q3levelmesh->getMesh(0), 0, IDFlag_IsPickable);

		
	if (q3node)
	{	
		q3node->setPosition(core::vector3df(-1350,-130,-1400));
		q3node->setPosition(core::vector3df(-2000,-300,-1000));		
	}

	camera = smgr->addCameraSceneNodeFPS(0, 100.0f, .3f, ID_IsNotPickable, 0, 0, true, 3.f);
	camera->setPosition(core::vector3df(0,70,-200));

	device->getCursorControl()->setVisible(false);

	node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/faerie.md2"),
						0, IDFlag_IsPickable | IDFlag_IsHighlightable);
	node->setPosition(core::vector3df(50,-30,50)); // Put its feet on the floor.
	node->setScale(core::vector3df(1,1,1)); // Make it appear realistically scaled
	node->setMD2Animation(scene::EMAT_RUN);
	node->setAnimationSpeed(30.f);
	
	video::SMaterial material;
	material.setTexture(0, driver->getTexture("../../media/faerie2.bmp"));
	material.Lighting = true;
	material.NormalizeNormals = true;
	node->getMaterial(0) = material;


	
	TargetGirlnode = smgr->addEmptySceneNode(node);
	TargetGirlnode->setPosition(irr::core::vector3df(1,0,0));
	

	scene::IMetaTriangleSelector* SumSelector = smgr->createMetaTriangleSelector(); 
	scene::ITriangleSelector* MapSelector = smgr->createOctreeTriangleSelector(q3node->getMesh(), q3node, 128);
	q3node->setTriangleSelector(MapSelector);
	
    SumSelector->addTriangleSelector(MapSelector); 

	if (SumSelector)
	{
		scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
			SumSelector, node, core::vector3df(10,10,10),
			core::vector3df(0,-5,0), 
			core::vector3df(0,15,0));		

		node->addAnimator(anim);
		anim->drop(); 
		MapSelector->drop(); 		
        SumSelector->drop(); 
	}



	material.setTexture(0, 0);
	material.Lighting = false;


	scene::ILightSceneNode * light = smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
		video::SColorf(1.0f,1.0f,1.0f,1.0f), 600.0f);
	light->setID(ID_IsNotPickable); // Make it an invalid target for selection.


	scene::ISceneNode* highlightedSceneNode = 0;
	scene::ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
	int lastFPS = -1;


	material.Wireframe=true;


	int lastState = Receiver.State;
	irr::core::vector3df TempV3df;
	irr::scene::ISceneNode* TempCamera = smgr->addEmptySceneNode(node);	//캐릭터 노드에 빈공간에 붙인다.
	TempCamera->setPosition(irr::core::vector3df(-50 , 30 ,0));
	 irr::gui::IGUIStaticText *pstextFPS = 
	 pGuiEnv->addStaticText(L"Frame rate",irr::core::rect<irr::s32>(0,0,100,20),true,true,0,100,true);


	 irr::u32 uLastTick = device->getTimer()->getTime();
	 irr::u32 CurrentTick = device->getTimer()->getTime();	 
	 irr::u32 fDelta = 0 ;
	 int sum= 0;


	while(device->run())
	{
		
	
		CurrentTick = device->getTimer()->getTime();		
		fDelta = (int) (CurrentTick - uLastTick)  ; 
	

		if( fDelta >= 3 ) // 0.3
		{
			uLastTick = device->getTimer()->getTime();			
			fDelta = 0;
			sum = 0;
			

	
			if (Receiver.Roll) 
			{
				TempV3df = node->getRotation();
				TempV3df.Y += 0.5f*Receiver.Roll; 
				node->setRotation(TempV3df);
			}
			if (Receiver.State) 
			{
				TempV3df = TargetGirlnode->getAbsolutePosition() - node->getPosition();
				node->setPosition(node->getPosition() + TempV3df * Receiver.State * 0.3f); 
			} 
	
			
		}
	


		if (device->isWindowActive())
		{
			driver->beginScene(true, true, 0);
			//============================================================//
			if (lastState != Receiver.State) 
			{
				if (Receiver.State) 
					node->setMD2Animation(irr::scene::EMAT_RUN); 
				else 
					node->setMD2Animation(irr::scene::EMAT_STAND);
	          
			}
			 lastState = Receiver.State;

		
			

			camera->setPosition (TempCamera->getAbsolutePosition ());
			camera->setTarget(node->getPosition());

			//============================================================//

			smgr->drawAll();


			if (highlightedSceneNode)
			{
				highlightedSceneNode->setMaterialFlag(video::EMF_LIGHTING, true);
				highlightedSceneNode = 0;
			}
			core::line3d<f32> ray; 
			ray.start = camera->getPosition();
			ray.end = ray.start + (camera->getTarget() - ray.start).normalize() * 1000.0f;


			core::vector3df intersection;
			core::triangle3df hitTriangle;


			char wszbuf[256];
			sprintf(wszbuf,"Frame rate : %d", sum);
			font->draw(wszbuf, core::rect<s32>(0,0,10,10),video::SColor(255,255,255,255));


			driver->endScene();

			int fps = driver->getFPS();
			if (lastFPS != fps)
			{
				core::stringw str = L"Collision detection example - Irrlicht Engine [";
				str += driver->getName();
				str += "] FPS:";
				str += fps;
				device->setWindowCaption(str.c_str());
				lastFPS = fps;
			}
		}
	}
	device->drop();

	return 0;
}

Posted: Mon Nov 22, 2010 3:14 pm
by Midnight
you can't go upstairs that is too complex... instead you make an invisible ramp you can walk up.

EDIT:: actually I take that back, you could use a ray on the y axis to follow the mesh, or you could use physics and make your character object shaped in such a way it would slide upwards.. and/or an invisible ramp.

Thank you!

Posted: Mon Nov 22, 2010 3:49 pm
by gantz0
Midnight wrote:you can't go upstairs that is too complex... instead you make an invisible ramp you can walk up.

EDIT:: actually I take that back, you could use a ray on the y axis to follow the mesh, or you could use physics and make your character object shaped in such a way it would slide upwards.. and/or an invisible ramp.


But I don't know how can i distinguish whether stairs or not.

Thanks your comment.

this comment is very helpful to me.

Posted: Mon Nov 22, 2010 4:20 pm
by lazerblade
Have you tried the tutorials?

http://irrlicht.sourceforge.net/docu/example007.html


From tutorial:
We will describe 2 methods: Automatic collision detection for moving through 3d worlds with stair climbing and sliding, and manual scene node and triangle picking using a ray.

Thanks but...

Posted: Mon Nov 22, 2010 4:47 pm
by gantz0
lazerblade wrote:Have you tried the tutorials?

http://irrlicht.sourceforge.net/docu/example007.html


From tutorial:
We will describe 2 methods: Automatic collision detection for moving through 3d worlds with stair climbing and sliding, and manual scene node and triangle picking using a ray.
yes, i tried tutorial7

But I already said how do i know stairs?

how can i find that wall or stairs? really wondering....

Tutorial7 jsut did show to able collision filed..

thanks your comment

Posted: Mon Nov 22, 2010 7:56 pm
by lazerblade
Try looking up 'smgr->createCollisionResponseAnimator()'.
This function automatically handles ramps/steps I think.
Although I have no idea about it's behavior related to scene nodes other than
cameras. It should be okay though.

Try following the tutorial, and then changing it little by little until it does your
thing instead of it's own thing. You'll probably learn a lot that way too. ;)

Posted: Mon Nov 22, 2010 7:57 pm
by lazerblade
P.S:

That is, I think it automatically detects thing short enough to walk up and
thusly creates stair effect. So you don't have to worry about telling it what
is and isn't stairs.

I appreciate you

Posted: Mon Nov 22, 2010 11:53 pm
by gantz0
lazerblade wrote:P.S:

That is, I think it automatically detects thing short enough to walk up and
thusly creates stair effect. So you don't have to worry about telling it what
is and isn't stairs.
I appreciate your comment.

very helpful to me.~

I should think about tutorial and add my code.

If and when you have related to code, if you don't mind give me code~


anyway thanks.