Game Development Community

dev|Pro Game Development Curriculum

Cast Ray Use Button

by mb · 10/18/2007 (2:06 pm) · 15 comments

Download Code File

client/scripts/default.bind.cs

First find the line for the zoom key and change the binding from 'e' to 'x'.
moveMap.bind(keyboard, x, toggleZoom);

Then add this code:

//-----------------------------------------------------------------------------------------------------------------
// Use Button - will popup menus on objects - for mounting vehicles, accessing 
// gui interfaces, etc...
// Casts a Ray and determines what object it is and what menu to push. 
//-----------------------------------------------------------------------------------------------------------------

function useButton(%val)
{
	if (%val)
	{
		commandToServer('UseButton', 1);
		
	}
	else
	{
		commandToServer('UseButton', 0);
		
	}
}
moveMap.bind(keyboard, "e", useButton);


Next add this code:
server/scripts/commands.cs

//-----------------------------------------------------------------------------------------------------------------
// Use Button - will popup menus on objects - for mounting vehicles, accessing 
// gui interfaces, etc...
// Casts a Ray and determines what object it is and what menu to push. 
//-----------------------------------------------------------------------------------------------------------------

function serverCmdUseButton(%client, %arg)
{
   // scope the player
   %player = %client.getControlObject();
   
   // key is being pressed	
   if(%arg)
   {   
      if(!%client)   
      {      
   	   return;
      } 
   
      if(%player.isMounted == 1)   
      {      
   	   return;
      }
      
      // RAY_CASTING
	   
	   //echo("%client: " @ %client);
	   //echo("%arg: " @ %arg);
	   //echo("%player: " @ %player);
	      
	   // muzVec is the vector coming from the gun's "muzzle"
      %muzVec = %player.getMuzzleVector(%slot);

      // muzNVec = normalized muzVec
      %muzNVec = VectorNormalize(%muzVec);

      // range of ray
      %range = 50;

      // scale muzNVec to the range the beam can reach
      %muzScaled = VectorScale(%muzNVec, %range);

      // muzPoint = the actual point of the gun's "muzzle"
      %muzPoint = %player.getMuzzlePoint(%slot);

      // rangeEnd = muzzle point + length of beam
      %rangeEnd = VectorAdd(%muzPoint, %muzScaled);
	
      %searchMasks = $TypeMasks::StaticObjectType |               
                     $TypeMasks::StaticShapeObjectType |
                     $TypeMasks::CorpseObjectType |
                     $TypeMasks::ItemObjectType |
                     $TypeMasks::VehicleBlockerObjectType |
                     $TypeMasks::VehicleObjectType |
                     $TypeMasks::WaterObjectType;

      
      // search for objects within the beam's range that fit the masks above
      %scanTarg = ContainerRayCast(%muzPoint, %rangeEnd, %searchMasks, %obj);

      if(%scanTarg)
      {
         %serverID = getWord(%scanTarg,0);
         		
		   // echo("%scanTarg.getClassname(): " @  %scanTarg.getClassname());
	
         switch$(%scanTarg.getClassname())
         {		
	case StaticShape:
	echo("Player is targeting  a StaticShape - Server ID = " @ %serverID);
	// Do stuff
                 		      
            case Player:
            echo("Player is targeting another player - Server ID = " @ %serverID);            
            // Do stuff

            case WheeledVehicle:
            
                   %serverID.mountObject(%player, 0);
                   %player.isMounted = 1;
			
            case FlyingVehicle:
            //echo("Player is targeting a vehicle - Server ID = " @ %serverID);
            // Do stuff

            case HoverVehicle:
            //echo("Player is targeting a vehicle - Server ID = " @ %serverID);
            // Do stuff

            case InteriorInstance:
            //echo("Player is targeting a building - Server ID = " @ %serverID);
            // Do stuff

            case TerrainBlock:
            //echo("Player is targeting the ground - Server ID = " @ %serverID);
            // Do stuff

            default:
            // Do stuff
         }
         
      }
   
   }
   
   // key is being released
   else
   {
	// Do Stuff
   }
	
	 		
}

Next replace this code:
server/scripts/player.cs

