How would you simulate gun recoil in a FPS ?

Started by
0 comments, last by softdev 12 years, 1 month ago
My goal is to remake the counter strike recoil feel, especially for automatic guns, and I'm having an hard time.

I was told on math.stackexchange with this function https://www.google.fr/search?aq=f&ix=hcb&ie=UTF-8&q=plot+5x*e%5E(1-5x)+from+0+to+1
which sort of simulate some sort of impulse, to tweak its quickness I just have to change '5' to something else, and it still results with something between 0 and 1.

For single shots it behave quite nicely, but with automatic guns in counter strike, the recoil sort of accumulate and adds up to an higher angle.

Any idea with some kind of equation or algorithm ?
Advertisement
First you need some kind of construct to ensure your recoil never exceeds a given value, so your angle doesn't grow to infinity. One such construct is:

Recoil = 1 - 1 / (1 + impulse power)

This way Recoil will always be between 0 and 1. It will be zero when the impulse power is zero, and it will tend to 1 as the power increases. The function behaves like this:

http://www.wolframal...om+x+%3D+0+to+5

Raising the (1 + power) term to higher powers will lead to faster recoil accumulation, but it will still remain between zero and one. Lower powers will similarly lead to slower recoil accumulation. Of course, don't use negative powers, or.. it doesn't work anymore. And make sure your power is never negative either. You can tweak those exponents for different guns, i.e. a Uzi probably will have a low exponent whereas a sniper rifle will have a big one.

At that point you have a recoil factor between zero and one, you can then scale it, and perhaps add a small initial angle so your shots at zero impulse power aren't spot on.

As for impulse power, you can simply keep a counter, increase it for each shot fired, and decrease it by a little each frame (clamping it to zero), this way recoil will gradually fade if you stop shooting. Of course you could be more elaborate by using exponential decay to model that, but that's off topic.

Now you can obviously use another function (even the one you suggested in your post) but you will need to use some form of smooth clamping to prevent the recoil becoming unrealistically big. And hard-clamping it doesn't really work well, it's pretty noticeable from a user point of view.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

By the looks of the plot in the link you provided, the curve looks right to what you said. I changed the 5 in the exponent to 9 and got the spike I think you are referring to. To cut a long story short, unless you have a downward force applied through whatever physics engine you use, it won't work. However it works perfectly fine if you simply limit the y with whatever you get for the result. So each gun can have a different limit. This would be up to you and how you are processing the recoil. Don't change the curve, the algorithm is fine. Just don't forget, that if you later want to use more accurate physics for the character (ie. 3rd person view), you are getting a visual translation as opposed to a gravity calculation.

This topic is closed to new replies.

Advertisement