Game Development Community

Shoot where the camera is looking

by Brajendu Behera · in Torque Game Engine · 07/21/2008 (2:03 am) · 7 replies

Hi,

I want to shoot the projectile at the point where the camera is looking in third person view. I am using the Advanced Camera resource third person and I made the player not at the center of the screen. So I want to shoot at the point where the camera is looking, its the center of the screen where the cross hair is.

I have seen that the projectile is generally fired at the point got from:
%muzzleVector = %obj.getMuzzleVector(%slot);

Can I get this thing from the camera.

Any help is really appreciated.

#1
07/21/2008 (4:30 am)
I think you could try casting a ray that is weaponrange long - from the cam's position and orientation. If the ray hits something, then take the one closest to the cam, and make that the target position. If it does not hit anything, then take the ray's end position.
#2
07/21/2008 (6:05 am)
Please tell me some codes, because I can't figure out how to do this.
#3
07/21/2008 (7:12 am)
I found some code in the forums for ray casting and used it in a 'cast ray use button'. you can see the full code here: 216.116.32.49/index.php?sec=mg&mod=resource&page=view&qid=13640


Here's the cast ray code from that resource... you'll have to make changes to it (not copy/paste) but it's pretty simple stuff. I'm sure there's code in the forums someplace for doing what you want. I think i might have seen a resource for it also.

case Player: is where you would check if it's an enemy and give damage, etc...

edit: you also need to change the muzVec to the camera, because my code uses the guns muzzle vector.

// 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
}

}

}
#4
07/21/2008 (11:35 am)
Thanks for your help, mb

But I actually want the vector from the camera, so getMuzzleVector won't work, is there any way to get the vector from the camera?

Thanks...
#5
07/21/2008 (1:33 pm)
I looked at the mouse raycast code in gameTSCtrl.cpp to see how it's done, since your solution will be very similar:

Point3F lineStart, lineEnd;
   MatrixF mat;
   Point3F vel;
   if ( GameGetCameraTransform(&mat, &vel) )
   {
      Point3F pos;
      mat.getColumn(3,&pos);
      Point3F screenPoint([b](F32)evt.mousePoint.x[/b], [b](F32)evt.mousePoint.y[/b], -1.0f);
      Point3F worldPoint;
      if (unproject(screenPoint, &worldPoint)) {
         Point3F vec = worldPoint - pos;
         lineStart = pos;
         vec.normalizeSafe();
         lineEnd = pos + vec * [b]1000[/b];

         // castray here

      }
   }

Insert the crosshair position where the mouse coords are, and change that 1000 to the range of your weapon. I think this should work. Then look at this CastRay example on TDN to see how to use those lineStart and lineEnd values: CastRay Example.
#6
07/21/2008 (9:59 pm)
Thanks Konrad...

:-)
#7
04/09/2009 (1:18 am)
Is this TorqueScript or c++? Cuz' it looks very weird as Torque Script and it says there is bugs.