Interacting with nodes from an .Irr file

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
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Interacting with nodes from an .Irr file

Post by dandi8 »

I seriously can't find anything on this...

I've successfuly loaded an .irr file but how do I interact with the stuff in there? I have two meshes in it called Terrain and PlayerMdl and want my camera to look on PlayerMdl (for example). But I can't do it without redeclaring PlayerMdl somewhere in my code o.O So how do I do it?
If it exists, it can be broken.
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

Code: Select all

smgr->getSceneNodeFromName("PlayerMdl");
That will give you the scene node with the name "PlayerMdl".
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Post by dandi8 »

But the scene node is an IMeshSceneNode (or supposed to be) and it says it can't convert one to the other.

Edit:
I'm so tired with getting this to work... Right now when I try to run it I get an unhandled exception error. Here's all my code so far:

Code: Select all

#include "stdafx.h" //Must be included to work
#include <irrlicht.h> //Include IrrLicht

using namespace irr; //Namespaces
using namespace core; //Namespaces
using namespace scene; //Namespaces
using namespace video; //Namespaces
using namespace io; //Namespaces
using namespace gui; //Namespaces

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
//^Setting up various stuff^

//Creating variables
float px; //player X position (default=0)
float py; //player Y position (default=140)
float pz; //player Z position (default=0)
float protx; //player X rotation (default=0)
float proty; //player Y rotation (default=0)
float protz; //player Z rotation (default=0)
double playerspeed; //player speed (default=0.035)
double playerrotspeed; //player rotation speed (default=0.15)

bool keys[KEY_KEY_CODES_COUNT]; //key codes (for movement and stuff)


//Keyboard input receiver ahead!
class MyEventReceiver : public IEventReceiver
{
public:
	// This is the one method that we have to implement
	virtual bool OnEvent(const SEvent& event)
	{
		// Remember whether each key is down or up
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

		return false;
	}

	// This is used to check whether a key is being held down
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}

	MyEventReceiver()
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
	}

private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
//End of keyboard input receiver


