I'm trying to code AI for a base class for zombies called CBaseZombie, and it's proving quite a challenge, all I want him to do at the moment is if the camera is a certain distance away from the zombie, then make the zombie slowly move toward the camera. Here is what I have so far:
This sort of works. It moves the zombie towards the camera, but only on the Z axis, I also want it to move toward the camera on the X axis. Can someone give me some ideas or pseudo code to achieve this, I don't want you to just give me the code.
#1 Most Common Error:
Reality Error: Object behind keyboard cannot program.
void CBaseZombie::Update(ICameraSceneNode* cam)
{
if(node)
{
pos = node->getPosition();
rot = node->getRotation();
core::vector3df vec = cam->getPosition() - pos; // the vector towards the camera from the zombie's position
// calculate how much you want to move towards the camera this frame, I'm just going to do one unit by normalising the vector;
vec = vec.normalise(); // i think that's the right code, check the docs to be sure
// move the zombie
node->setPosition( node->getPosition() + vec );
}
obviously you also want to rotate the zombie slowly towards the camera, there's code elsewhere on the forum showing how you can do that.
you may be interested to look at IrrAI if all this is a bit much for you... you could atleast get some pointers from looking at the code if you didn't want to use all of it.
That's because it's moving one unit every frame. If your game is running at 60fps, for example, then every second the zombie will move 60 units.
What you really want to do is to pass in the elapsed time since the last frame and use that, together with a defined speed of movement for your zombie, to calculate the distance it should move and you multiple the normalised vector by that.
If you search the forum for elapsed AND time then I'm sure you'll find some code showing you how to easily calculate it. It basically gives you consistent movement regardless of your framerate.
Hey thanks, I searched for that and I got LOTS of posts, I didn't know which one to go on, so I chose a random one, it was calleed "syncing the game to a constant fps." and I got this code from it:
That's not what you're after and should keep looking through the other threads to try and find what it is you need. Though you don't really know what you need so I'll throw you a bone:
ITimer* pTimer = pDevice->getTimer();
u32 uLastTime = pTimer->getTime();
while( pDevice->run() )
{
u32 uCurrentTime = pTimer.getTime();
u32 uElapsedTime = uCurrentTime - uLastTime;
uLastTime = uCurrentTime;
// now you do all your necessary updates in which you can use the elapsed time.
// the distance you want to move your zombie might go something like this:
f32 fDistance = uElapsedTime * fZombieSpeed;
// so you multiply the normalised vector within your update function in your previous post with the distance
vec = vec.normalise() * fDistance;
// now do your render stuff
}
fZombieSpeed you will have to trial and error to find a nice speed for the zombie. The elapsed time will be in milliseconds (i think) so you'll probably want a rather small fZombieSpeed... 0.05f might be a good place to start, even smaller may be better