Need some help on some things

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.
cgcgames2
Posts: 24
Joined: Mon Jul 26, 2010 6:29 am

Post by cgcgames2 »

thanks for all you guys help i manged to get what i wanted apart from one thing.

how can you make it so the model accelrates to that speed over time. and when the key is released the model keeps moving.

another thing is when the model is rotated how do you change its Z axes. or is there another way without doing that. Basilcy i if rotat the ship 180 degrees with the A KEY then press w and s it will go from side to side instead of forward and back :). so i need the model to move with the rotation.

anyway if you guys could help me with that :).

sorry for asking with all this help. im sure ill get it all eventrally its just all very complicated because i dont know any of the syntax thats needed.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

cgcgames2 wrote: how can you make it so the model accelrates to that speed over time. and when the key is released the model keeps moving.
you just need to program this however you wnat it do happen. Have a variable something like float speed = 0.00; and have it change values they way you want. Use that value to move the node when the keys are pressed.
cgcgames2 wrote:another thing is when the model is rotated how do you change its Z axes. or is there another way without doing that. Basilcy i if rotat the ship 180 degrees with the A KEY then press w and s it will go from side to side instead of forward and back :). so i need the model to move with the rotation.
again, you do this easily with the code that you already have so it is just a matter of you seeing the steps needed. Maybe if you were to post your file we could help better.
cgcgames2
Posts: 24
Joined: Mon Jul 26, 2010 6:29 am

Post by cgcgames2 »

ok well im very exhausted atm. been a long day at work :). been helping out someone with landscaping so by the end of the day its hard to think of complex things XD.


um i managed to get the camera i wanted. i came up with the idea that since i am moving the model on a 2d plane. i can move the camera aswell. this way i can set the camera where i want it to be and then it only flows it if it moves and doesnt rotat with the model.

but the things i dont know how to do. i work out how to move the model forward once it has turned off of the z axis. do i have to do some calculation that checks how far the model has moved from the z axis and move it forward from how far its from the z axis?

then next thing i cant work out aswell. is i want the model to tilt. and i want it to tilt about 150 degrees. basicly think of it as a RL plane. how it banks and then turns. i want the model to bank and turn on the spot. i got the turning on the spot but if i add tilting into the eqation it thoughs of all the axis. so again i need to do somthing to keep it working with the axises.

i've tried to think of ways that can do this but im not sure how becuase i just dont have enough knowledge of the irrlicht syntax.

anyway i cant seem to attach files on this forum so i have to copy and past all the code in this post. so sorry for the spam :(. you will notice that the tilt code has been commented out for the moment.

Code: Select all

#ifdef _MSC_VER

#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "Irrlicht.lib")
#endif

#include <irrlicht.h>
#include "driverChoice.h"

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


vector3df GetIrrIn(ISceneNode* node)
{
         matrix4 mat = node->getRelativeTransformation();
         vector3df in(mat[8],mat[9],mat[10]);
         in.normalize();
         return in;
}

vector3df GetIrrUp(ISceneNode* node)
{
         matrix4 mat = node->getRelativeTransformation();
         vector3df up(mat[4],mat[5],mat[6]);
         up.normalize();
         return up;
}

vector3df GetIrrLeft(ISceneNode* node)
{
        matrix4 mat = node->getRelativeTransformation();
         vector3df left(mat[0],mat[1],mat[2]);
         left.normalize();
         return left;
}

vector3df GetPointInFront(ISceneNode* node, float distance)   
{   
       vector3df p = node->getPosition();
       p += distance * GetIrrIn(node);
       return p;   
};

vector3df GetPointInBack(ISceneNode* node,float distance)   
{   
      vector3df p = node->getPosition();
      p -= distance * GetIrrIn(node);
      return p;   
};
 
vector3df GetPointAbove(ISceneNode* node,float distance)     
{   
      vector3df p = node->getPosition();
      p += distance * GetIrrUp(node);
      return p;   
};

vector3df GetPointBelow(ISceneNode* node, float distance)     
{   
      vector3df p = node->getPosition();
      p -= distance * GetIrrUp(node);
      return p;   
};
 
vector3df GetPointInFrontAndAbove(ISceneNode* node,float d, float u)   
{   
   vector3df p = GetPointInFront(node,d);
   p.Y = GetPointAbove(node,u).Y;
   return p;     
};

vector3df GetPointInBackAndAbove(ISceneNode* node,float d, float u)   
{   
   vector3df p = GetPointInBack(node,d);
   p.Y = GetPointAbove(node,u).Y;
   return p;     
}; 

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];
};


