[Help please] Movement problem

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
Daggio
Posts: 9
Joined: Mon Oct 20, 2008 7:04 am
Location: Surabaya, Indonesia

[Help please] Movement problem

Post by Daggio »

Hi, I'm new to irrlicht and this is my first post so first of all I wanna say: Hello guys! :D

Well, right now I'm making a project in irrlicht and there's a movement problem

the movement keys are:
W - go forward
S - go back
A - strafe left
D - strafe right
Q - rotate left
E - rotate right
Spacebar - jump

everything's right except for two things, the jumping code and the direction of the character's movement

at first if I press W the character is going forward without any problem but when I move it after rotating it (let's just say I rotate it to the right) it's strafing left and not moving forward.

so what I want to know is how do I move the character according to where it's facing

secondly the jump code,when I press spacebar the screen becomes jaggy (or jagged,hm?) maybe because the jumping code is fighting against the gravity and collision detection code,so how do I jump right?

thanks in advance to anyone who helps,and this is my code if you wanna know:

in the MyEventReceiver class:

Code: Select all

if(event.EventType == EET_KEY_INPUT_EVENT)
		{

			if(event.KeyInput.Key == KEY_KEY_W)
			{
				up = event.KeyInput.PressedDown;
			}

			if(event.KeyInput.Key == KEY_KEY_S)
			{
				down = event.KeyInput.PressedDown;
			}

			if(event.KeyInput.Key == KEY_KEY_A)
			{
				left = event.KeyInput.PressedDown;
			}

			if(event.KeyInput.Key == KEY_KEY_D)
			{
				right = event.KeyInput.PressedDown;
			}

			if(event.KeyInput.Key == KEY_KEY_Q)
			{
				rotateleft = event.KeyInput.PressedDown;
			}

			if(event.KeyInput.Key == KEY_KEY_E)
			{
				rotateright = event.KeyInput.PressedDown;
			}

			if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown && event.KeyInput.Key == KEY_SPACE)
			{
				jump = true;
			}
			else
			{
				jump = false;
			}

		}
in the game loop:

Code: Select all

			core::vector3df c = AlanNode->getPosition();
			core::vector3df d = AlanNode->getRotation();

			if(up)
			{
				c.Z -= KecepatanGerak;
			}

			if(down)
			{
				c.Z += KecepatanGerak;
			}

			if(left)
			{
				//d.Y -= 0.1f;
				c.X += KecepatanGerak;
			}

			if(right)
			{
				//d.Y += 0.1f;
				c.X -= KecepatanGerak;
			}

			if(rotateleft)
			{
				d.Y -= 2.0f;
			}

			if(rotateright)
			{
				d.Y += 2.0f;
			}

			if(jump)
			{
				c.Y += 10.0f;
			}

			AlanNode->setRotation(d);

			AlanNode->setPosition(c);

			camera->setTarget(c);
note: KecepatanGerak is a global float variable that determines the movement speed :D
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Not entirely sure about the jumping problem but for moving in the direction the character is facing you need to first define which way he's facing. An Irrlicht node has no concept of which direction it's facing, imagine it's just a cube and which way does a cube face? It doesn't face any direction at all really. But as you've probably got a character which your node is representing it's probably got a direction it appears to be facing. Like the Sydney model from the tutorials, it's fairly clear which direction she should move due to the look of the model.

So basically it's dependent on your model as to which direction it's facing. First off let's guess that it's facing down the positive Z axis, into the screen.

So we do this:

vector3df dir(0,0,1); // the direction our node is initially facing
matrix4 mat = node->getAbsoluteTransformation();
mat.rotateVect(dir); // after this 'dir' is pointing in the way our node is currently 'facing'
node->setPosition(node->getPosition() + dir); // this moves our node 1 unit in the direction it's facing

Now the 'dir' guess may not be right for your model so try a few different values until you get it right. It's most likely to be facing along one axis, probably either X or Z so try positive and negative 1 for those until you discover it!
Image Image Image
Daggio
Posts: 9
Joined: Mon Oct 20, 2008 7:04 am
Location: Surabaya, Indonesia

Post by Daggio »

I don't know what's wrong the code just didn't work

I wrote the

Code: Select all

	core::vector3df dir(0,0,-1);
	core::matrix4 mat = LefNode->getAbsoluteTransformation();
	mat.rotateVect(dir);
before the main loop (the 'while(device->run()' ) and the

Code: Select all

				LefNode->setPosition(LefNode->getPosition() + dir);
inside the if statement that returns true if W is pressed,but when I pressed W the node didn't move, I don't know what's wrong

EDIT:

Don't mind my writings up there, I've fixed it and now it works!

thanks for helping me JP! :D
MasterM
Posts: 128
Joined: Sat Oct 20, 2007 2:38 am
Location: netherlands antilles, Curacao

Post by MasterM »

Hmmm, just a simple question...what does

Code: Select all

matrix4 mat = node->getAbsoluteTransformation(); 
means?
and what is the difference between absolute(for example

Code: Select all

getabsoluteposition
) and the other normal fuctions(like for example

Code: Select all

getposition
)?
C++ is not the Magdalena...it takes patience...shes like the well aged prostitute, it takes years to learn her tricks! she is cruel...laughs at you when you are naked...
Life of a programmer == Const abuse
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

one returns the absolute position and the other returns the relative position of a node... :lol:

well, this makes no difference as long as the node has no parent node...
the absolute position is the node's position in the world...
the relative position is it's position "relative" to it's parent...
look at this pic:
Image
node b is a child of node a...
so node a's absolute position is 10,10
node b's absolute position is 20,20
node a's relative position is 10,10, because it has no parent (in fact the world is it's parent)
node b's relative position is 10,10, because node a is it's parent
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
MasterM
Posts: 128
Joined: Sat Oct 20, 2007 2:38 am
Location: netherlands antilles, Curacao

Post by MasterM »

@Acki thanks for the answer it makes lots more sense now :)
C++ is not the Magdalena...it takes patience...shes like the well aged prostitute, it takes years to learn her tricks! she is cruel...laughs at you when you are naked...
Life of a programmer == Const abuse
Post Reply