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
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
#22
(A little camera shake rumble with a 1/1000 projectileSpread will be perfect for the assault rifle)
02/19/2006 (6:23 am)
Thanks Tim, the camShake is exactly what I want in my project. :) (A little camera shake rumble with a 1/1000 projectileSpread will be perfect for the assault rifle)
#23
NB I release with the stipulation that any improvements must be released back to the community!!!
02/19/2006 (6:52 am)
Ok I've tidied up the code and will release it here in a few minutes. NB I release with the stipulation that any improvements must be released back to the community!!!
#24
02/19/2006 (7:06 am)
Of course :) On a side note, the FPWM resource is up.
#25
Here's the camera shake resource as promised
Camera Shake
All feedback and suggestions are welcome
02/19/2006 (7:33 am)
Hey thanks C2, we noticed your resource and got FPWM working!!!Here's the camera shake resource as promised
Camera Shake
All feedback and suggestions are welcome
#26
Anyway, nice work guys.
02/19/2006 (8:42 am)
I'm glad you got it working without my help. :) Adding it into the engine is a better idea (less hackible) but I'm planning in coding anti-cheat software, and a scan for a "no-recoil" hack wouldn't be a problem. That camera shake resource isn't what I'm looking for anyway, and the best way to apply recoil would still be in a scripted function (in my game at least) the script may be called by the engine exe however.Anyway, nice work guys.
#27
@Tim: That resource is exactly what I was looking for. Excellent job. :D OOC: If that's your gunfire sound effect in the camShake video you posted, would you mind if I use it? (I extracted it from the video already heh)
02/19/2006 (8:54 am)
Matt, would you have any idea why this doesn't work correctly in networked play?@Tim: That resource is exactly what I was looking for. Excellent job. :D OOC: If that's your gunfire sound effect in the camShake video you posted, would you mind if I use it? (I extracted it from the video already heh)
#28
eg
In return, as well as not prosecuting you for piracy we'll throw something like this in our credits...
***chuckle chuckle***
[edit] Yeah you can use it (with our blessings). We seem to have a habbit of bumping into each other & helping each other out.[/edit]
Just a quick thought, perhaps we could look at partnering up for a Torque project sometime in the future. I'm sure we all have different and varying skills we can bring to the table.
How many people you got in your team?
I have two people including myself...
I specialise in modelling & animation, texturing and general artwork. My partner is the audio guru, he's resposible for all fx and music. We're both pretty adept at TS, and I'm currently learning C, Java, Vb in a diploma I'm attending.
Anyway, just an idea...
02/19/2006 (9:22 am)
Thief! Well I can't stop you from stealing it but perhaps you could chuck my name & my audio guys name in the credits for your game :)eg
Quote:
Special thanks to Tim Heldna & Frank Skilton for their wonderful contributions to my game. Including, but not limited to, the awsome weapon camera shake effect and the M60 sound file I illegally ripped from a kindly donated video of theirs.
In return, as well as not prosecuting you for piracy we'll throw something like this in our credits...
Quote:
We'd like to aknowledge C2 for his help with our sniper zoom & getting our 1st person arms animated. Also, the 5% royalty we receive from each game of his sold (due to an unlicensed sound file) really helps us put food on the table!
***chuckle chuckle***
[edit] Yeah you can use it (with our blessings). We seem to have a habbit of bumping into each other & helping each other out.[/edit]
Just a quick thought, perhaps we could look at partnering up for a Torque project sometime in the future. I'm sure we all have different and varying skills we can bring to the table.
How many people you got in your team?
I have two people including myself...
I specialise in modelling & animation, texturing and general artwork. My partner is the audio guru, he's resposible for all fx and music. We're both pretty adept at TS, and I'm currently learning C, Java, Vb in a diploma I'm attending.
Anyway, just an idea...
#29
Wow, didn't expect this thread to go anywhere in the first place, and look where we are now! This stuff here looks great, I'll have to give it a try later today when I have some time. I feel as though I ought to give you all somthing also, so heres my bit of projectile spread code that gets decressed accuracy as the gun is fired. I at the moment just have it using the energy bar for the ammount of accuracy
On your weapons *.cs file, under server/scripts dirrectory for example. Change the onFire function to this.
Make sure that in player.cs that the maxEnergy is set to 100, it shouldn't matter how much it is, but I've found that 100 works best. Also the rechargeRate should be set to 0.856
Then last of all, under ShapeBaseImageData, put
-Max
02/19/2006 (9:44 am)
Hey guys,Wow, didn't expect this thread to go anywhere in the first place, and look where we are now! This stuff here looks great, I'll have to give it a try later today when I have some time. I feel as though I ought to give you all somthing also, so heres my bit of projectile spread code that gets decressed accuracy as the gun is fired. I at the moment just have it using the energy bar for the ammount of accuracy
On your weapons *.cs file, under server/scripts dirrectory for example. Change the onFire function to this.
function RifleImage::onFire(%this, %obj, %slot)
{
%energy = %obj.getEnergyLevel();
if (%energy<10)
{
%obj.setEnergyLevel(%obj.getEnergyLevel()+2);
}
else
{
%obj.setEnergyLevel(%obj.getEnergyLevel()-9);
}
%projectile = %this.projectile;
%obj.decInventory(%this.ammo,1);
%spread = %this.projectileSpread;
%spread = %spread $= "" ? 0.001 : %spread;
%finalspread = %spread <= 0 ? 0.001 : %spread;
%vector = %obj.getMuzzleVector(%slot);
%objectVelocity = %obj.getVelocity();
%vector1 = VectorScale(%vector, %projectile.muzzleVelocity);
%vector2 = VectorScale(%objectVelocity, %projectile.velInheritFactor);
%velocity = VectorAdd(%vector1,%vector2);
%x = (getRandom() - 0.5) * 2 * 3.1415926 * %finalspread / (%obj.getEnergyLevel() * 0.5);
%y = (getRandom() - 0.5) * 2 * 3.1415926 * %finalspread / (%obj.getEnergyLevel() * 0.5);
%z = (getRandom() - 0.5) * 2 * 3.1415926 * %finalspread / (%obj.getEnergyLevel() * 0.5);
%mat = MatrixCreateFromEuler(%x @ " " @ %y @ " " @ %z);
%velocity = MatrixMulVector(%mat, %velocity);
%p = new (%this.projectileType)()
{
dataBlock = %projectile;
initialVelocity = %velocity;
initialPosition = %obj.getMuzzlePoint(%slot);
sourceObject = %obj;
sourceSlot = %slot;
client = %obj.client;
};
MissionCleanup.add(%p);
return %p;
}Make sure that in player.cs that the maxEnergy is set to 100, it shouldn't matter how much it is, but I've found that 100 works best. Also the rechargeRate should be set to 0.856
Then last of all, under ShapeBaseImageData, put
projectileSpread = 0.1; // 11 / 1000and give it a try! Well, hope it works for all of you.
-Max
#30
02/19/2006 (9:57 am)
Thanks for that bit of code Max, I will definately be giving that a try!
#31
@Tim: Collaboration is a great idea, double the skills, double the solving the strange problems and double the amazing outcomes. :) In fact, modeling/texturing and audio is exactly what we need to really give our game a boost in visuals.
02/19/2006 (10:12 am)
Wow thanks, Max! The final touch to an amazing recoil/aim system. :D (Woops, except actually I use the energy bar a shield bar in my game, so oh well.)@Tim: Collaboration is a great idea, double the skills, double the solving the strange problems and double the amazing outcomes. :) In fact, modeling/texturing and audio is exactly what we need to really give our game a boost in visuals.
#32
02/19/2006 (10:19 am)
Well C2, I liked the screen shots i've seen so far of your game in your plan file and I like your attitude. I'm off to bed now but we'll have to arrange a chat on msn or xfire sometime. I'll be in contact.
#33
Here's a vid of what it does when you do not try to stabilize the weapon (you can set the recoil to anything you like in the datablock (including the varience coefficient)):
www.valdegames.com/pig/recoil.avi (~17mb)
As you can see the sniper has more recoil per shot then the AK but the NADE has no recoil. Also (I dunno if you can see this with the bad quality) but the extent of the recoil is randomized (coeffient found randomly using the varience var). It's easiest to see with the AK.
02/19/2006 (10:35 am)
See, now your making me think I should post my code. It seems a lot more effient then what we have here. (And more organized).Here's a vid of what it does when you do not try to stabilize the weapon (you can set the recoil to anything you like in the datablock (including the varience coefficient)):
www.valdegames.com/pig/recoil.avi (~17mb)
As you can see the sniper has more recoil per shot then the AK but the NADE has no recoil. Also (I dunno if you can see this with the bad quality) but the extent of the recoil is randomized (coeffient found randomly using the varience var). It's easiest to see with the AK.
#34
The posting of your code would be very welcome indeed. I know you might be reluctant, but think of how many people you will make happy, not to mention the street cred to be gained for yourself. Also think of all the code from others you have found on this site & put to good use, if everyone kept their code a secret where would any of us be??? Let's keep the circle of good code spinning...
If not than stop showing off!!! : P (joke joke joke, don't get offended)
02/19/2006 (10:42 am)
@ Matt "Mr. Pig" RazzaThe posting of your code would be very welcome indeed. I know you might be reluctant, but think of how many people you will make happy, not to mention the street cred to be gained for yourself. Also think of all the code from others you have found on this site & put to good use, if everyone kept their code a secret where would any of us be??? Let's keep the circle of good code spinning...
If not than stop showing off!!! : P (joke joke joke, don't get offended)
#35
@All - This code is copyrighted and is NOT public donaim. The recoil system is property of Valde Games. The basic "ideas" outlined in this code are public donaim, the system itself is not. Also, it's all scripted.
In each weapons datablock (server):
In each weapons "onFire" function (server):
In my inventory code file (server):
In "generalWeapons.cs" (client):
-------------------------------------------------------------------------------------------------------------------------
Please be respectful and do not steal my system. If you haven't noticed, over time the recoil gets more intense, and over time the recoil "intenseness" decreases. This makes it easier to aim if you fire in bursts. This is one of the things that make my recoil system so dynamic. Please, do not use my code in your game. This is only to learn from. You must include...
-Matt
http://www.valdegames.com
02/19/2006 (10:54 am)
@Tim - lol, good point. Where would we be.@All - This code is copyrighted and is NOT public donaim. The recoil system is property of Valde Games. The basic "ideas" outlined in this code are public donaim, the system itself is not. Also, it's all scripted.
In each weapons datablock (server):
//Weapon Recoil Hooks recoilPitchFactor = 0.025; //Pitch recoil recoilPitchRandomize = 2; //Range to randomize (-/+) recoilYawFactor = 0.0125; //Yaw recoil recoilYawRandomize = 2; //Range to randomize (-/+)
In each weapons "onFire" function (server):
//Apply recoil clientUpdateRecoil(%obj.client, ak47Shape);Replace ak47Shape with the datablock of the weapon.
In my inventory code file (server):
//Tell client to apply recoil
function clientUpdateRecoil(%client, %weaponShape)
{
commandToClient(%client, 'applyRecoil', %weaponShape.recoilPitchFactor, %weaponShape.recoilPitchRandomize, %weaponShape.recoilYawFactor, %weaponShape.recoilYawRandomize);
}In "generalWeapons.cs" (client):
//==============================
//General Weapon Client Commands
//Code (C) Valde Games
//==============================
//------------------------------
//Set vars
//------------------------------
$shotFactor = 1;
//------------------------------
//Basic Weapon Recoil Functions
//------------------------------
//Apply recoil pitch (+ up/- down)
function applyPitchRecoil(%recoilFactor)
{
$mvPitch -= %recoilFactor; //Apply recoil
}
//Apply recoil yaw (+ right/- left)
function applyYawRecoil(%recoilFactor)
{
$mvYaw += %recoilFactor; //Apply recoil
}
//Calcs difference in weapon recoil (random)
function calcRecoil(%recoilFactor, %randomizeFactor)
{
%factor = getRandom(-1 * %randomizeFactor, %randomizeFactor); //Get random factor
//Calc factor (if negitive)
if (%factor < 0)
{
%factor = (-1 * %factor) / %randomizeFactor; //Fix recoil
}
else if (%factor == 0)
{
%factor = %randomizeFactor; //Have some recoil
}
%recoil = %recoilFactor * %factor; //Get recoil
%recoil = %recoil * $shotFactor;
return %recoil;
}
//Apply weapon recoil
function doRecoil(%pitchFactor, %pitchRandomize, %yawFactor, %yawRandomize)
{
applyPitchRecoil(calcRecoil(%pitchFactor, %pitchRandomize)); //Apply pitch
//Calc left or right yaw
if (getRandom(1, 4) == 3) //Left recoil
{
applyYawRecoil(-1 * calcRecoil(%yawFactor, %yawRandomize)); //Apply yaw (left)
}
else //Right recoil
{
applyYawRecoil(calcRecoil(%yawFactor, %yawRandomize)); //Apply yaw (right)
}
if ($shotFactor < 1.2)
{
$shotFactor += 0.05;
if (!$checkingShotFactor)
{
checkShotFactor();
}
}
}
//Manage decrease
function checkShotFactor()
{
if (!$checkingShotFactor)
{
$checkingShotFactor = true;
schedule(250, 0, checkShotFactor);
}
else
{
if ($shotFactor <= 1)
{
$checkingShotFactor = false;
}
else
{
lowerShotFactor();
schedule(250, 0, checkShotFactor);
}
}
}
//Decrease
function lowerShotFactor()
{
$shotFactor -= 0.05;
}
//------------------------------
//Client Commands
//------------------------------
//Cleint command (called by server)
function clientCmdapplyRecoil(%pitchFactor, %pitchRandomize, %yawFactor, %yawRandomize)
{
doRecoil(%pitchFactor, %pitchRandomize, %yawFactor, %yawRandomize);
}-------------------------------------------------------------------------------------------------------------------------
Please be respectful and do not steal my system. If you haven't noticed, over time the recoil gets more intense, and over time the recoil "intenseness" decreases. This makes it easier to aim if you fire in bursts. This is one of the things that make my recoil system so dynamic. Please, do not use my code in your game. This is only to learn from. You must include...
//============================== //General Weapon Client Commands //Code (C) Valde Games //==============================...if you use any of it. And, it would be nice to see a "Special Thanks to Valde Games" in your about page. Sorry I'm so protective but I'm really happy with the way the system came out and our game will be the third game that I know of with a "Full Physical" recoil system.
-Matt
http://www.valdegames.com
#36
Modifying the objects pitch/yaw directly on the server is a much better approach if you're going to make it safer against hacks.
02/19/2006 (10:59 am)
That code is still very prone to cheating, but if you're doing single player then it's all fine.Modifying the objects pitch/yaw directly on the server is a much better approach if you're going to make it safer against hacks.
#37
--------------------------------------
All you need to do is scan the MD5 of that .dso file to stop cheaters. Maybe I'll move it into the engine, we'll see.
02/19/2006 (11:01 am)
I am aware of that. I will be implamenting an anti-cheat program into my game, that's why I'm not worried. This code could be easily converted into C++ engine code. It'd take me no more then 2 min to do it. Like I said, don't use the code. Just the idea.--------------------------------------
All you need to do is scan the MD5 of that .dso file to stop cheaters. Maybe I'll move it into the engine, we'll see.
#38
02/19/2006 (5:54 pm)
So.. in order to use that we'd have to design our own based on the "ideas" of that system. And if we used and modified as is we'd be attacked legally. Cool. I'll stick with Tim's. :)
#39
02/19/2006 (6:36 pm)
:P Good. You can change it. Like use the code as a base, but don't implament a system that works the exact same way.
#40
04/13/2006 (8:12 am)
@Max Thomas: What do you think would be the best way to get away from using the energy as part of the projectile spread? I use energy for shields, rendering it as it is, non-plausible.
Torque Owner Tim Heldna
@C2, I will post my changes as soon as I have time. As I mentioned it was from a very old copy of Torque and I've got a lot of custom code in my projectile.cc file. I'd like to wrench through it and tidy it up a bit before I release anything. I went through it briefly last night and think it would only be a matter of shifting some code around.
On a side note C2, thanking you in advance for taking the time to put together a resource on the 1st person weapon, hands stuff!!!