Okay... lets look at your code line by line...
I'm going to repost your code, with Comments and Questions added
I suggest, that the best way for you to answer my questions, is to repost this, with even more comments of your own added
//tell me about dir and angle, what do they represent?//is one the target and one the current orientation? which is which?bool Agent::LookAtDirection(Vector3f& dir, float angle, float speed /*=0*/){ if(!speed) speed = GetMaxSpeed(); dir.Normalize(); //is this returning the current direction your character faces? Vector3f direction = GetLocalDirection(); Vector3f upVector; GetWorldRotation().TransformVector(&upVector, Vector3f::k); //here you seem to be finding the dot produce between direction and dir //but I dont know exactly what each one means... //if the angle between them is small... this should return near1 //if they are near perpendicualr, it shoudl return near 0 //obtuse angles are negative... //it tells nothing about rotating left or right, just magnitude //are you sure that dot product is what you wanted? //it's not Exactly the same as the actual angle between them... //BUG? float actualAngle = direction.Dot(dir); float error; //as usual, what is 'angle'? if(actualAngle >= angle) error = 0; else if(actualAngle > 0)//both of these branches are the same!!?? error = angle - actualAngle; else //-actualangle+angle is the same as angle-actualangle above //this IF doesnt actually do anything //BUG? error = -actualAngle + angle; //not sure where you're going with this part... //complicated, can you explain? Vector3f c; float amount = 1; c.Cross(direction, dir); if (upVector.Dot(c) < 0) amount = -amount; float amountAux = m_pidController.ControlDecision( -amount * (error)); std::string text("PIDController returned: "); SharedTools::RealToString(amountAux, text); FE_LOG(text); SetDesirableRotateZSpeed(amountAux); if ( amountAux < 0.05 && amountAux > -0.05) return true; return false;}
Here's how I might do it (if debugging all my comments above doesnt work)
(its good to help think about it anyway)
a lot simpler anyhow
given:
vector goaldirection //aim here
vector currendirection //currently we face here
vector up //the world UP direction
...
vector rightside=cross(currendirection,up) //this might be backwards
float turnapprox= dot(goaldirection,rightside)
//turnapprox will return +1 when the direction to turn is near rightside
//-1 when direction to turn is opposite to rightside
//0 when nearly centered
//potential bug, it doesnt check if goal is in front or back of himself
//only left/right side comparision, this is OK for most cases, since we just turn...
float amountturn = m_pidController.ControlDecision( turnapprox);