function Armor::doDismount(%this, %obj, %forced)
{
// This function is called by player.cc when the jump trigger
    // is true while mounted 
    if (!%obj.isMounted())      
    return; 

    // Position above dismount point   
    %pos    = getWords(%obj.getTransform(), 0, 2); 
    %oldPos = %pos;  
    %vec[0] = " -1  0  1"; 
    %vec[1] = " 0  0  1";  
    %vec[2] = " 0  0 -1";  
    %vec[3] = " 1  0  0"; 
    %vec[4] = "-1  0  0"; 
    %impulseVec  = "0 0 0";
    %vec[0] = MatrixMulVector( %obj.getTransform(), %vec[0]);

    // Make sure the point is valid 
    %pos = "0 0 0";  
    %numAttempts = 5;
    %success     = -1;   
    
    for (%i = 0; %i < %numAttempts; %i++)
    {     
        %pos = VectorAdd(%oldPos, VectorScale(%vec[%i], 3));
        if (%obj.checkDismountPoint(%oldPos, %pos))
        { 
            %success = %i;      
            %impulseVec = %vec[%i];  
            break;      
        }       
    }   
        if (%forced && %success == -1)
        %pos = %oldPos; 
        %obj.unmount();  
        %obj.setControlObject(%obj);

        if(%obj.mVehicle)
        %obj.mVehicle.mountable = true;
        %obj.mountVehicle = false; 
        %obj.schedule(4000, "mountVehicles", true);   // Position above dismount point   
        %obj.setTransform(%pos);   
        %obj.applyImpulse(%pos, VectorScale(%impulseVec, %obj.getDataBlock().mass));
        
        %obj.isMounted = 0;      
        
}


Thats all. I use this to make menu's pop up for objects and buildings, etc... it's has many uses! Enjoy.

#1
10/08/2007 (11:02 pm)
Crashes the engine as soon as I press e aiming at a car. What am I missing?

Bye, Berserk.
.
#2
10/11/2007 (11:39 am)
I tested on a clean install of 1.5 and it worked for me. Not sure why it would crash. Check the log file and see what the last thing it says, or post it here. Could be something as simple as a missing }.
#3
10/11/2007 (2:07 pm)
I use 1.4.2, may that be my issue?

Bye, Berserk.
.
#4
10/12/2007 (7:00 am)
It could be 1.4 I don't know. Does your player have a gun? Something I forgot to mention ... the code casts a ray from the muzzle vector of the gun. I could change this to cast from something else if that is the problem.
#5
10/19/2007 (2:55 pm)
Cool upgrade (from the mount vehicle resource). This is similar to where I went with the Mount Vehicle as well, but I rolled it into a "use" function I'd already based on the scripted doors resource. I think that resource uses the eye instead of the weapon to do ray casts from - this may be better than using the weapon.

Also, you reference %slot in a few places, but never initialize it. This works because the weapon mount defaults to "0" as does an uninitialized variable.

@bezerk: Are you using the "buggy" from the demo or a custom model? My custom vehicle crashed every time I tried to get in at first .... I finally loaded the model down with every damn node I could find referenced anywhere having to do with vehicles and it seemed to fix the problem - don't know if that helps.
#6
10/25/2007 (10:59 am)
Thanks Kent. I haven't changed it to use the eye vector yet but I think it can be done by just changing:

// muzVec is the vector coming from the gun's "muzzle" 
 %muzVec = %player.getMuzzleVector(%slot);

to:

%muzVec = %player.GetEyeVector();


and

%muzPoint = %player.getMuzzlePoint(%slot);

to:

%muzPoint = %player.GetEyePoint();


Haven't tried it yet but in theory it should work.
#7
10/26/2007 (1:19 am)
Kent, yes I'm using the default buggy.dts wich comes with starter.racing
Please, call me Berserk (not bezerk in eg)

MB: How do I make sure this will fix everything?

Bye, Berserk.
.
#8
10/26/2007 (7:07 am)
To be honest I don't know why it doesn't work for you. Take a look at the log and see what the last few lines say. That will give us a hint as to what is happening.
#9
10/26/2007 (3:11 pm)
I just got a "using button" echo as the last console.log message. Nothing related to this as the rest of the file.

