Game Development Community

precise click and move

by Roger D Vargas · in Torque 3D Beginner · 06/20/2014 (9:10 am) · 6 replies

I copied the click on terrain code from RTS tutorial to my project and it worked apparently fine. It even works when the floor is a mesh and not a terrain, to my surprise. But i noticed something, in the second case (using a floor mesh, not a terrain) the click doesnt works always. Seems that if the click point is close to an object (a wall, a furniture) the click is not processed and I have to zoom to click. Is there any better method for this?

#1
06/20/2014 (10:04 am)
I'm guessing the raycast is hitting the bounds box of the furniture, though I'm not sure about the wall (don't know how your geometry is set up). As far as "precision" I'm not sure how you could be any more precise - it's a raycast that returns the exact world coordinates of its collision. You can filter for class names or some other field value (perhaps add a .objType field to the non-floor objects) and ignore those hits - I wrote a "filter" that would find a hit and if it didn't match my field it would cast a new ray from a point farther along so it would "skip ahead" and continue looking. I am on vacation now but I'll be back home tomorrow evening - I'll see if I can dig that script up this weekend.
#2
06/22/2014 (12:51 pm)
Ok, I didn't find the final version, but this is close:
function serverCmdmovePlayer(%client, %pos, %start, %ray)
{
    %longRay = VectorScale(%ray, 1000);
    %end = VectorAdd(%start, %longRay);

    %searchMasks = $TypeMasks::TerrainObjectType | $TypeMasks::StaticTSObjectType | 
    $TypeMasks::InteriorObjectType | $TypeMasks::ShapeBaseObjectType | $TypeMasks::StaticObjectType;

    %scanTarg = ContainerRayCast( %start, %end, %searchMasks);

    if( %scanTarg )
    {
        %obj = getWord(%scanTarg, 0);

        while (%obj.class $= "barrier")
        {
            // Get the X,Y,Z position of where we clicked
            %hit = getWords(%scanTarg, 1, 3);
            %restart = VectorAdd(%hit, %ray);
            // move forward a little along our ray and search again
            %scanTarg = ContainerRayCast( %restart, %end, %searchMasks);
            %obj = getWord(%scanTarg, 0);
        }

        // Get the X,Y,Z position of where we clicked
        %pos = getWords(%scanTarg, 1, 3);

}
The while loop checks the object class - in this case "barrier" - and then re-casts from the hit position plus the normalized ray. This should then not hit the object (collisions don't work on back-faces so casting a ray from within an object can't fire a collision) but instead return the next object down the line.
#3
06/25/2014 (8:45 am)
Ok, this is my final code:
function serverCmdmovePlayer(%client, %pos, %start, %ray)
{
  
   // Get access to the AI player we control
   %ai = %client.player;

   %longRay = VectorScale(%ray, 1000);  
    %end = VectorAdd(%start, %longRay);  
  
    %searchMasks = $TypeMasks::TerrainObjectType | $TypeMasks::StaticTSObjectType |   
    $TypeMasks::InteriorObjectType | $TypeMasks::ShapeBaseObjectType | $TypeMasks::StaticObjectType;  
  
    %scanTarg = ContainerRayCast( %start, %end, %searchMasks);  
  
    if( %scanTarg )  
    {  
        %obj = getWord(%scanTarg, 0);  
  
        while (%obj.class $= "barrier")  
        {  
            // Get the X,Y,Z position of where we clicked  
            %hit = getWords(%scanTarg, 1, 3);  
            %restart = VectorAdd(%hit, %ray);  
            // move forward a little along our ray and search again  
            %scanTarg = ContainerRayCast( %restart, %end, %searchMasks);  
            %obj = getWord(%scanTarg, 0);  
        }  
  
        // Get the X,Y,Z position of where we clicked  
        %pos = getWords(%scanTarg, 1, 3);  
	%ai.setMoveDestination( %pos );
   }
}

But still have issues. Check the red circle in the image, I was clicking there but the character didnt move. Perhaps adding a navmesh could help to better find the path?

i249.photobucket.com/albums/gg237/rogerdv/dnt2014-06-2511-26-35-71.jpg
#4
06/25/2014 (7:58 pm)
Check the hud - Stock T3D has a damage display object that has caused me issues in the past, as well as the target reticle object. I usually clear all of the GUI elements from the hud except the ammo counter and the chat window.
#5
06/26/2014 (5:03 am)
Richard, you are a genius. Made a quick test and all clicks worked. I think we can officially blame the gui for the problems (which, coincidently, I had removed yesterday for a test).
Are we going to have some day a functional empty project without all the hassle of FPS gui, scripts and such?
#6
06/26/2014 (6:46 am)
<shrug> Not my normal department... lol The other option is to create your own template project and add it to the list. Hell, I keep adding the 2012/2013 project generation scripts and they keep getting dropped (easy to overlook - they don't affect the game at all, just php to generate 2012 and 2013 .sln files/projects).