Page 1 of 2

control

Posted: Sat Oct 04, 2003 6:04 pm
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

Posted: Sat Oct 04, 2003 6:27 pm
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

Posted: Sat Oct 04, 2003 7:03 pm
by Cleves
Yes, they exsist i init them before as global variables

Posted: Sat Oct 04, 2003 7:08 pm
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...

Posted: Sun Oct 05, 2003 7:18 am
by gastar
Hi,

yes more code would be usefull.

Posted: Sun Oct 05, 2003 2:43 pm
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.

Posted: Sun Oct 05, 2003 2:46 pm
by t
show your code for node.

Posted: Sun Oct 05, 2003 6:16 pm
by Cleves
Here is my global declarations:

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

Posted: Sun Oct 05, 2003 6:31 pm
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.

Posted: Sun Oct 05, 2003 6:55 pm
by Cleves
Can you plz show me how you did the movment?

Posted: Mon Oct 06, 2003 2:19 am
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.. 

Posted: Mon Oct 06, 2003 11:46 am
by Cleves
Ya it seems to work fine but how can i reduce the speed of the movment?

Thanks

Posted: Mon Oct 06, 2003 1:20 pm
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.

Posted: Mon Oct 06, 2003 1:21 pm
by t
guest was me btw :). I should set up an account.

Posted: Mon Oct 06, 2003 1:45 pm
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.