walking forward?

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.
HIM
Posts: 13
Joined: Sat Dec 20, 2003 12:22 am
Location: Stockholm, Sweden
Contact:

walking forward?

Post 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??
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

reinvention

Post 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
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post by DarkWhoppy »

Someone should write tutorial on this :P. Many people have been asking about how to do that...
RayOfAsh
Posts: 56
Joined: Wed Dec 24, 2003 4:08 am
Location: San Diego
Contact:

Post 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.
Image
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Post 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);
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post 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??
a screen cap is worth 0x100000 DWORDS
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post by DarkWhoppy »

The TechDemo uses the FPS camera thats within the engine source.
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post 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.
HIM
Posts: 13
Joined: Sat Dec 20, 2003 12:22 am
Location: Stockholm, Sweden
Contact:

Post 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..
qwe
Posts: 112
Joined: Sun Dec 28, 2003 12:54 am
Location: Oregon, USA

Post 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 :?
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Post 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.
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post by DarkWhoppy »

Thanks :) . That works perfectly, now you should write a tutorial on explaining how it all works =P
qwe
Posts: 112
Joined: Sun Dec 28, 2003 12:54 am
Location: Oregon, USA

Post 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.
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post 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.
a screen cap is worth 0x100000 DWORDS
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Post 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)
Post Reply