Page 1 of 2

walking forward?

Posted: Tue Dec 30, 2003 10:17 pm
by HIM
I've been trying for a week now. My character refuses to walk forward. It walks in some relation to the gaming world. How the heck do I get it to follow its nose??

I've tried to alter X, Y and Z on the position vector. I've tried to use a matrix. But it refuses to walk in any other direction.

If someone has solved this, would you like to post the code so I can see how you did??

reinvention

Posted: Wed Dec 31, 2003 12:17 am
by rt
i thought my post from this thread (http://irrlicht.sourceforge.net/phpBB2/ ... .php?t=773) explained how to move a node in the direction that the camera is looking.. have you tried that?

if you want the character to always walk forward no matter how the camera is looking then you need to define a 'forward' vector and current rotation (although you could get these from ISceneNode)

now assuming that you start the model facing the positive X direction then try this code:

Code: Select all

//global forward direction vector and rotation for node
float curr_rotation = 0.0f;
vector3df forward(1,0,0);
// when the user presses 'W' on keyboard move node 'forward'
node->setPosition( node->getPosition() + forward );
the tricky part is to update the vector when the user presses left or right which will rotate the 'forward' vector

Code: Select all

//user has pressed left, update 'forward' vector
curr_rotation += 10;
if (curr_rotation >= 360.0f) curr_rotation -= 360.0f
forward.X = cos( curr_rotation*PI/180.0f );
forward.Z = sin( curr_rotation*PI/180.0f );

//user has pressed right, update 'forward' vector
curr_rotation -= 10;
if (curr_rotation <= 0.0f) curr_rotation += 360.0f
forward.X = cos( curr_rotation*PI/180.0f );
forward.Z = sin( curr_rotation*PI/180.0f );
i hope this helps

Posted: Wed Dec 31, 2003 12:48 am
by DarkWhoppy
Someone should write tutorial on this :P. Many people have been asking about how to do that...

Posted: Wed Dec 31, 2003 1:29 am
by RayOfAsh
There should be a tutorial that teach's you how to create a simple FPS game. That should be enough to get EVERYBODY started and be able to understand most of the code, and what their doing.

Posted: Wed Dec 31, 2003 4:02 pm
by rt
if you wanted to more tightly integrate your code with irrlicht then you could do something like this:

Code: Select all

// when the user presses 'W' on keyboard move node 'forward'
// first determin which way is 'forward'
vector3df forward( cos( node->getRotation().Y*PI/180.0f, 0, sin( node->getRotation().Y*PI/180.0f );
//now move 'forward' 10 units 
node->setPosition( node->getPosition() + forward*10.0f );

//when the user presses 'A' turn the node to the left
node->setRotation( vector3df(0,node->getRotation().Y + 10.0f,0);
//when the user presses 'D' turn the node to the left
node->setRotation( vector3df(0,node->getRotation().Y - 10.0f,0);

Posted: Wed Dec 31, 2003 6:17 pm
by keless
DarkWhoppy wrote:Someone should write tutorial on this :P. Many people have been asking about how to do that...
doesnt the TechDemo do this??

Posted: Wed Dec 31, 2003 6:20 pm
by DarkWhoppy
The TechDemo uses the FPS camera thats within the engine source.

Posted: Wed Dec 31, 2003 7:49 pm
by DarkWhoppy
rt wrote:if you wanted to more tightly integrate your code with irrlicht then you could do something like this:

Code: Select all

// when the user presses 'W' on keyboard move node 'forward'
// first determin which way is 'forward'
vector3df forward( cos( node->getRotation().Y*PI/180.0f, 0, sin( node->getRotation().Y*PI/180.0f );
//now move 'forward' 10 units 
node->setPosition( node->getPosition() + forward*10.0f );

//when the user presses 'A' turn the node to the left
node->setRotation( vector3df(0,node->getRotation().Y + 10.0f,0);
//when the user presses 'D' turn the node to the left
node->setRotation( vector3df(0,node->getRotation().Y - 10.0f,0);
I tried switching my code with what you have here, and it .. kinda works. But isn't accurate. It doesn't go by the players rotation or somethin. My model is facing the way the camera is facing.... I move it forward it decides to move left... I turn it around backwards, it moves right. :?: It just keeps moving in random directions when it rotates.

Posted: Thu Jan 01, 2004 2:27 am
by HIM
The rotation part seems to work. The walking part doesn't... the model seems to run off in another direction.

Is it the model that is contructet in a strange way perhaps?? I'm using the Sydney.md2 model..

Posted: Thu Jan 01, 2004 9:08 pm
by qwe
There's a member function of the vector3d class called rotateXZ(). Since getRotation() provides rotation in degrees(i believe) you should be able to use getRotation in conjunction with rotateXZ to manipulate the "forward" vector.

i have no idea if this is plausible or not :?

Posted: Fri Jan 02, 2004 2:36 am
by rt
HIM wrote:The rotation part seems to work. The walking part doesn't... the model seems to run off in another direction.

Is it the model that is contructet in a strange way perhaps?? I'm using the Sydney.md2 model..
i think irrlicht works in a different manner than i assumed.. try this code:

Code: Select all

// when the user presses 'W' on keyboard move node 'forward' 
// first determin which way is 'forward' 
vector3df forward( sin( node->getRotation().Y*PI/180.0f ), 0, cos( node->getRotation().Y*PI/180.0f ) ); 
//now move 'forward' 10 units 
node->setPosition( node->getPosition() + forward*10.0f ); 

//when the user presses 'A' turn the node to the left 
node->setRotation( vector3df(0,node->getRotation().Y - 10.0f,0)); 
//when the user presses 'D' turn the node to the left 
node->setRotation( vector3df(0,node->getRotation().Y + 10.0f,0)); 
i switched sin and cos, as well as switching + and - with the rotation (and fixed some syntax errors). i tested this out and it worked for me :D good luck. the model should start facing to move in the positive z direction. you might have to play around to get the initial position to look correct, but the movement code should be ok.

Posted: Fri Jan 02, 2004 6:24 am
by DarkWhoppy
Thanks :) . That works perfectly, now you should write a tutorial on explaining how it all works =P

Posted: Fri Jan 02, 2004 7:59 pm
by qwe
i think it works by taking the rotation angle and then(by using sin and cos) decides the amount of force to apply in the X and Z directions to move at the same speed in a diagonal line.

Posted: Fri Jan 02, 2004 9:31 pm
by keless
yes. basically it simplifies things by breaking your 3D forward vector into just its X-component and just the Z-component.

If you think about your rotation around the Y axis, this is the same as some angle around a point on the X-Z plane. Imagine a hypotenues sticking out from the origin based on this angle. Now, as you recall from basic Trigonometry, SOH CAH TOA: the 'opposite' side's length is equal to Sin(Hypoteneus) and the 'adjacent' size's length is equal to Cos(Hypotenenus). So, we can get Unit-Normalized Vector components in the X and Z axis by taking the Opposite and Adjacent sides from our angle.

Now, because we have a Unit-Normalized Vector, we can multiply it by a scalar value to get a vector in the right direction, with the scalar's length. (Its like multiplying 1*Scalar, where our '1' also signifies direction).

Adding this vector to our position puts us 'Scalar' number of units in the 'Forward' direction.

The end.

Posted: Fri Jan 02, 2004 9:54 pm
by rt
keless wrote:yes. basically it simplifies things by breaking your 3D forward vector into just its X-component and just the Z-component.

If you think about your rotation around the Y axis, this is the same as some angle around a point on the X-Z plane. Imagine a hypotenues sticking out from the origin based on this angle. Now, as you recall from basic Trigonometry, SOH CAH TOA: the 'opposite' side's length is equal to Sin(Hypoteneus) and the 'adjacent' size's length is equal to Cos(Hypotenenus). So, we can get Unit-Normalized Vector components in the X and Z axis by taking the Opposite and Adjacent sides from our angle.

Now, because we have a Unit-Normalized Vector, we can multiply it by a scalar value to get a vector in the right direction, with the scalar's length. (Its like multiplying 1*Scalar, where our '1' also signifies direction).

Adding this vector to our position puts us 'Scalar' number of units in the 'Forward' direction.

The end.
yah... what he said 8)