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

This is a discussion on Helm, plot out a course to the gamma system... within the Windows Programming forums, part of the Platform Specific Boards category; Using SIN and COS I got a virtual ship on points to rotate as well as moving it forward at ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Post 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.

  2. #2
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    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);
    }
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    That only rotates around the y axis correct? Essentially in an airplane this would be yaw. I don't see anything for other axes.

  4. #4
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    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.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operating System
    By Houssen in forum C Programming
    Replies: 18
    Last Post: 04-22-2008, 12:51 PM
  2. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 12:04 PM
  3. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 06:56 PM
  4. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 06:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21