Ok im very a much a noob at anything to do with AI but ive just tried to do something i have done before in a different project, which worked, where an ai character follows a target. however for some reason today, even with many different methods to find the angle to rotate by, it refuses to work! ive been staring at it for so long now that i really need someone to cast a fresh eye over it! is my logic just totally messed up? is there a better way of doing it? any suggestions on different ways of approaching this?
RVector diff = this->Target->getPos() - this->getPos(); //difference between destination point and current point
float distFrom = sqrt(diff.x*diff.x + diff.y*diff.y + diff.z*diff.z); //distance between two points
if(distFrom > this->Target->BoundingSphere+this->BoundingSphere) //if it is not already close enough to its target
{
RVector aPos = this->Target->getPos();
aPos.y = 0;
float dirToTarget; //direction angle to target
diff.y = 0.1; //this stops errors in normalize
diff.Normalize();
dirToTarget = (diff.Angle(RVector(0,0,-1))); //this is meant to give me the angle of the vector from the enemy to the player in relation to the neg z axis, i think this is where the problem is but it has worked before
float sinYaw = (float)sin(dirToTarget);
float cosYaw = (float)cos(dirToTarget);
//
// //change pos
RVector position = this->getPos();
float theSpeed = this->getSpeed();
position.x += (float)((cosYaw) * theSpeed); //facing that way move along by speed
position.z += (float)((sinYaw) * theSpeed);
this->setPos(position);
above is a snippet of code that is the current state of it, its very messy as they all used to be in neat other parts of the program but ive just hacked it all together in one bit just to see if it will work an so far it hasnt! at the moment, the ai character will start moving at what seems like a 45degree angle down z (as if its following line x = z) and if the target (i.e. me the player) gets in the way of that path the object moves around me anticlockwise, which is interesting behavoir and no doubt useful for something else but not for what i want! i want the AI character to come to me not move around me to go to some random area on the map! i have tried different ways of getting the angle including this very long way round:
RVector aPos = Target->getPos();
RVector a = RVector(this->getPos().x,1,this->getPos().z);
RVector b = RVector(aPos.x,1,aPos.z);
RVector c = RVector(aPos.x,0,this->getPos().z);
float sides[2];
sides[0] = (a-c).Length();
sides[1] = (a-b).Length();
float Angle = (acos(sides[0]/sides[1]));
this->setAngle(Angle);
which did pretty much the same thing, so im assuming that my logic is flawed but im sure this how i have done it time and time again! any help would be greatly appreciated, the problem could be staring me right it the face but i just cant see it, im sure im being very dumb and i will probably kick myself when someone posts an answer! Thank you for taking the time to look at my post and thank you in advance for any replies! p.s. im very tired so excuse the terrible grammer and spelling!