Game Development Community

dev|Pro Game Development Curriculum

3D Arrow aiming to objective target

by Herru Cules D · 03/17/2009 (1:52 am) · 30 comments

This resource will give you a 3D arrow (or any object you like) aiming to the target object you want.

This is the preview of how this works.

This resource uses GUIObjectView to load the 3d arrow.

you can download it here www.geocities.com/herrudarmadi/3darrow.zip
place it on data/shapes/direction/arrow.dts

first add this to your server/game.cs
$aimTarget = 0;

function GameConnection::AimTarget( %this )
{   
   if($aimTarget)
   {         
      %player = %this.player;
      %target = $aimTarget;
      
      %playerTrans = (%player.getTransform());   
      %xPlayer = getWord(%playerTrans, 0);
      %yPlayer = getWord(%playerTrans, 1);   
      %zRot = getWord(%playerTrans, 5);
      %playerRot = getWord(%playerTrans, 6);
         
      %targetPosition = (%target.getPosition());
      %xTarget = getWord(%targetPosition, 0);
      %yTarget = getWord(%targetPosition, 1);
         
      %xDiff = %xTarget - %xPlayer;
      %yDiff = %yTarget - %yPlayer;
      
      // dont have to change it to degrees
      // settransform automatically change it to degrees
      %rotToTarget = mAtan(%xDiff, %yDiff); 
      
      if(%zRot < 0)
         %playerRot = (2 * 22/7) - %playerRot;
            
      // multiply by -1 because the rotation in 3dspace is oppositing the guiobjectview rotation
      %rotation = -1 * (%rotToTarget - %playerRot);
         
      commandToClient(%this, 'SetDirection', %rotation);
   }
   %this.schedule(100, AimTarget);
}

on the client/ui/playgui.gui add
new GuiObjectView(direction) {
      canSaveDynamicFields = "0";
      Profile = "GuiDefaultProfile";
      HorizSizing = "center";
      VertSizing = "bottom";
      position = "244 94";
      Extent = "149 149";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      applyFilterToChildren = "1";
      cameraZRot = "0";
      forceFOV = "0";
      lightDirection = "-0.57735 -0.57735 -0.57735";
      lightColor = "0.6 0.58 0.5 1";
      ambientColor = "0.3 0.3 0.3 1";
      emapAmount = "1";
   };

and on the client/playgui.cs add
function PlayGui::onWake(%this)
{
   // Turn off any shell sounds...
   // alxStop( ... );
   $enableDirectInput = "1";
   activateDirectInput();   
     direction.setObject("gb","mod/data/shapes/direction/arrow.dts","","");

   // Activate the game's action map
   moveMap.push();
}

function clientCmdSetDirection(%rotation)
{
   direction.setRotation(0,  %rotation ); 
}

to get setRotation console method works first:

add this to guiobjectview.h before setCamera()
void setRotation( const F32 x, const F32 z ); // <- add this   
void setCamera();

and at guiobjectview.cc add this before guiobjectview::setcamera
void GuiObjectView::setRotation( const F32 x, const F32 z )   
{   
    mCameraRot.x = x;   
    mCameraRot.z = z;   
}

and add this also so that the setrotation can be called from the script
// Script function handling for "setRotation"   
ConsoleMethod( GuiObjectView, setRotation, void, 4, 4, "ObjectView.setRotation(x,z)" ) {   
    argc;   
    GuiObjectView* view = static_cast<GuiObjectView*>( object );   
    view->setRotation( dAtof(argv[2]), dAtof(argv[3]) );   
}

dont forget to rebuild your engine.

how to use it ?

Set the aimTarget method to run automatically just after the player enters the game. here the snippet
function GameConnection::onClientEnterGame(%this)   
{   
   // Every client get's a camera object.   
   %this.camera = new Camera() {   
      dataBlock = Observer;   
   };   
   MissionCleanup.add( %this.camera );   
   %this.camera.scopeToClient(%this);   
  
   // Create a player object.   
   %spawnPoint = pickSpawnPoint("kijang");   
   %this.createPlayer(%spawnPoint);   
      
   // here you go just call the aimTarget here   
   // and you can set the $aimtarget anywhere and anytime you want.   
   %this.aimTarget();   
}

just set the $aimTarget to any object id in your mission.

that's it, hope this resource usefull. cheers!
Page «Previous 1 2
#1
03/17/2009 (3:14 am)
Very nice! :)
#2
03/17/2009 (4:02 am)
Great resource! I'll be sure to try this!
#3
03/17/2009 (8:03 am)
That's really smart! Well done.

