Hi! I'm having a lot of problems doing a LookAt function. The lookat function should rotate the agent so it faces some position, or direction. Here is the function:
bool Agent::LookAtDirection(Vector3f& dir, float angle, float speed /*=0*/)
{
if(!speed)
speed = GetMaxSpeed();
dir.Normalize();
Vector3f direction = GetWorldDirection();
Vector3f upVector;
GetWorldRotation().TransformVector(&upVector, Vector3f::k);
float actualAngle = direction.Dot(dir);
Vector3f c;
float amount = 1;
c.Cross(direction, dir);
if (upVector.Dot(c) < 0)
amount = -amount;
if ( !((actualAngle >= angle)))
{
SetDesirableRotateZSpeed(-amount * speed);
return false;
}
}
As you notest I have a variable named amount this is used to determine to wich direction should the agent turn, to the left or to the rigth. But sometimes the agent passes the angle, and enters in the if, then the amount change the signal and agent turns to the other side. So the agent does'nt move and keeps shaking. The angle is the aperture angle so it is safe to say, ok you are aligned. The angle should vary accordingly with the distance. So as longer the distance the angle should be closer to 1, else it should be closer to 0.85 or something like that. For the angle I have this function:
float Agent::CalculateAngle(const Vector3f& pos)
{
float sqrdist = GetWorldPosition().SquareDistance(pos);
float alpha = sqrdist / (1600 + sqrdist);
//alpha = cos(pi / 2 - atan(sqrdist));
// alpha = sqrdist / sqrt( 500 + sqrdist * sqrdist );
return ClampMinMax(alpha, 0.85f, 0.97f);
}
The comment code is some experience based on a post in this forum to. Basically I've tried to change the aperture of the angle, but or it is to much and the agent thinks that is in front of something and it isn't or else it starts shaking :p. I've tried to make some signal test, I mean when the variable amount changes its signal i return true. But it makes the same shake problem sometimes. I've also tried to slow down the rotation speed so it should not "jump" through the angle, without any positive result also. Now I'm running out of ideias. :p Have anyone passed through the same problem? Any more ideas?