Game Development Community

Controlling Turrets

by Dustin Williams · in Torque Game Engine · 12/30/2004 (10:16 pm) · 7 replies

I downloaded Paul Dana's Turret code and I just got it working. However, rather than having ai or player controled turrets, I want the player to adjust the turret position variables (the angles it's pointing at) through a gui dialog box (I already have the box) and I want the player to be able to click another button or a keyboard key to fire the turret. In otherwords, the player controls the turret through a dialog box rather than controlling it via mouse.

I'm creating a physics simulation with a cannon. The turret is going to be a cannon, and the user will be able to 'fine tune' it's positioning through a dialog box.

The main question being:
How do I control the turret's pointing position through code (preferably with degrees)?

#1
12/30/2004 (10:47 pm)
I don't think there's any console methods that move that control the turret, but you can make one.
#2
12/31/2004 (2:07 pm)
How do I do that? What code do I need to change/add?
#3
12/31/2004 (2:51 pm)
Take a look in the turret source files and see if you can force a move event with a console method.
#4
12/31/2004 (10:13 pm)
These are the available console function from aiTurret.cc. I'm a fairly experience programmer, but this is my first time messing with Torque engine code. It seems to me that I need to write my own console methods manipulating similar variables as the ones below. What will the code for those look like? How will I use those function in script?

// --------------------------------------------------------------------------------------------
// Console Functions
// --------------------------------------------------------------------------------------------

/**
 * Tells the AI to fire a given ShapeBaseImage
 */
ConsoleMethod( AITurret, fire, void, 3, 3, "ai.fire( %slot );" )
{
   AITurret *ai = static_cast<AITurret *>( object );
   ai->fire( dAtoi(argv[2]) );
}

/**
 * Tells the AI to aim at the location provided
 */
ConsoleMethod( AITurret, setAimLocation, void, 3, 3, "ai.setAimLocation( \"x y z\" );" )
{
   AITurret *ai = static_cast<AITurret *>( object );
   Point3F v( 0.0f,0.0f,0.0f );
   dSscanf( argv[2], "%f %f %f", &v.x, &v.y, &v.z );

   ai->setAimLocation( v );
}

/**
 * Returns the point the AI is aiming at
 */
ConsoleMethod( AITurret, getAimLocation, const char *, 2, 2, "ai.getAimLocation();" )
{
   AITurret *ai = static_cast<AITurret *>( object );
   Point3F aimPoint = ai->getAimLocation();

   char *returnBuffer = Con::getReturnBuffer( 256 );
   dSprintf( returnBuffer, 256, "%f %f %f", aimPoint.x, aimPoint.y, aimPoint.z );

   return returnBuffer;
}

/**
 * Sets the turrets target object
 */
ConsoleMethod( AITurret, setAimObject, void, 3, 3, "ai.setAimObject( obj );" )
{
   AITurret *ai = static_cast<AITurret *>( object );
   
   // Find the target
   GameBase *targetObject;
   if( Sim::findObject( argv[2], targetObject ) )
      ai->setAimObject( targetObject );
   else
      ai->setAimObject( 0 );
}

. . . etc.
#5
01/01/2005 (12:27 am)
I threw together this console method to force a mvoe on the turret. You have to make mRot a public variable in turret.h for this to work.

//
// Force a Move on the Turret - J. Moore AKA The_Force
//
ConsoleMethod( Turret, forceMove, void, 3, 3, "turret.forceMove(Point3F);" )
{
   Turret *turret = static_cast<Turret *>( object );
   Point3F pos;
   dSscanf( argv[2], "%f %f %f", &pos.x, &pos.y, &pos.z );
   turret->mRot += pos;
}

Use:
%turret.forceMove("x y z");

X controls the pitch and Z controls the rotation, I don't know what Y does.
#6
01/02/2005 (9:55 pm)
Thanks a lot! That was exactly what I needed. I'll be sure to include your name in the credits.
Thanks again!

-Dustin Williams
#7
01/02/2005 (11:48 pm)
No problem. :)