int main(){
  MyEventReceiver receiver;	//Create the event receiver (for receiving keyboard input)

 IrrlichtDevice *device = createDevice(EDT_DIRECT3D9, dimension2d<s32>(1024, 768), 32, 
	 false, false, false, &receiver);															//Create the window and device
	if(!device) //Meh
		return 1; //Return 1 on error

	device->setWindowCaption(L"Waste Rage"); //Set the window caption

	IVideoDriver* driver = device->getVideoDriver(); //Loading...
	ISceneManager* smgr = device->getSceneManager(); //Loading...
	IGUIEnvironment* guienv = device->getGUIEnvironment(); //Loading...

	ITexture* logo_small = driver->getTexture("C:/Users/Dandi8/Documents/Visual Studio 2008/Projects/irrLicht_helloworld/Debug/Data/Gfx/logo_small.png");

	smgr->loadScene("C:/Users/Dandi8/Documents/Visual Studio 2008/Projects/irrLicht_helloworld/Debug/Data/Maps/WRMap.irr");

	IMesh* mesh = smgr->getMesh("C:/Users/Dandi8/Documents/Visual Studio 2008/Projects/irrLicht_helloworld/Debug/Data/Models/Terrain.obj"); //Load the terrain model

	ISceneNode* Terrain = smgr->getSceneNodeFromName("Terrain");
		ISceneNode* PlayerMdl = smgr->getSceneNodeFromName("PlayerMdl");
		PlayerMdl->drop();

		ICameraSceneNode* cam = smgr->addCameraSceneNode(); //Add camera
		playerspeed=0.15; //Set player's speed
		playerrotspeed=0.15; //Set player's rotation speed
		PlayerMdl->setPosition(vector3df(px,py,pz)); //Set player's position

		//Collision ahead!
		scene::ITriangleSelector* level_selector = 0;
		scene::ITriangleSelector* player_selector = 0;
		level_selector = smgr->createOctTreeTriangleSelector(mesh, Terrain, 128);
		Terrain->setTriangleSelector(level_selector);

		if (level_selector)
	{
		scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
			level_selector, PlayerMdl, core::vector3df(10,10,10),
			core::vector3df(0,0,0), 
			core::vector3df(0,0,0));
		PlayerMdl->addAnimator(anim);

		anim = smgr->createCollisionResponseAnimator(
			level_selector, cam, core::vector3df(1,1,1),
			core::vector3df(0,0,0), 
			core::vector3df(0,py,0), 1);
		level_selector->drop();
		cam->addAnimator(anim);
		anim->drop();
	}

	guienv->addImage(logo_small, core::position2d<s32>(10,10));

		while(device->run()){
			driver->beginScene(true, true, SColor(255, 144, 255, 140)); //Begin the scene

			//movement ahead!
			if(receiver.IsKeyDown(irr::KEY_KEY_W) || receiver.IsKeyDown(irr::KEY_UP)){
			px = sin(double (proty * irr::core::DEGTORAD)) * playerspeed; 
			pz = cos(double (proty * irr::core::DEGTORAD)) * playerspeed; 
			PlayerMdl->setPosition(PlayerMdl->getPosition() + vector3df(px,0,pz)); //Set player's position
			} 
			if(receiver.IsKeyDown(irr::KEY_KEY_S) || receiver.IsKeyDown(irr::KEY_DOWN)){
			px = sin(double (proty * irr::core::DEGTORAD)) * playerspeed; 
			pz = cos(double (proty * irr::core::DEGTORAD)) * playerspeed; 
			PlayerMdl->setPosition(PlayerMdl->getPosition() - vector3df(px,0,pz)); //Set player's position
			} 
			if(receiver.IsKeyDown(irr::KEY_KEY_A) || receiver.IsKeyDown(irr::KEY_LEFT)){
				proty-=playerrotspeed;
			}
			if(receiver.IsKeyDown(irr::KEY_KEY_D) || receiver.IsKeyDown(irr::KEY_RIGHT)){
				proty+=playerrotspeed;
			}
			if(receiver.IsKeyDown(irr::KEY_KEY_E) || receiver.IsKeyDown(irr::KEY_KEY_X)){
			px = sin(double ((proty+90) * irr::core::DEGTORAD)) * playerspeed; 
			pz = cos(double ((proty+90) * irr::core::DEGTORAD)) * playerspeed; 
			PlayerMdl->setPosition(PlayerMdl->getPosition() + vector3df(px,0,pz)); //Set player's position
			} 
			if(receiver.IsKeyDown(irr::KEY_KEY_Q) || receiver.IsKeyDown(irr::KEY_KEY_Z)){
			px = sin(double ((proty+90) * irr::core::DEGTORAD)) * playerspeed; 
			pz = cos(double ((proty+90) * irr::core::DEGTORAD)) * playerspeed; 
			PlayerMdl->setPosition(PlayerMdl->getPosition() - vector3df(px,0,pz)); //Set player's position
			} 
			//end of movement!
			PlayerMdl->setRotation(vector3df(protx,proty,protz)); //Set player's rotation
			cam->setPosition(PlayerMdl->getPosition() - vector3df(sin(double (proty * irr::core::DEGTORAD)) * 35, -15, cos(double (proty * irr::core::DEGTORAD)) * 35)); //Alternate cam
			cam->setTarget(PlayerMdl->getPosition()); //Make the camera look at the vehicle
			smgr->drawAll(); //Draw the scene
			guienv->drawAll(); //Draw the GUI

			driver->endScene(); //End the scene
		}

		device->drop(); //Destroy the device

		return 0; //Return the zero
}
Can someone please, pretty please make it work again? :(
If it exists, it can be broken.
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

Code: Select all

 PlayerMdl->drop(); 
Bad, bad. Get rid of this. Why are you dropping it? You're using it just a few lines down!
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Post by dandi8 »

Stupid me! It was a leftover from previous experiments. Now it launches :)

Question #2:
Why are there strange outlines in my terrain? How do I get rid of them? Notice they only appear far away.

Image

And question #3:
Is there a way to isolate X, Y and Z (extract them to 3 different variables) from the getPosition function? I basicaly want to get X and Z of a model in space.
If it exists, it can be broken.
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

For the position:

Code: Select all

float x = node->getPosition().X;
Same with y and z.
As for the terrain, it looks like a mip mapping problem. Try disabling mipmaps and see if it gets rid of the problem.
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Post by dandi8 »

Hmmm. How do you disable mipmaps?
If it exists, it can be broken.
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

Before you load the model:

Code: Select all

	driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS,false);
and after:

Code: Select all

	driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS,true);
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Post by dandi8 »

Why would I want to reenable mipmaps anyway? I think I don't really need them if it isn't a big game?
Last edited by dandi8 on Thu May 07, 2009 10:01 pm, edited 2 times in total.
If it exists, it can be broken.
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

Well, if you don't feel the need to reenable them then that's fine. Just remember you get a lower frame rate with them disabled.
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Post by dandi8 »

Just tried it, it werkz! Another question though - this time concerning something different entirely. Dynamic shadows - how to enable them? Scrolling through the driver's functions I've found drawStencilShadowVolume and drawStencilShadow, which I believe should be executed in that order. But I don't really know what to write in the parameters nor if I need anything else for that to work?
If it exists, it can be broken.
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

Look in the specialfx example that comes with the irrlicht download, there should be a shadow example there. But be warned, default irrlicht shadows are extremely slow. You may want to use something shader based, like BlindSide's XEffects.
Post Reply