Game Development Community

Iron Sights

by Adam Beer · in Torque Game Engine · 12/08/2007 (8:23 pm) · 11 replies

I am trying to implement iron sights and I have found a solution, but I an not a programmer and have no idea how I would do this in script. I have changed the eyeOffset to "-0.008 0.35 -0.395" and it look like it aligns perfectly with the sights. Normally I would comment out eyeOffset as it screws up the way first person view works. How would I right click my mouse and apply that eyeOffset to the weapon and when I am not right clicking, it totally turns off eyeOffset? Any help is appreciated.

Thanks.

#1
12/08/2007 (9:30 pm)
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=8397

That's what I'm using. Not for iron sights, but I'm sure it'll work just fine for that purpose as well.
#2
12/08/2007 (9:31 pm)
I am using that as well, but that doesnt really help for iron sights.
#3
12/08/2007 (9:32 pm)
I imagine all you'd have to do is make the player play an animation, holding up the gun to the eye node. Using that resource, of course.
#4
12/08/2007 (9:39 pm)
My idea that I got working well was changing the eyeoffset. Would you happen to know how I would do it as I described above?
#5
12/09/2007 (6:09 am)
Ok, here's a crude way of achieving what you need. It's a crazy hack however it works, including across a network. Not the most elegant way of doing things but far from the worse.

Start on the client.

Add in your keybinding:
moveMap.bind(mouse0, "button2", toggleEyeOffset);

Function to server.
function toggleEyeOffset( %val )
{
   if ( %val == 1 )
      commandToServer( 'AdjustEyeOffset' );
   else if ( %val == 0 )
      commandToServer( 'ReturnEyeOffset' );
}

Last thing for the client, a couple of functions to handle adjusting and returning offset when instructed by the server.
function clientCmdAdjustEyeOffset(%client,%curWeapon,%offset)
{
   %curWeapon.eyeOffset = %offset;
   echo("-->Adjusting EyeOffset to " @ %offset);
}

function clientCmdReturnEyeOffset(%client,%curWeapon,%offset)
{
   %curWeapon.eyeOffset = %offset;
   echo("-->Returning EyeOffset to " @ %offset);
}

Now to the server.
function serverCmdAdjustEyeOffset(%client,%val)
{
   %curWeapon = %client.player.getMountedImage($WeaponSlot);
   %offset = %curWeapon.adjustedEyeOffset;
   %canAdjustEyeOffset = %curWeapon.adjustEyeOffset;

   if(%canAdjustEyeOffset)
   {
      commandToClient(%client,'AdjustEyeOffset',%client,%curWeapon,%offset);
      %weaponName = %curWeapon.item.getName();
      echo("-->Current Weapon is " @ %weaponName);
   }

   else if(!%canAdjustEyeOffset)
   {
      echo("-->Weapon does not have adjustable eyeOffset");
      return;
   }
}

function serverCmdReturnEyeOffset(%client,%val)
{
   %curWeapon = %client.player.getMountedImage($WeaponSlot);
   %offset = %curWeapon.normalEyeOffset;
   %canAdjustEyeOffset = %curWeapon.adjustEyeOffset;

   if(%canAdjustEyeOffset)
   {
   commandToClient(%client,'ReturnEyeOffset',%client,%curWeapon,%offset);
   }

   else if(!%canAdjustEyeOffset)
   {
      return;
   }
}

Finally, add some new values to each weapon script that is to have an adjustable eyeOffset.

This goes in the ShapeBaseImageData section.
datablock ShapeBaseImageData(desertEagleImage)
{
   // Basic Item properties
   shapeFile         = "~/data/shapes/weapon/desertEagle/weapon.dts";
   emap              = true;

   // Specify mount point & offset for 3rd person, and eye offset
   // for first person rendering.
   mountPoint        = 0;
   eyeOffset         = "0.01 0.25 -0.42"; //(xyz)

   // Added for adjustable eyeOffset
   adjustEyeOffset   = true; 
   normalEyeOffset   = "0.01 0.25 -0.42"; //This should always match eyeOffset
                                          //value listed above
   adjustedEyeOffset = "-0.0840 0.175 -0.355"; //This is the eyeOffet value we
                                               //switch to on zoom
   // Added for adjustable eyeOffset

Voila, scripted adjustable eyeOffsets across a network!
#6
12/09/2007 (7:07 am)
Thanks a ton Tim. Accept it doesnt work for me. In the console it will say:

-->Current Weapon is mp44
-->Returning EyeOffset to 0 0 0
-->Adjusting EyeOffset to -0.008 0.35 -0.395
-->Returning EyeOffset to 0 0 0

But it actually doesnt change the offset. I added the first 3 sections of code to defaultbinds.cs and the last block of code I added to a file called zoom.cs in server/scripts/. My weapon image looks like this:

datablock ShapeBaseImageData(mp44Image)
{
   // Basic Item properties
   shapeFile = "~/data/shapes/mp44/weapon.dts";

   // Specify mount point & offset for 3rd person, and eye offset
   // for first person rendering.
   mountPoint = 0;
   eyeOffset = "0 0 0";

   // Added for adjustable eyeOffset
   adjustEyeOffset   = true; 
   normalEyeOffset   = "0 0 0"; //This should always match eyeOffset
                                          //value listed above
   adjustedEyeOffset = "-0.008 0.35 -0.395"; //This is the eyeOffet value we
                                               //switch to on zoom
   // Added for adjustable eyeOffset
#7
12/09/2007 (9:07 am)
If you don't mind me asking, why do you need it to be done this way?
#8
12/09/2007 (9:08 am)
This way works best for me. I havent found any other way to do this that works as well.
#9
12/09/2007 (9:38 am)
Alright I got it to work now. Changing this:

moveMap.bind(mouse0, "button2", toggleEyeOffset);

to this:

moveMap.bind(mouse, button2, toggleEyeOffset);

made it work. Thanks for helping me out. Now if in shapebaseImage.cpp if I want to turn off the effects of eyeoffset when its set at "0 0 0" would I do this:

if (data.eyeOffset="0 0 0" && isFirstPerson()) {
getMountTransform(image.dataBlock->mountPoint,&nmat);
mat->mul(nmat,data.mountTransform);
}

When you use eyeoffset at "0 0 0" the gun is still and doesnt move with the players animations, and I would like it to just use the mount point at "0 0 0".

Thanks.
#10
12/09/2007 (7:48 pm)
Any ideas on the source change? That little bit of code didnt work above. What is wrong in there?
#11
12/23/2007 (2:21 am)
Hey there.

i tried something similar myself. i made two extra nodes in the weapon model. one to be used as your eye when zooming, and one to aim through (so that you would hit exactly what your gun is aiming at). it worked as far as that, however the player wasnt animated on the server so you would see your gun going up and down breathing, but you would still shoot on the same spot.