int main()
{
	// ask user for driver
	video::E_DRIVER_TYPE driverType=driverChoiceConsole();
	if (driverType==video::EDT_COUNT)
		return 1;

	// create device
	MyEventReceiver receiver;

	IrrlichtDevice* device = createDevice(driverType,
			core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);

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

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();

	IAnimatedMesh* usership = smgr->getMesh("C:/Users/Jordan/Documents/My Received Files/Space_Ship_35.3DS");
	if (!usership)
	{
		device->drop();
		return 1;
	}
	IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(usership);

	if (node)
	{
		node->setMaterialFlag(EMF_LIGHTING, false);
		node->setMaterialTexture( 0, driver->getTexture("C:/Users/Jordan/Documents/My Received Files/Space_Ship_35_Texmap.png") );
	}

	//load camera at the right distance from the model and remove cursor
	ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(-500,600,500));
	device->getCursorControl()->setVisible(false);

	
	int lastFPS = -1;

	u32 then = device->getTimer()->getTime();

	// This is the movemen speed in units per second.
	const f32 MOVEMENT_SPEED = 2000.f;
	const f32 ROTATION_SPEED = 100.f;

	while(device->run())
	{
		// Work out a frame delta time.
		const u32 now = device->getTimer()->getTime();
		const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
		then = now;

		vector3df nodePosition = node->getPosition();
		vector3df cameraPosition = camera->getPosition();
		vector3df rotation = node->getRotation(); 

		if(receiver.IsKeyDown(irr::KEY_KEY_W))
		{
			nodePosition.Z -= MOVEMENT_SPEED * frameDeltaTime;
			cameraPosition.Z -= MOVEMENT_SPEED * frameDeltaTime;
		}
		else if(receiver.IsKeyDown(irr::KEY_KEY_S))
		{
			nodePosition.Z += MOVEMENT_SPEED * frameDeltaTime;
			cameraPosition.Z += MOVEMENT_SPEED * frameDeltaTime;
		}

		if(receiver.IsKeyDown(irr::KEY_KEY_A))
		{
			rotation.Y += ROTATION_SPEED * frameDeltaTime;
			/*rotation.Z += ROTATION_SPEED * frameDeltaTime;*/
			node->setRotation(rotation); 
		}
		else if(receiver.IsKeyDown(irr::KEY_KEY_D))
		{
			rotation.Y -= ROTATION_SPEED * frameDeltaTime;
			node->setRotation(rotation);

			/*rotation.Z -= ROTATION_SPEED * frameDeltaTime;
			node->setRotation(rotation);*/

		}

		//set postions of camera and node
		node->setPosition(nodePosition);
		camera->setPosition(cameraPosition);

		/*Set camera to look at node*/
		camera->setTarget(node->getPosition()); 

		driver->beginScene(true, true, video::SColor(255,113,113,133));

		smgr->drawAll(); // draw the 3d scene
		device->getGUIEnvironment()->drawAll(); // draw the gui environment (the l

		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
			core::stringw tmp(L"Movement Example - Irrlicht Engine [");
			tmp += driver->getName();
			tmp += L"] fps: ";
			tmp += fps;

			device->setWindowCaption(tmp.c_str());
			lastFPS = fps;
		}
	}

	/*
	In the end, delete the Irrlicht device.
	*/
	device->drop();
	
	return 0;
}

/*
That's it. Compile and play around with the program.
**/

thanks againg for all the help you been giving me. I am very greatful i dont know how i would of got as far as i have wihtout this help hehe :).

cgcgames
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

hmmm. it depends on what you are trying to do.

you are creating a 2.5D scroller it looks like. I prefer full 3D.

for example, I would have a thrust variable that keeps the node moving forward and then just adjust the position via the IN of the node.

node->setPosition( getIrrIn(node) * thrust * elapsedtime);

and then use the rotations to adjust the rotation of the craft.

if (LEFT) then rotate node left by some small amount;
if (RIGHT) then rotate node right by some small amount;
if (UP) then rotate the node up a little.
etc...

