Walk forward
Walk sideways
==================================
Hi
I've seen in the forum quiet a few questions and answers about
turning scenenodes to walk forward or to face some other scenenode.
I have here some code example that should clarify things a bit for newbies.
This is certainly not the only way to do things, so feel free to post more ways
and/or corrections/enhancements that deal with this sort of problems.
I think this will help every one to find things faster.
The code should work with irrlicht 0.4.2
I assume you have something like this in your code:
Code: Select all
// the initial up direction of the hero
vector3df up(0,1,0);
// the up direction during gameplay
vector3df curup(up);
// the initial forward direction
vector3df vtHeroInitDir(0,0,1);
// the forward direction during gameplay
vector3df vtHeroCurrentDir(vtHeroInitDir);
// the hero of the game
ISceneNode* myHero;
// the bad guy
ISceneNode* myMonster;
you just call the following function
(note that this function can also be modified to turn to a point!):
Code: Select all
void faceTarget(irr::scene::ISceneNode& hero, irr::scene::ISceneNode& target){
using namespace irr;
using namespace core;
vector3df dir=vtHeroInitDir;
vector3df targetdir=target.getPosition()-hero.getPosition();
// now build the rotation matrix
vector3df z(targetdir-dir);
z.normalize();
vector3df x( up.crossProduct(z) );
x.normalize();
vector3df y( z.crossProduct(x) );
y.normalize();
transform(0,0) = x.X;
transform(0,1) = x.Y;
transform(0,2) = x.Z;
transform(0,3) = 0;
transform(1,0) = y.X;
transform(1,1) = y.Y;
transform(1,2) = y.Z;
transform(1,3) = 0;
transform(2,0) = z.X;
transform(2,1) = z.Y;
transform(2,2) = z.Z;
transform(2,3) = 0;
transform(3,0) = 0;
transform(3,1) = 0;
transform(3,2) = 0;
transform(3,3) = 1;
irr::core::vector3df rot=MatrixToEulerAngles(transform);
transform.transformVect(vtHeroInitDir,vtHeroCurrentDir);
transform.transformVect(up,curup);
hero.setRotation(rot);
vtHeroCurrentDir.normalize();
curup.normalize();
}
[url]http://irrlicht.sourceforge.net/phpBB2/ ... php?t=1288[\url]
has posted. The aproach for the 'faceTarget' function mentioned there doesn't work though.
Also, You have to keep in mind that this function rotates the hero completely, that is,
it doesn't only turn the node in the xz-plane,
which is what you normaly would want in a game where your object's can't fly.
<b>Walking forward</b> is really easy if you update the vector 'vtHeroCurrentDir' every time the direction
changes. just call 'goForward(myHero, vtHeroCurrentDir , MillisSinceLastFrame)'
Code: Select all
void goForward(ISceneNode& hero, vector3df& direction, long MillisSinceLastFrame){
// scale the direction to match different framerates
vector3df newPos=hero.getPosition()+ (direction * MillisSinceLastFrame/1000.0);
hero->setPosition(newPos);
}
Code: Select all
void goSide(ISceneNode& hero, vector3df& forward, vector3df& updir, bool left){
vector3df s=forward.crossProduct(updir);
s.normalize();
vector3df newPos=hero.getPosition() + ( s * (left?1:-1) );
}
Gorgon Zola