I know that many similar questions have been already posted on the forum, but I can't find a clear, 'universal' answer to the problem of character movement.
The queston is: What is the best way to move a character from a point (P1) to another (P2) so that he faces the direction of movement?
In other words, the character is at position X1, Y1 (point P1) and he should move to position X2, Y2 (point P2), facing the direction between P1 and P2. The idea is similar to the controller movement of World of Warcraft 3 / Baldur's Gate / Heroes of Might and Magic V.
moving a character from a point to another
Re: moving a character from a point to another
I hope I understand the question
if the player is at position1 and and looking direction2, then you can simply use the 'in' vector of the scenenode to move in that direction.
and you would use it like this.........
playerSceneNode->setPosition(getIn() * movement speed);
this would move the player 'forward' by some amount
if you wanted to strafe left you could use
and use it like this.......
playerSceneNode->setPosition(getLeft() * movement speed);
which would strafe the player to the left.
it would be better to use the elapedtime of the frame to make for smoother movement though
playerSceneNode->setPosition(getIn() * movement speed * elapsedtime);
playerSceneNode->setPosition(getLeft() * movement speed * elapsedtime);
let me know if you need more info.
if the player is at position1 and and looking direction2, then you can simply use the 'in' vector of the scenenode to move in that direction.
Code: Select all
virtual vector3df getIn()
{
if (getPrimarySceneNode())
{
matrix4 mat = getPrimarySceneNode()->getRelativeTransformation();
vector3df in(mat[8], mat[9], mat[10]);
in.normalize();
return in;
}
else return vector3df(0, 0, 0);
}
playerSceneNode->setPosition(getIn() * movement speed);
this would move the player 'forward' by some amount
if you wanted to strafe left you could use
Code: Select all
virtual vector3df getLeft()
{
if (getPrimarySceneNode())
{
matrix4 mat = getPrimarySceneNode()->getRelativeTransformation();
vector3df left(mat[0], mat[1], mat[2]);
left.normalize();
return left;
}
else return vector3df(0, 0, 0);
}
playerSceneNode->setPosition(getLeft() * movement speed);
which would strafe the player to the left.
it would be better to use the elapedtime of the frame to make for smoother movement though
playerSceneNode->setPosition(getIn() * movement speed * elapsedtime);
playerSceneNode->setPosition(getLeft() * movement speed * elapsedtime);
let me know if you need more info.
Re: moving a character from a point to another
of course, this is for my own framework, you would rewrite the code to something like this......
Code: Select all
vector3df getLeft(ISceneNode* node)
{
if (node)
{
matrix4 mat = node->getRelativeTransformation();
vector3df left(mat[0], mat[1], mat[2]);
left.normalize();
return left;
}
else return vector3df(0, 0, 0);
}
Re: moving a character from a point to another
if you are wnating to 'face' towards the position2 then you could use this
which will abruptly turn the player and face him towards the position, and used like this.....
however, you may want to slowly turn the player before you start moving, in which case I will need more space for the answer
Code: Select all
void lookAt(ISceneNode* node, vector3df pos)
{
// rotate to face target pos
vector3df nodePos = node->getAbsolutePosition();
vector3df targetPos = pos;
vector3df diff = targetPos - nodePos;
node->setRotation(diff.getHorizontalAngle());
}
Code: Select all
// look at the enemy
lookAt(playerSceneNode, enemySceneNode->getAbsolutePosition());
// move towards the enemy
playerSceneNode->setPosition(getIn(playerSceneNode) * movement speed * elapsedtime);
however, you may want to slowly turn the player before you start moving, in which case I will need more space for the answer
-
- Posts: 51
- Joined: Fri May 30, 2014 12:55 am
Re: moving a character from a point to another
I create a timer by storing the current time from one movement and then checking it before the next movement. The lower the time difference you check for. The faster the model moves.
In your main loop you then do something like
PlayerHandler::process();
Then in the player handler you loop through all the current players ect. Check to see if bools in the Player class are set these bools would state weather a movement update is required. You can then use if statements to check to see if it has reached the position and you would then move the scene node depending on where it needs to move to. So it would move like 1 position each time the timer delay has been reached.
Thats how I do it in my game anyway
In your main loop you then do something like
PlayerHandler::process();
Then in the player handler you loop through all the current players ect. Check to see if bools in the Player class are set these bools would state weather a movement update is required. You can then use if statements to check to see if it has reached the position and you would then move the scene node depending on where it needs to move to. So it would move like 1 position each time the timer delay has been reached.
Thats how I do it in my game anyway