Bye, Berserk.
.
#10
12/22/2007 (3:40 pm)
I think I know the problem: In serverCmdUseButton, you never define %slot, and then try to get the muzzle vector and point of the weapon at null. You probably should change it to getMuzzleVector(0) or, since you use %slot in multiple places, use %slot = "0".
#11
12/24/2007 (9:13 am)
I just tested the code I posted above and it worked fine for me and my player didn't have any weapons.

changed: %muzVec = %player.getMuzzleVector(%slot);
to: %muzVec = %player.GetEyeVector();

and

changed: %muzPoint = %player.getMuzzlePoint(%slot);
to: %muzPoint = %player.GetEyePoint();
#12
07/05/2009 (4:46 am)
thanks, have many uses, like AI senses...
#13
10/13/2011 (8:56 am)
Sorry to post on an old thread however,

I have just implemented the ray caster in Torque3D 1.1 Pro unfortunately im having difficulties getting it to detect any type of vehicle, it seems to always detect other players or terrain-blocks that are behind the vehicle but not the vehicle its self

iv tested this with the two cars in game (default buggy and cheetah) and also a flying vehicle i have added my self.

I'm thinking the search Masks just need updating


any one have an idea on what the new search marks need to be.
#14
10/13/2011 (10:24 am)
I'm assuming that the vehicle doesn't have a collosion box (more
specifically LOSCol). Look at this thread here:
http://www.garagegames.com/community/forums/viewthread/82447



#15
10/13/2011 (11:31 am)
@mb thank you for that, one of the col's i missed out on my test vehicle ill add that in and then see what occurs

Update: Well that has not worked, im still getting a echo back staying that no objects were found.

Just to be on the safe side, could any one link me to a visual reference on how to properly rigg a flying vehicle for T3D 1.1 in 3dsMax, just so i can be sure that iv got every thing set up properly.

Just in case iv done some thing wrong with the ray caster here is my "modified version"

Yes before it gets asks i have updated the default bind mapping to call the new function name :P

function serverCmdMountVehicle(%client)
{
   // scope the player
   %player = %client.getControlObject();
   
   if(%player.isMounted == 1)   
   {      
 	   return;
   }
   
   // RAY_CASTING
   //echo("%client: " @ %client);
   //echo("%arg: " @ %arg);
   //echo("%player: " @ %player);
   // muzVec is the vector coming from the gun's "muzzle"
   %muzVec = %player.getMuzzleVector(%slot);
   
   // muzNVec = normalized muzVec
   %muzNVec = VectorNormalize(%muzVec);
   
   // range of ray
   %range = 5;
   
   // scale muzNVec to the range the beam can reach
   %muzScaled = VectorScale(%muzNVec, %range);
   
   // muzPoint = the actual point of the gun's "muzzle"
   %muzPoint = %player.getMuzzlePoint(%slot);
   
   // rangeEnd = muzzle point + length of beam
   %rangeEnd = VectorAdd(%muzPoint, %muzScaled);
	
   %searchMasks = $TypeMasks::VehicleObjectType;
   
   // search for objects within the beam's range that fit the masks above
   %scanTarg = ContainerRayCast(%muzPoint, %rangeEnd, %searchMasks, %obj);
   
   if(%scanTarg)
   {
      %serverID = getWord(%scanTarg,0);
      
      echo("Vehicle Class Name: " @  %scanTarg.getClassname());
	
      switch$(%scanTarg.getClassname())
      {		
         case WheeledVehicle:
            echo("Player is targeting a Wheeled Flying vehicle - Server ID = " @ %serverID);
            %serverID.mountObject(%player, 0);
            %player.isMounted = 1;
			
         case FlyingVehicle:
            echo("Player is targeting a Flying vehicle - Server ID = " @ %serverID);
            //%serverID.mountObject(%player, 0);
            //%player.isMounted = 1;

         case HoverVehicle:
            echo("Player is targeting a Hover vehicle - Server ID = " @ %serverID);
            //%serverID.mountObject(%player, 0);
            //%player.isMounted = 1;

         default:
            echo("Un-Catregorised Object Found");
      }       
   }
   // key is being released
   else
   {
      echo("No Object Found");
   }
}