Game Development Community

RayCast not picking up my AI

by Bryce · in General Discussion · 07/10/2007 (1:31 pm) · 6 replies

What I've been doing lately is making a Tactical AI pack (gotta save up to buy Torque), and I'll end up posting a .plan file as soon as I do something that's worth reading about. One of the features on my agenda was to implement order stacking. First, the player aims at the AI and presses a key to "select it". Then, the player aims at a point for the AI to move to and presses a key, and finally, he uses the same process to define where the AI should be looking during all this. The orders are saved, and the player can click an EXECUTE ORDERS button to make the AI do what you told it to.
I'm putting this in to let the player set up a bound overwatch or flanking manuever, or to even clear a room, by giving individual AI orders and sulmitaneously executing the orders. I started by programming AI selection. Here's my chunk of code:
function serverCmdSelectAI(%client)
{
   %plr = %client.player;
   %start = %plr.getPosition();
   %eyevec = %plr.getEyeVector();
   %end = VectorScale(%eyevec,30);
   %end = VectorAdd(%start,%end);
   %ray = ContainerRayCast(%start,%end,$TypeMasks::PlayerObjectType,%plr); 
   echo("Found object: "@ %ray);
   %obj = getWord(%ray,0);
   if (%obj.getClassName() $= "AIPlayer")
   {
      if (%plr.selectedAI == 0 && %plr.selectedAI !$= %obj)
      {
         %obj.isSelected = 1;
         %obj.oldName = %obj.getShapeName();
         %obj.setShapeName(">>>" @ %obj.getShapeName() @ "<<<");
         %oldSel = %plr.selectedAI;
         %oldSel.isSelected = 0;
         %oldSel.setShapeName(%oldSel.oldName);
      }
      else if (%plr.selectedAI $= %obj)
      {
         %obj.isSelected = 0;
         %obj.setShapeName(%obj.oldName);
      }
   }
   else
   {
      echo("Not an AI Player.");
   }
   echo("No AI to select.");
}
I have a variable set in the player datablock which stores the AI that is selected. If the AI you are trying to select is already selected, that AI is no longer selected. I have everything bound correctly, no console errors, but when I try to select an AI by aiming over it and pressing a key, my console prints this
Found Object:
Not an AI Player.
No AI to select.
What I'm doing is scaling the eye vector up by 30 and adding it to the player's position to create and end point some distance out there, straight ahead. I would run a ContainerRayCast from the player's position to the end point, and the PlayerObjectType that is between those points is hopefully an AIPlayer. If it is, then change its ShapeName to reflect the new state and set a field in its ScriptObject to tell Torque that it is selected.

I'm aiming dead on at him, but it doesn't see him. Do I need to change the search masks in my RayCast? Is there a better way to figure out what I'm aiming at? Is there a typo? Am I stupid? Is it Texas? Help is appreciated.

#1
07/10/2007 (4:21 pm)
AI Player uses the typemask "AIObjectType" rather than "PlayerObjectType".
#2
07/10/2007 (5:53 pm)
Really? I've been looking at the reference section of my 3d Game Programming All in One book, it isn't listed. Has this been added since Torque 1.2?
#3
07/10/2007 (6:26 pm)
I'm not sure about 1.2. With 1.5 AIPlayer is based off of Player with an extra type mask AIObjectType to help you identify AI players. However, since it inherits from Player, it should also work with PlayerObjectType.

Assuming TGE 1.2 has these functions, try using "%plr.getEyePoint()" as your %start location. Also for the end location use a larger scale factor as a test, say 500, and normalise the eyeVector prior to scaling it.

%plr = %client.player;
   %start = %plr.getEyePoint();
   %eyevec = VectorNormalize( %plr.getEyeVector() );
   %end = VectorScale(%eyevec, 500);
   %end = VectorAdd(%start, %end);
   %ray = ContainerRayCast(%start,%end,$TypeMasks::PlayerObjectType,%plr); 
   echo("Found object: "@ %ray);

That's on the assumption either your end point wasn't far enough away to make it to the AI player, or the vector from the players position to the end point misses the ai player, where as a vector from the eye location to the end point should hit.
#4
07/10/2007 (7:55 pm)
No, I'm actually using 1.5.2, but the book I'm getting references from was based on 1.2. I'll give that a shot tomorrow, thank you for your help.
#5
07/11/2007 (2:07 pm)
Thanks for your help, Gary. It works exactly the way I want. Next, movement orders and fire sectors!
#6
07/13/2007 (10:13 am)
Hey Bryce whats up. I have a demo coming out soon for a game that will blow you away! My friend and I have been working on it for 5 years. I was wondering if you were interested in maybe helping with the project. The game is called "Code 10 the Base" the base means the client will have control of scripting there own charcter! There's alot of scripting put into the game to make it more about scripts. But there are so many differnt things in it I cant really explaine it you will just have to see. My friend and I are going to release a demo very soon we do have the torque engine but we just put it on the realm wars comminuty project because people play still play there a little. Your alowed to mod your version of realm wars test release 1. 0. 0 but just not sell it. Anyways check out these web sites praxinok.blogspot.com creatureconvention.com