Heres a function snippet straight from an old game. There are a couple custom Macros which you'll have to replace with something equivelant. The Macro names will indicate precisely what they do. I'm not sure its the best implimentaion (as I mentioned, its an old game of mine) but I'm fairly sure it works. It should rotate your source object an "Amount" closer to your target in the closest direction to rotate in. Code Warrantee: As Is.
Code:
C3DEntity & C3DEntity::PlanarLookAt(const D3DXVECTOR3 lookatPoint, float Amount)
{
float
RealXTarget = lookatPoint.x - x, //x is this objects x coordinate
RealZTarget = lookatPoint.z - z, //z is this objects z coordinate
destRot = fRot, //fRot if this objects Rotion in Radians
Rd,
Ld;
//Get the Effects Rotation value based on its trajectory
if (RealZTarget != 0.0f)
{
destRot = float( atan(RealXTarget / -RealZTarget) );
if (RealZTarget < 0.0f)
destRot += _MATH_PI;
}
else
{
if (RealXTarget < 0.0f)
destRot = 1.5707f; //Hardcoded for speed //DegreesToRadians(90.0f);
else
destRot = 4.7122f; //Hardcoded for speed //DegreesToRadians(270.0f);
}
//determine which direction to rotate
if (fRot > destRot)
{
Rd = fRot - destRot;
Ld = (destRot + 3.14f) - fRot;
}
else
{
Rd = (destRot + 3.14f) - fRot;
Ld = fRot - destRot;
}
//Make sure Ld is posative
POSATIVE(Ld);
//Rotate in the closest direction
if (Rd < Ld)
{
PlanarRotate(-Amount);
if (fRot < destRot)
fRot = destRot;
}
else
{
PlanarRotate(Amount);
if (fRot > destRot)
fRot = destRot;
}
return (*this);
}