Game Development Community

Weapon recoil/kickback

by Max Thomas · in Torque Game Engine · 02/09/2006 (11:23 pm) · 42 replies

Hey guys,


Been a good while since I've posted any threads on here, but none the less we all have t ocome running back with a question here and there. The last few days I've been trying to get somthing along the lines of the cone of fire resource into my game. I've tried to make the players camera shake when the weapon is being fired to no avail, as with a few other ways of going about it that didn't work etheir. I easily got my weapons not firing perfectly strait and having bullet spread, but it's far from realistic. I don't know if anyone has found a good salution for this, but idealy if I could get the players entire veiw to move up as the weapon was fired, or to just get a general cone of fire dealy working. This has just been driving me to all ends of insanity, I figured I would come here before I was sent away to some hospital for the mentally ill. :)

-Max
Page «Previous 1 2 3 Last »
#2
02/15/2006 (3:18 pm)
I'm about to try that resource as well. By the way, you'll need the guiCoFCrossHairHud.cc. The one on the resource page is nonexistant.
#3
02/15/2006 (5:03 pm)
Quote:
but idealy if I could get the players entire veiw to move up as the weapon was fired

I found a quick simple way of achieving this, do the following...

Create a new *.cs script called weaponFx.cs and place it in your server/scripts directory. Be sure to execute this script from game.cs in the usual manner. Within this script place the following...

function gunupLight(%val)
{
   $mvPitch = %val ? $Pref::Input::KeyboardTurnSpeed : -0.005;
}

function gunupMedium(%val)
{
   $mvPitch = %val ? $Pref::Input::KeyboardTurnSpeed : -0.01;
}

function gunupHeavy(%val)
{
   $mvPitch = %val ? $Pref::Input::KeyboardTurnSpeed : -0.05;
}

Now to use the feature just add

gunupLight();
or
gunupMedium();
or
gunupHeavy();

to the top of your onFire function in your weapon.cs script.
#4
02/15/2006 (5:37 pm)
Won't that affect either all clients at once, or only affect the server player when it gets called when a client fires a weapon?

I tried doing this before making the pitch move up in the respective weapon's *.cs file and was told that pitch is a client side command so putting something that alters pitch on the server side would not work in MP. I am interested in getting the entire view to shake as well, much like the explosion shake maybe.
#5
02/15/2006 (6:08 pm)
Yeah Tim, that will not work on a networked game. $mvPitch is a client side global that the MoveManager uses for pitch. You'll want to either send a client command and call those functions(giving the client total control over recoil), or add some C++ code to the player class for some kind of server controlled recoil.
#6
02/15/2006 (6:16 pm)
Hey guys,


I actually wasn't expecting any responces to this, so thanks! I actually managed to rig something that works pretty well as of now, where when you fire a weapon it decreses the energy, and the weapon spread is devided by the energy ammount. I'll post it as a resource when I have some time assuming that someone is interested. Tim, thanks for that code there, if you manage to get it working client side I'd love to see the code, till then I'll see if I can get it working also.

If anyones interested I can post my code for the energy use/projectile system on here just for the time being?



-Max
#7
02/15/2006 (6:16 pm)
Ok, sorry guys never got round to testing it on the network.

I did manage to alter the projectile code once to allow a camera shake on firing of projectile but I had the same problem over the network where it was affecting everyone so I took it out. Having a camera shake when firing a weapon looked pretty cool, might have to try it again.
#8
02/15/2006 (6:51 pm)
I found an old copy of a game I was working on which has the camera shake in it. Basically all I did was add all the camera shake data into projectile.cc If you wanted a weapon to have camera shake all you had to do was add the following into the weapon's projectile datablock

shakeCamera = true;
   camShakeFreq = "2.5 2.5 5.5";
   camShakeAmp = "0.8 0.8 0.8";
   camShakeDuration = 0.2;

Pretty basic but if tweaked right could be very effective. As mentioned earlier I stopped using this as it didn't work on multipalyer.

Here's a demo video of it in action, being hosted on filefront.
#9
02/17/2006 (9:57 am)
Hey Tim,


That little video you have there is quite neat looking, but as you said it doesn't work on multiplayer so I can't get it into my game as of now. I'm going to continue on trying to figure out why this is all messed up on multiplayer, if you manage to ever get it working though I'd love to see.

Thanks, Max
#10
02/18/2006 (5:28 am)
Thanks Max,