by doing this, the IN is calculated automatically based upon the rotation matrix. this means that you dont move the node in the Z or Z or Y like you are doing, instead, you simply setup the rotation of the node and let the node->setPosition( getIrrIn(node) * thrust * elapsedtime); code calculate what "move forward" means. I hope that make sense. I will be back on Monday and can write a small demo that shows what I mean if needed, but I htink that you can get it if you forget about 'MOVING' the node and only focus on rotating the node and letting the IN determine the move forward for you.

this means that you will have to release control of the camera as well though, and go back to the original method I presented.

basically.....
the user controls the thrust and rotation of the node.
the IN is calculated form that rotation
the movement code is just the IN times the THRUST
the camera is never moved manually, but is instead moved to a position relative to the node.

i hope this makes senses........:)
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

Code: Select all

#ifdef _MSC_VER 

#define _CRT_SECURE_NO_WARNINGS 
#pragma comment(lib, "Irrlicht.lib") 
#endif 

#include <irrlicht.h> 
#include "driverChoice.h" 

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


vector3df GetIrrIn(ISceneNode* node) 
{ 
         matrix4 mat = node->getRelativeTransformation(); 
         vector3df in(mat[8],mat[9],mat[10]); 
         in.normalize(); 
         return in; 
} 

vector3df GetIrrUp(ISceneNode* node) 
{ 
         matrix4 mat = node->getRelativeTransformation(); 
         vector3df up(mat[4],mat[5],mat[6]); 
         up.normalize(); 
         return up; 
} 

vector3df GetIrrLeft(ISceneNode* node) 
{ 
        matrix4 mat = node->getRelativeTransformation(); 
         vector3df left(mat[0],mat[1],mat[2]); 
         left.normalize(); 
         return left; 
} 

vector3df GetPointInFront(ISceneNode* node, float distance)    
{    
       vector3df p = node->getPosition(); 
       p += distance * GetIrrIn(node); 
       return p;    
}; 

vector3df GetPointInBack(ISceneNode* node,float distance)    
{    
      vector3df p = node->getPosition(); 
      p -= distance * GetIrrIn(node); 
      return p;    
}; 
  
vector3df GetPointAbove(ISceneNode* node,float distance)      
{    
      vector3df p = node->getPosition(); 
      p += distance * GetIrrUp(node); 
      return p;    
}; 

vector3df GetPointBelow(ISceneNode* node, float distance)      
{    
      vector3df p = node->getPosition(); 
      p -= distance * GetIrrUp(node); 
      return p;    
}; 
  
vector3df GetPointInFrontAndAbove(ISceneNode* node,float d, float u)    
{    
   vector3df p = GetPointInFront(node,d); 
   p.Y = GetPointAbove(node,u).Y; 
   return p;      
}; 

vector3df GetPointInBackAndAbove(ISceneNode* node,float d, float u)    
{    
   vector3df p = GetPointInBack(node,d); 
   p.Y = GetPointAbove(node,u).Y; 
   return p;      
}; 

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]; 
}; 


