Game Development Community

dev|Pro Game Development Curriculum

Basic Weapon Sway TGE

by Bryce · 08/07/2010 (7:52 pm) · 1 comments

Based off my Torque 3D resource.

Ok, so here's the deal.

THIS CODE LOOKS ABSOLUTELY HORRID.

It's badly written, doesn't make an ounce of sense. I threw it together a few months ago with the assumption that nobody but me would see it, and even I don't know how it works today. Probably doesn't work over multiplayer (but if it does, fantastic). I thought it may be useful for people, so I made the resource. There aren't a ton of code changes, but it's always a good idea to back up your project before including this.

Tested and works with TGE 1.5.2 and TGE 1.4.2, not sure about earlier versions however.

Files modified:
game/player.cc
game/player.h
client/scripts/default.bind.cs
server/scripts/player.cs

Open up engine/game/player.cc

At line 195, under minImpactSpeed = 25.0f;, add

weaponSwayAmtX = 0.0012;
   weaponSwayAmtZ = 0.0012;
   weaponSwayResetX = 0.0010;
   weaponSwayResetZ = 0.0010;

At line 446, under addField("minImpactSpeed", TypeF32, Offset(minImpactSpeed, PlayerData));, add:

addField("weaponSwayAmtX", TypeF32, Offset(weaponSwayAmtX, PlayerData));
   addField("weaponSwayAmtZ", TypeF32, Offset(weaponSwayAmtZ, PlayerData));
   addField("weaponSwayResetX", TypeF32, Offset(weaponSwayResetX, PlayerData));
   addField("weaponSwayResetZ", TypeF32, Offset(weaponSwayResetZ, PlayerData));

At line 482, under stream->write(minImpactSpeed);, add:

stream->write(weaponSwayAmtX);
   stream->write(weaponSwayAmtZ);
   stream->write(weaponSwayResetX);
   stream->write(weaponSwayResetZ);

At line 695, under stream->read(&minImpactSpeed);, add:

stream->read(&weaponSwayAmtX);
   stream->read(&weaponSwayAmtZ);
   stream->read(&weaponSwayResetX);
   stream->read(&weaponSwayResetZ);

At line 811, under mReversePending = 0;, add:

mSwayX = 0;
   mSwayZ = 0;

At line 1167, under updatePos();, add:

// weapon sway
		  F32 amtX;
		  F32 amtZ;
		  F32 resetX;
		  F32 resetZ;
		  F32 limitX;
		  F32 limitZ;
		  amtX = mDataBlock->weaponSwayAmtX;
		  amtZ = mDataBlock->weaponSwayAmtZ;
		  resetX = mDataBlock->weaponSwayResetX;
		  resetZ = mDataBlock->weaponSwayResetZ;
		  limitX = 0.02 * 0.2;
		  limitZ = 0.02 * 0.2;
		  if (Yaw > 0 && Yaw != 0 && mSwayX < limitX)
			  mSwayX += amtX;
		  if (Yaw < 0 && Yaw != 0 && mSwayX > -limitX)
			  mSwayX -= amtX;
		  // Yaw sway
		  if (Yaw == 0)
		  {
			  if (mSwayX < 0)
				  mSwayX += resetX;
			  if (mSwayX > 0)
				  mSwayX -= resetX;
		  }
		  // Now do pitch sway
		  if (Pitch > 0 && Pitch != 0 && mSwayZ < limitZ)
			  mSwayZ += amtZ;
		  if (Pitch < 0 && Pitch != 0 && mSwayZ > -limitZ)
			  mSwayZ -= amtZ;
		  if (Pitch == 0)
		  {
			  if (mSwayZ < 0)
				  mSwayZ += resetZ;
			  if (mSwayZ > 0)
				  mSwayZ -= resetZ;
		  }

At line 3950, above if (image.dataBlock->cloakable && mCloakLevel != 0.0f), add:

Yaw = Con::getFloatVariable("$swayYaw");
		  Pitch = Con::getFloatVariable("$swayPitch");
		  GameConnection* con = getControllingClient();
			glTranslatef(mSwayX,0,mSwayZ);

Open up engine/game/player.h

At line 91, under F32 minImpactSpeed;, add:

// weapon sway
   F32 weaponSwayAmtX;
   F32 weaponSwayAmtZ;
   F32 weaponSwayResetX;
   F32 weaponSwayResetZ;

At line 298, under S32 mImpactSound;, add:

F32 mSwayX;
   F32 mSwayZ;
   F32 Yaw;
   F32 Pitch;

Compile, good luck!

Now for scripts. Open up ~/client/scripts/default.bind.cs

Find the function
function getMouseAdjustAmount(%val)
{
   // based on a default camera fov of 90'
   return(%val * ($cameraFov / 90) * 0.01);
}

Change it to

function getMouseAdjustAmount(%val)
{
	if (!$gettingTurnValues)
	  getTurnValues();
	
   // based on a default camera fov of 90'
   return(%val * ($cameraFov / 90) * 0.01);
}

Now underneath that function, add

function getTurnValues()
{
	$gettingTurnValues = true;
	$swayYaw = $mvYaw;
	$swayPitch = $mvPitch;
	schedule(100,0,getTurnValues);
}

This part is optional, but if you want to tweak the sway values, open up server/scripts/player.cs and add the following to the datablock (as well as your other player datablocks, again, this is optional because it's already defined in player.cc):

// Weapon sway
   weaponSwayAmtX = 0.0012;
   weaponSwayAmtZ = 0.0012;
   weaponSwayResetX = 0.0010;
   weaponSwayResetZ = 0.0010;

Good luck, hope this works out! If you have any questions or comments, leave a comment or fire me an email, I'm always available to help out.

#1
08/08/2010 (4:32 am)
Thank you very much Bryce! You are awesome!!!