I don't know how much you know about vectors - but for this case just think of them as arrows pointing from one point to another (that gives you a direction and a length). Getting your "direction" which is just an arrow from the object to your character (or camera) is easy (subtraction works as both points are given relative to (0,0,0) so if you subtract them you get just the small way between them).
For the other case - moving stuff away from the character or camera you need again one arrow pointing away from you and then you have to rotate it so it's identical to the camera-direction. The first part is probably what confuses you - which vector to use before the rotation. That vector is not your "direction" vector (that one is already rotated). But it's your base vector which is just 1,0,0 or 0,1,0 or 0,0,1. Mathematically it does not matter - you can use any of them. But you want probably to use the one that ensures the nose of your model is in the front and the ass in the back :-) If you don't know how your models are exported (depends entirely on your artist and modeling tool) just try all 3 (well 0,1,0 is unlikely, so probably one of the other 2).
So it's for example (not tested):
- cpp Code: Select all
irr::core::vector3df base(0,0,1); // or (1,0,0)
// don't know your character-class, but you probably have an irrlicht-node in there
// You can also use the camera instead, but make sure bindTargetAndRotation is set to true then
irr::core::matrix4 mat = character->node->getAbsoluteTransformation(); // this matrix has the rotation of your node (player or cam)
mat.rotateVect(base); // rotate your base vector by the same rotation as the camera was rotated
irr::core::vector3df moveStep = base * stepSize * deltaTime; // give it a nice length
object->node->setPosition( object->node->getPosition() + moveStep ); // move it by one step
edit: Hm, wait a second... for your case you can have it way easier. No matrices needed. You can get the direction vector for camera simply by:
- cpp Code: Select all
cam->getTarget() - cam->getAbsolutePosition();
You can then use this vector - normalize it (normalizing just sets the length to 1 so you know how long your step will be) and multiply by some stepsize and deltaTime. And then add it to your object position.
Although it won't hurt learning about matrices as well, sooner or later you will also need them :-)