Problem with callback on the rotateTo() function
by Josias Gibbs · in Torque Game Engine · 09/21/2008 (10:02 am) · 4 replies
I've run into another minor roadblock in the AI I'm working on. I'm using the rotateTo() function in the following way:
As far as I can tell on the Torque reference guide (http://tdn.garagegames.com/wiki/Torque_2D/Reference_Guide), setting the fourth field to true in the rotateTo() function should enable callback (ie: "Whether to callback "onRotationTarget()" when the target is reached")
What I'm trying to do is: use the %this.turning variable to tell whether the current object is still rotating toward %targetRotation (ie: I don't want to have this ship start moving forward until it's finished turning toward it's desired direction ((since we are in space, and there is inertia)), so I use %this.turning to tell if this ship is done turning toward it's %targetRotation)
unfortunately, the function traderAIBehavior::onRotationTarget(%this) never gets called - which means I must be doing something wrong. I'm assuming it's some absurdly simple syntax error - am I right?
function traderAIBehavior::onRotationTarget(%this)
{
%this.turning = false;
}
function traderAIBehavior::mainLoop(%this)
{
//a bunch of code, including code that calculates %targetRotation
%this.turning = true;
%this.owner.rotateTo(%targetRotation, $turnSpeed, true, true, true, getRandom(0, 3));
}As far as I can tell on the Torque reference guide (http://tdn.garagegames.com/wiki/Torque_2D/Reference_Guide), setting the fourth field to true in the rotateTo() function should enable callback (ie: "Whether to callback "onRotationTarget()" when the target is reached")
What I'm trying to do is: use the %this.turning variable to tell whether the current object is still rotating toward %targetRotation (ie: I don't want to have this ship start moving forward until it's finished turning toward it's desired direction ((since we are in space, and there is inertia)), so I use %this.turning to tell if this ship is done turning toward it's %targetRotation)
unfortunately, the function traderAIBehavior::onRotationTarget(%this) never gets called - which means I must be doing something wrong. I'm assuming it's some absurdly simple syntax error - am I right?
About the author
#2
09/21/2008 (10:17 pm)
Woohoo - got it working.
#3
09/22/2008 (12:36 pm)
@Jonias: What did you do to get it working?
#4
I created a variable called %this.desiredDirection which defaults to a value of -1 (causing the initial "if" statement to evaluate to false). Whenever I want a ship to turn, I set %this.desiredDirection to the angle I want it to turn to. Once the ship reaches that rotation, %this.desiredDirection gets set back to -1, causing the initial "if" statement to evaluate to false.
09/28/2008 (8:28 pm)
I did this:function traderAIBehavior::mainLoop(%this)
{
//a bunch of code, including code that calculates %targetRotation
%this.desiredDirection = %targetRotation;
}
function traderAIBehavior::onUpdate(%this)
{
if (%this.desiredDirection != -1)
{
%currentDirection = ((%this.owner.rotation+360)%360);
%oldDirection = %currentDirection;
if ( (mabs(%currentDirection - %this.desiredDirection)) > 180)
//if we're going to cross the 360/0 part of the unit circle
//(in this case, between the quadrants that would normally be the first and second quadrants).
{
if (%currentDirection < %this.desiredDirection)
{
%currentDirection -= $turnTick;//logically, it seems like we need to increase the value
//of direction, but because we're gonna cross the 360/0 part of the unit circle, we actually need to
//decrease the value of direction.
if (%currentDirection > %oldDirection)//if we've crossed the 360/0 spot
{
if (%currentDirection <= %this.desiredDirection)//if we've now reached/passed the direction we want to go
{
%currentDirection = %this.desiredDirection;
%this.desiredDirection = -1;//we've reached our desired direction - nothing more needs to be done
}
}
}
else if (%currentDirection > %this.desiredDirection)//this whole elseif statement does the same thing as the if statement about 30 lines
//back, but this one happens if we have to increase the value of direction instead of decreasing it
{
%currentDirection += $turnTick;
if (%currentDirection < %oldDirection)//if we've crossed the 360/0 spot
{
if (%currentDirection >= %this.desiredDirection)//if we've now reached/passed the direction we want to go
{
%currentDirection = %this.desiredDirection;
%this.desiredDirection = -1;//we've reached our desired direction - nothing more needs to be done
}
}
}
else//direction == desiredDirection
{
%this.desiredDirection = -1;//we've reached our desired direction exactly without passing it.
}
}
else if ( (mabs(%currentDirection - %this.desiredDirection)) <= 180)
{
if (%currentDirection < %this.desiredDirection)
{
%currentDirection += $turnTick;
if (%currentDirection >= %this.desiredDirection)
{
%currentDirection = %this.desiredDirection;
%this.desiredDirection = -1;
}
}
else if (%currentDirection > %this.desiredDirection)
{
%currentDirection -= $turnTick;
if (%currentDirection <= %this.desiredDirection)
{
%currentDirection = %this.desiredDirection;
%this.desiredDirection = -1;
}
}
else
%this.desiredDirection = -1;
}
%this.owner.setRotation(%currentDirection);
}
}I created a variable called %this.desiredDirection which defaults to a value of -1 (causing the initial "if" statement to evaluate to false). Whenever I want a ship to turn, I set %this.desiredDirection to the angle I want it to turn to. Once the ship reaches that rotation, %this.desiredDirection gets set back to -1, causing the initial "if" statement to evaluate to false.
Associate James Ford
Sickhead Games