control

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.
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

control

Post by Cleves »

Hey all,

I wrote this code for controling my spaceship:

if(event.KeyInput.Key == KEY_KEY_W){
vector3df Target(1,0,0);
matrix4 mat;
mat.setRotationDegrees(nodeRotation);
mat.transformVect(Target);
Target.normalize();
v+= Target;
node->setPosition(v);
}
It compiles good,when i run the program it works but when i press the W key for the movment it crashes.The debugger tells me something is wrong with node->setPosition(v);
but i can't find what.

Any ideas?
Thanks
gastar
Posts: 9
Joined: Wed Sep 24, 2003 7:52 am

Post by gastar »

Hi,

are you sure v and node exists ?
your description sounds like one of you variables is not initialized correct.

hope i could help
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post by Cleves »

Yes, they exsist i init them before as global variables
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post by Cleves »

I can post my full code if it will help you...cause i'm trying to figure it for days and i just can't find the problem...
gastar
Posts: 9
Joined: Wed Sep 24, 2003 7:52 am

Post by gastar »

Hi,

yes more code would be usefull.
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post by Cleves »

ok here is the mesh i want to control:
//Loads 1st mesh
IAnimatedMesh* scout = smgr->getMesh("Data/scout.3DS");

if(scout)
{
node = smgr->addOctTreeSceneNode(scout->getMesh(0));
node->setMaterialFlag(EMF_LIGHTING, false);
};

if(node)
node->setPosition(vector3df(-2000,-230,-1249));

I delcare node as global,at the begining.
t

Post by t »

show your code for node.
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post by Cleves »

Here is my global declarations:

ICameraSceneNode* camera = 0;
ISceneNode* skyboxNode;
IrrlichtDevice *dvc;
ISceneNode* node = 0;
vector3df v;
vector3df nodeRotation;
t

Post by t »

did you initialise v to 0,0,0 anywhere in the program?

I tried the code above (before you posted the declarations) and it worked when properly initialised..... but to get the movement fluid etc, i did some things differently.
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post by Cleves »

Can you plz show me how you did the movment?
t

Post by t »

Sure. Here's my code.

Dec's.

Code: Select all

core::vector3df shipRotation;
core::vector3df ship_velocity;
core::vector3df shipspeed(3.0,3.0,3.0);

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public: 
	virtual bool OnEvent(SEvent event)
	{    	if(event.KeyInput.Key == KEY_KEY_W)
		{ 
			if (event.KeyInput.PressedDown){
			
			core::vector3df Target(0,0,1); 
			core::matrix4 mat; 

			mat.setRotationDegrees(shipRotation); 
			mat.transformVect(Target); 
			Target.normalize(); 
			ship_velocity+= Target * shipspeed.Z; 

			
			} 

		}
	if(event.KeyInput.Key == KEY_KEY_A)
		{ 
			if (event.KeyInput.PressedDown)
			{
					shipRotation.Y -=  10;

			shipnode->setRotation(shipRotation);
			
			} 

		}
		
		
		if(event.KeyInput.Key == KEY_KEY_D)
		{ 
			if (event.KeyInput.PressedDown)
			{	shipRotation.Y += + 10;

			shipnode->setRotation(shipRotation);
			
			} 

		}


		if (camera)      
			return camera->OnEvent(event);
	
		return false; 
	}
};

My updatePlayer Code, which is called every frame from the gameloop.

Code: Select all

void UpdatePlayer()
{
// ......
	shipnode->setPosition(shipnode->getPosition() + ship_velocity); 
// ....
}
Now the main loop defined in main()

Code: Select all

	while(device->run())
	{
		if (device->isWindowActive())	
		{

		driver->beginScene(true, true, video::SColor(0,100,100,100));

		// switch based on game state here............ 

		UpdatePlayer();
     // ........... etc.. 
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post by Cleves »

Ya it seems to work fine but how can i reduce the speed of the movment?

Thanks
Guest

Post by Guest »

Change the shipspeed values from, 3.0, 3.0, 3.0 to, say, 1.0, 1.0,1.0

and secondly set a test using an if statement to ensure the ship_velocity vector stays under a certain amount. eg if (ship_velocity.X > 3 ) ship_velocity.X = 3; do that for each component.
t

Post by t »

guest was me btw :). I should set up an account.
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post by Cleves »

Everything works perfect now.
But i want to add is when the key isn't pressed the ship stops,cause now if the key isn't pressed they ship continues to move.

Thanks again T.
Post Reply