int main() 
{ 
   // ask user for driver 
   video::E_DRIVER_TYPE driverType=driverChoiceConsole(); 
   if (driverType==video::EDT_COUNT) 
      return 1; 

   // create device 
   MyEventReceiver receiver; 

   IrrlichtDevice* device = createDevice(driverType, 
         core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver); 

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

   video::IVideoDriver* driver = device->getVideoDriver(); 
   scene::ISceneManager* smgr = device->getSceneManager(); 

   IAnimatedMesh* usership = smgr->getMesh("C:/Users/Jordan/Documents/My Received Files/Space_Ship_35.3DS"); 
   if (!usership) 
   { 
      device->drop(); 
      return 1; 
   } 
   IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(usership); 

   if (node) 
   { 
      node->setMaterialFlag(EMF_LIGHTING, false); 
      node->setMaterialTexture( 0, driver->getTexture("C:/Users/Jordan/Documents/My Received Files/Space_Ship_35_Texmap.png") ); 
   } 

   //load camera at the right distance from the model and remove cursor 
   ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(-500,600,500)); 
   device->getCursorControl()->setVisible(false); 

    
   int lastFPS = -1; 

   u32 then = device->getTimer()->getTime(); 

   // This is the movemen speed in units per second. 
   const f32 MOVEMENT_SPEED = 2000.f; 
   const f32 ROTATION_SPEED = 100.f; 
   const f32 MAX_THRUST     = 10.0f;
   const f32 MIN_THRUST     = 0.0f;

   const f32 CAMERA_BACK    = 100.0;
   const f32 CAMERA_UP      = 50;

   float     thrust   = MIN_THRUST;
   vector3df rotation = node->getRotation(); 

   while(device->run()) 
   { 
      // Work out a frame delta time. 
      const u32 now = device->getTimer()->getTime(); 
      const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds 
      then = now; 

      // thrust - decrease if key not down 
      if(receiver.IsKeyDown(irr::KEY_KEY_W)) thrust += MOVEMENT_SPEED * frameDeltaTime; 
      else thrust -= MOVEMENT_SPEED * 2;

      if(receiver.IsKeyDown(irr::KEY_KEY_S)) thrust -= MOVEMENT_SPEED * frameDeltaTime;
      
      // rotate node left / right
      if(receiver.IsKeyDown(irr::KEY_KEY_A)) rotation.Z -= ROTATION_SPEED * frameDeltaTime; 
      if(receiver.IsKeyDown(irr::KEY_KEY_D)) rotation.Z += ROTATION_SPEED * frameDeltaTime;
      
      // tilt up / down
      if(receiver.IsKeyDown(irr::KEY_KEY_Z)) rotation.X -= ROTATION_SPEED * frameDeltaTime; 
      if(receiver.IsKeyDown(irr::KEY_KEY_X)) rotation.X += ROTATION_SPEED * frameDeltaTime;

      // clip these values (you could clip the rotations here also if you like
      if (thrust > MAX_TRUST) thrust = MAX_THRUST;  
      if (thrust < MIN_TRUST) thrust = MIN_THRUST;  


      // set the nodes rotation / position
      node->setRotation(rotation);
      node->setPosition(node->getPosition() + GetIrrIn(node) * thrust); 


// setup the camera

// #define FOLLOW_NODE // define - undefine this to change camera behavior

#ifdef FOLLOW_NODE

   // if you want the camera to follow the node around then do this
   camera->setPosition(GetPointInBackAndAbove(node,CAMERA_BACK,CAMERA_UP)); 

#else

  // otherwise, if you just want the camera above the node looking down
  // do this
  vector3df newPos = node->getPosition();
  newPos.Y += CAMERA_UP;
  camera->setPosition(newPos);

#endif


// comment this out to use fancy camera lookat point code
#define LOOK_AT_NODE

#ifdef LOOK_AT_NODE
      /*Set camera to look at node*/ 
      camera->setTarget(node->getPosition()); 
#else
      // try looking in front of the node instead to see what it looks like
      camera->setTarget(GetPointInFront(node,50)); 
#endif



      driver->beginScene(true, true, video::SColor(255,113,113,133)); 

      smgr->drawAll(); // draw the 3d scene 
      device->getGUIEnvironment()->drawAll(); // draw the gui environment (the l 

      driver->endScene(); 

      int fps = driver->getFPS(); 

      if (lastFPS != fps) 
      { 
         core::stringw tmp(L"Movement Example - Irrlicht Engine ["); 
         tmp += driver->getName(); 
         tmp += L"] fps: "; 
         tmp += fps; 

         device->setWindowCaption(tmp.c_str()); 
         lastFPS = fps; 
      } 
   } 

   /* 
   In the end, delete the Irrlicht device. 
   */ 
   device->drop(); 
    
   return 0; 
} 

/* 
That's it. Compile and play around with the program. 
**/
Last edited by Seven on Wed Oct 20, 2010 3:56 am, edited 5 times in total.
cgcgames2
Posts: 24
Joined: Mon Jul 26, 2010 6:29 am

Post by cgcgames2 »

ok :). sorry if i am sounding a bit think. im really not used to game programing. how do you rotat a little bit?

if (LEFT) then rotate node left by some small amount;
if (RIGHT) then rotate node right by some small amount;
if (UP) then rotate the node up a little.

im pretty sure those arnt commands. those are just and idea right?.

basicly meaning if key D is pressed, rotat model left by a an amount. however how does it know what left is?. see im strugling to under stand this a little lol. if i rotat the model in the X axis. then how would it know what left is?.

do i have to tell it what left is?. and do i have to tell it what rotating in the x axis is?