The video is from an old project, about 2.5 years old so it's nice to hear you say it was neat looking :) I had no idea what I was doing back then, not that I have much more of an idea now : p

In multiplayer, the camera shake works for both the person who hosts the server & joined the server. The problem being everytime the host shoots the joined person receives the camera shake (that might be the other way around, can't remember).

I imagine it wouldn't be too hard to fix, just a matter of shifting some code around. Of course it will be a resource if I ever get it working.
#11
02/18/2006 (3:01 pm)
I am finished my recoil system in my FPS. It's not too complicated of a system but I am rather proud of it.

Tim was on the right track with the yaw and pitch change, I just have the server run a command to the client to apply recoil. I call my system "Full Physical Recoil" and am rather proud of it. For that case I would rather not expose the code.

The basic idea is outlined in my .Plan found here/
#12
02/18/2006 (3:33 pm)
Come on Matt, I was looking forward to your system. :'(
#13
02/18/2006 (3:51 pm)
I'm basing this off Tim's script from above, I have not tested this(just typed it up in-browser).
Note that I added left/right recoil.

Client script:
function clientCmdGunUp(%type) {
   switch$(%type) {
      case "Light":
         $mvPitch += -0.005;
         $mvYaw += getRandom() ? 0.002 : -0.002;
      case "Medium":
         $mvPitch += -0.01;
         $mvYaw += getRandom() ? 0.008 : -0.008;
      case "Heavy":
         $mvPitch += -0.05;
         $mvYaw += getRandom() ? 0.02 : -0.02;
   }
}

Server useage:
CommandToClient(%client, 'GunUp', "Medium");

Edit: This method of "server-side recoil" is pretty much a redundant hack, since it tells the client to change it's Move which is then read by the server and used in updateMove. It would be much better(and not nearly as hackable) to add recoil directly to the Player class and have everything server controlled.
#14
02/18/2006 (4:34 pm)
No compilation errors or any funny business in the console, but no recoil. (I put the commandToClient in the onFire function of the weapon)
#15
02/18/2006 (4:42 pm)
Are you actualy sending the client the command though? Just copy/pasting that into your onFire function will not work. I think you'd have to use:

CommandToClient(%obj.client, 'GunUp', "Medium");
#16
02/18/2006 (4:44 pm)
Alas, it works. Thanks. Is it possible to make it also have the option of recoiling left and/or down, randomly? (I'm just not good with the syntax there)
#17
02/18/2006 (5:25 pm)
If you read the code, it does do random left/right recoil(the $mvYaw lines). You can add similar code to the pitch lines, but I don't know of any recoil the bumb your view down.
#18
02/18/2006 (5:43 pm)
Alright I've got it working pretty good, let me tweak it and I'll post a way to make it shake slightly and in all directions. (Like a rumble for a machinegun weapon)

Here it is. In any client/scripts/*.cs script place:

function clientCmdGunUp(%type) {
   switch$(%type) {
      case "handgun":
         $mvPitch += (getRandom(-1,0) + -0.5) * (0.03/getRandom(3,5));
         $mvYaw += (getRandom(0,1) + 0.5) * (0.03/getRandom(3,5));
      case "assaultRifle":
         $mvPitch += (getRandom(-1,0) + 0.5) * (0.03/getRandom(3,5));
         $mvYaw += (getRandom(-1,0) + 0.5) * (0.03/getRandom(3,5));
   }
}

And then in the respective server/scripts/*.cs file, place the following in the function _______Image::onFire:

CommandToClient(%obj.client, 'GunUp', "assaultRifle");

   or 

   CommandToClient(%obj.client, 'GunUp', "handgun");

The assaultRifle one rumbles the yaw and pitch slightly making a neat looking effect, and the handgun one just tosses the yaw and pitch up and right a bit. Ideally, I want the camera to shake like it did in the video Tim posted, so I'll be working on getting it to work correctly. Edited: Aaaand, I fail at even being able to copy the camera shake data from explosion.cc to projectile.cc without a million errors.
#19
02/18/2006 (7:46 pm)
@Tim: Could you put up the changes necessary to get the camShake working? Maybe I can get a start on what needs to be changed to get it in MP.
#20
02/18/2006 (8:35 pm)
I posted a solution to this in another thread which modifies only the clients value and is not done clientside. It had a problem though if the server had a playerobject. That's not an issue for me though as the server is dedicated.
Page «Previous 1 2 3 Last »