I suppose you could use it like a compass too, if you have waypoint/object placed far to the north/X of your gameworld.
#4
03/17/2009 (1:46 pm)
I can think of lots of uses for this. LOTS :D
Cheers for this nice resource!
#5
03/17/2009 (10:00 pm)
Oh yeah! That is cool. Simple but very very functional. The possibilities for other uses boggles the mind.
#6
03/18/2009 (10:05 am)
Very cool, I could definatly see MMO or Racer games getting a use out of this.
#7
03/19/2009 (10:31 am)
Hm, somethings strange, apparently the shape loads and I can't find any errors in the log..

But nothing shows up. The arrow *is supposed* to show up on load isn't it?

Any ideas? Anyone?

-EDIT-

Scrap that, I got the arrow now.

Embarrasing misstake..

Anyhoo, ok so I'm a beginner, how do I set $aimtarget and use the GameConnection to make it work? Could some kind soul give me an example?
I can't see what he's writing there in the video :/
#8
03/19/2009 (7:53 pm)
Hi all, I'm glad this resource helpfull.

@Marcus

at the video I'm just set the global variable $aimtarget to anything I want for example the static shape object I named torquelogo via the console command. You could set it anywhere in your gamescript for example when entering a trigger you set the $aimtarget to any object id you want.

you also could set the aimTarget method to run automatically just after the player enters the game. here the snippet
function GameConnection::onClientEnterGame(%this)
{
   // Every client get's a camera object.
   %this.camera = new Camera() {
      dataBlock = Observer;
   };
   MissionCleanup.add( %this.camera );
   %this.camera.scopeToClient(%this);

   // Create a player object.
   %spawnPoint = pickSpawnPoint("kijang");
   %this.createPlayer(%spawnPoint);
   
   // here you go just call the aimTarget here
   // and you can set the $aimtarget anywhere and anytime you want.
   %this.aimTarget();
}

hope it clears things up :)
#9
03/19/2009 (11:57 pm)
I can't find "setObject" function in tgea,where is it?
#10
03/20/2009 (12:05 am)
Well, I dont have TGEA and never tried it yet. Any inputs from other ?

try to find guiobjectview for TGEA, maybe the method name for setting the object for guiobjectview is different from the one used in TGE.
#11
03/20/2009 (12:27 am)
I find a "setModel" function in TGEA which is similar to "setObject",but I can't rotate the arrow.
Thanks anyway,this is a good resource.
#12
03/22/2009 (5:23 am)
Herru Cules D, thank you so much for the detailed example! Very appreciated.

I'm not on my dev workstation so I can't try it out yet, but should be able to in another day or so I hope.

I was wondering though, noobish as I feel, how to I get the object ID, and what's the command to set $aimtarget?
Is it as simple as $aimTarget(1234); or $aimTarget = 1234; ?

As you can tell, I'm quite new to TS, hope I don't make your brain bleed with my stupid questions.
#13
03/23/2009 (2:04 pm)
The reason the rotation does not work in TGEA (at least 1.7.1) guiObjectView does not seem to contain a setRotation() function. I'll see what I can come up with and post it in the private forums when I have it working.
#14
03/23/2009 (4:01 pm)
Okay, I have a fix for this to work in TGEA in the private forums. The topic can be found at:

http://www.garagegames.com/community/forums/viewthread/87751

-Travis
#15
03/23/2009 (8:18 pm)
Great job Travis! thx for the addition for the guiobjectview in TGEA.

@Marcus:

your welcome, GG community will always help :).
well to get the object ID -> i think you should read this first tdn.garagegames.com/wiki/TorqueScript#Handles_and_Names it will give you directions and samples how to use object ID or object Names.

about assigning the $aimTarget variable yes, just like the second one:
$aimTarget = objectID; or $aimTarget = objectName;
#16
04/01/2009 (5:16 pm)
Hey guys, I did everything like ya said and add the autoload stuff, replace $aimTarget = 0; in the server/game.cs file with
$aimTarget = Waypoint01; which is what I named the target I want the arrow to point to. In the console it says the arrow is loaded but I get
an error saying that there is unknown command setRotation????
Any Ideas as to what would cause this??? I have TGE 1.5.2
Thanks in advance for any help............
#17
04/01/2009 (5:23 pm)
I forgot, also even though it says its loading the arrow, it doesn't display at all on screen.......

Also could you explain what this means

direction.setObject("gb","mod/data/shapes/direction/arrow.dts","","");

What is "gb"???? The rest I know about.......
#18
04/02/2009 (9:48 pm)
@Donnie

i assume you've added guiobjectview resource to your engine.. but maybe there's some method missing, try to search for a newer guiobjectview resource.

the "gb" it's just a name for your direction object - you could changed it to anything you want.

cheers.
#19
04/04/2009 (9:40 pm)
Hey Herru, Is there anyway you could please give me a copy of your
guiObjectView.cc and guiObjectView.h files. It would be of great help to me if you could.....Thanxs in advance for any help.......
Page «Previous 1 2