I know what im going to have to do with tilting it to only 150 degrees or so. ill basicly say keep titlting model till a certan number is reached once that has been reched no longer tilt. then put in code saying when button is released slowy move back to 0.

and yes this game is 2.5d becuase of how the game play is going to work so it cant be full 3d.

basicly how i want it to work is. when the user presses a the ship will tilt to 150 degrees or so, then also turn aswell. then when the key is released the ship stops turning and the tilt slowly moves back to 0 so the ship is flat again.

sorry if some of my wording doesnt make sence like i said been a long day so im tiered and im finding it hard to concentrate XD.

i hope you can help me with that. I got the bit about thrust :) i think i can get that working now :).

im still strugling to understand the rotating.

hope you can help me to understand a bit more :).

Thanks,
Jordan
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

np. did the code i just gave you work as expected? ( i modified your code and reposted it)
cgcgames2
Posts: 24
Joined: Mon Jul 26, 2010 6:29 am

Post by cgcgames2 »

lmao you posted as i was writing a post XD. i didnt even see that post sorry. im a bit tiered and trying to concentrate :).

ill try it now.

and then ill let you know
cgcgames2
Posts: 24
Joined: Mon Jul 26, 2010 6:29 am

Post by cgcgames2 »

the code looks like it works. im still trying to work things out.

I am a bit tired now. i think you have helped me in what i was looking for :D.

thank you so much for all your help. there is still on think im not sure i know how to do. but ill see if i can figure it out tomorrow.

its 12:10 here atm. so im going to bed :). thanks again.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

i modified the code again to try a few camera tricks. it should be a complete program at this point so feel free to hack at it to see what it can do. int he mean time, I am off to Ireland for a few days and wont have any system to chat on. I will look this back up on Monday when I return.

Seven
cgcgames2
Posts: 24
Joined: Mon Jul 26, 2010 6:29 am

Post by cgcgames2 »

hmm. is there anyway to change the pivot point? i thought i would only have to change it in 3ds max and it would work in irrlicht. but the ship still wants to pivot at the front instead of the middle
cgcgames2
Posts: 24
Joined: Mon Jul 26, 2010 6:29 am

Post by cgcgames2 »

nvm it was working. just for some reason i had to set the pivot at the back of the ship for it to be centered lol :)
cgcgames2
Posts: 24
Joined: Mon Jul 26, 2010 6:29 am

Post by cgcgames2 »

i been tryingto go through how to do it. i got your trust code. that makes sence. but what i am finding hard. is trying to do what i want lol.

i know what i want i just dont know what syntax is needed. I been looking at the Hierarchy to try and get some of the syntax i need but its hard when your dont really know what your looking for.

there is a few things i wanted to change to the trust. i dont want it to trust once the key has been released i just want it to drift. which for the most part it does until your rotat then it changes its movment to follow the rotat. i want it to drift until the key is pressed again.

thats not a major issue i think i can figure it out eventrally. but im still having major problems with rotation. in 3ds max the rotation never follows the object.

however in irrlicht the rotation does follow the object. basicly mean the axises follow the object. all of them apart from the Z axis. basicly i want the object to rotat on the Z axis. then on the y axis. Its hard to explain lol

look at this vid. its of another game. look how the ships fly. i basicly want to get the same movment as that :).


http://www.youtube.com/watch?v=hs9EctOZ ... r_embedded#!

hope that helps you understand what im after. you dont need to do all the code :). i just like an explnation of the syntax needed. kinda like what you been doing. showing me what does what and an explaintion of what its doing :).

thanks,
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

code changed to reduce thrust each frame.

model rotation will take me longer than I have this morning. will look at again on tues. basically, you need to keep the Y of the node position vector constant. you do this by getting the position, changing the Y to some set value before multiplying times the IN and movement speed.
cgcgames2
Posts: 24
Joined: Mon Jul 26, 2010 6:29 am

Post by cgcgames2 »

i fill like a noob lol :P. is the GetIrrIn a thing you defined?.

becuase it looks like its a function.

if it is what does getRelativeTransformation() do?

in(mat[8],mat[9],mat[10]);

and what is the deal with mats? i dont really get them lol. i know its ment to be 3 postions. but how does that work with the GetIrrUp?

sorry been working on the code all day and i have only just started to get it XD :).
Post Reply