Game Development Community

Rotation of an AI

by Brian · in Torque Game Engine · 05/01/2008 (12:41 am) · 5 replies

I'm trying to make a function that just rotates an AI by degrees. Here's the code I have which works great if you give it 90 degrees until you hit 270 degrees (West). When you do it again (basically, running the script 4 times in a row using 90 degrees) the bot spins back to 180 degrees (South) from 0/North. Any help would be appreciated.

function aiTurn(%ai, %degrees)
{
	// Get the current transforms.
	%xfrm = %ai.getTransform();
	%lx = getWord(%xfrm,0);
	%ly = getWord(%xfrm,1);
	%lz = getWord(%xfrm,2);
	%rx = getWord(%xfrm,3);
	%ry = getWord(%xfrm,4);
	%rz = getWord(%xfrm,5);
	%rd = getWord(%xfrm,6);
	%rx = "0";
	%ry = "0";
	%rz = "1";
	%rd += mDegToRad(%degrees);
	%ai.setTransform(%lx SPC %ly SPC %lz SPC %rx SPC %ry SPC %rz SPC %rd);	
	
}

#1
05/01/2008 (7:07 pm)
Woohoo, after sleeping on it, distracting myself with real world stuff, then mulling on it with a co-worker of mine, then playing around some more, I came up with this solution that works. Hope this helps someone else in the future.

function aiTurn(%ai, %degrees)
{
	// Get the current transforms.
	%xfrm = %ai.getTransform();
	// Translation
	%lx = getWord(%xfrm,0);
	%ly = getWord(%xfrm,1);
	%lz = getWord(%xfrm,2);
	//Rotation
	%rx = "0"; //Override the x rotation
	%ry = "0"; //Override the y rotation
	%rz = getWord(%xfrm,5);
	//Rotation Amount in Radians
	%rd = getWord(%xfrm,6);
	
	//Make sure if the AI is facing Absolute north it can be rotated.
	if (%rz == "0")
	{
		%rz = "1"; //Rotate on the Z Axis
	}
	// Make sure if the AI is facing the opposite way we rotate correctly
	if (%rz == "-1")
	{
		%rd -= mDegToRad(%degrees); //Rotate the other way on the Z Axis
	}
	// If it did not need to be updated to rotate correctly,
                // rotate normally on the Z Axis.
	else
	{
	%rd += mDegToRad(%degrees);
	}
	%ai.setTransform(%lx SPC %ly SPC %lz SPC %rx SPC %ry SPC %rz SPC %rd);	
	
}
*edited one of the comments to prevent scroll bars on this post
#2
05/01/2008 (7:47 pm)
Couldn't you just use setAimLocation()?
#3
05/01/2008 (9:52 pm)
@ Brain, wouldn't this consider to always be false ?

if ( %rz == "0" )
   .
   .
   .

if (%rz == "-1")


Maybe, you'll have to do something like this

if ( %rz $= "0" )
   .
   .
   .

if (%rz $= "-1")


Aun.

*edit ahh... sorry my mistake, it should work as you intend it
#4
05/01/2008 (10:05 pm)
Dan, the reason I didn't pursue just feeding a location is because I want the bot to be more like a real robot that has no idea on anything other than the information it gathers. Sort of like a simulation of a real robot. If I use setAimLocation() I have to feed it another object's location to aim at... then it always aims at it until I clear it. Also, I can use this function for other things like rotating a .dif file or whatever else doesn't need X/Y axis rotation.

Aun, the reason I do that is because if you're facing directly north or 0 (that's the way my bot spawns), it won't rotate at all. The way the function works is I can give it a postive number for %degrees to make it go right, and a negative number to turn left. Try it out.

Usage examples:
function aiTurnLeft(%ai)
{
		aiTurn(%ai , "-90");
}

function aiTurnRight(%ai)
{
		aiTurn(%ai , "90");
}

Another thing to note is I'm new to vector math and rotations so I'm kind of reading a lot and stumbling through it so this may not be the best way, but it works for what I'm trying to do and is pretty easy to read. If you guys have any suggestions I'd be happy to learn more.
#5
05/02/2008 (12:36 am)
Aun, I went back and checked it out and you had the right idea... except that my conditional always evaluated to true. I've made a better version.

function aiTurn(%ai, %degrees)
{
	// Get the current transforms.
	%xfrm = %ai.getTransform();

	// Translation
	%lx = getWord(%xfrm,0);
	%ly = getWord(%xfrm,1);
	%lz = getWord(%xfrm,2);

	//Rotation
	%rx = "0"; //Override the x rotation
	%ry = "0"; //Override the y rotation
	%rz = getWord(%xfrm,5);

	//Rotation Amount in Radians
	%rd = getWord(%xfrm,6);

	// Make sure if the AI is facing the opposite way we rotate correctly
	if (%rz == "-1")
	{
		%rd -= mDegToRad(%degrees); //Rotate the other way on the Z Axis
	}

	// If it did not need to be updated to rotate correctly,
                // rotate normally on the Z Axis.
	else
	{
	%rz = "1"; //Rotate on the Z Axis
	%rd += mDegToRad(%degrees);
	}
	%ai.setTransform(%lx SPC %ly SPC %lz SPC %rx SPC %ry SPC %rz SPC %rd);
}
*Edited for readability