# Helm, plot out a course to the gamma system...

• 08-07-2006
Queatrix
Helm, plot out a course to the gamma system...
Using SIN and COS I got a virtual ship on points to rotate as well as moving it forward at any angle.

I want to be able to make my AI ship turn to the right angle to goto the target; with out having to loop my program to (in the background): check each angle, then go all the way forward to see if it collides with the target. That would take up much of my CPU time.

Know of a better way of doing it? By the way, I am using radians, not degrees.
• 08-08-2006
lightatdawn
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); }```
• 08-09-2006
VirtualAce
That only rotates around the y axis correct? Essentially in an airplane this would be yaw. I don't see anything for other axes.
• 08-09-2006
lightatdawn
Quote:

Originally Posted by Bubba
That only rotates around the y axis correct? Essentially in an airplane this would be yaw. I don't see anything for other axes.

Correct. This code isn't valid in a 3D enviroment. For some reason when I read "ship" I assumed sailng ship or whatnot in a 2D game, not a flying craft in a 3D one. Rotating in a 3D envirment using Pitch, Yaw, and Roll is more irritating. Better to use vectors in that case. However, the code given could be expanded to work in a 3D enviroment by applying a second pass with it to cover the other axis.
• 08-16-2006
Queatrix
Thanks lightatdawn, it works,

I'll have to change the code a little, my ship will turn all of the way around (to the right) rather than just turning left a little, if I move may target to it's left.