Making realistic recoil for 3d gun and realistic walking

Hey guys, haven’t posted on here in a while but i recently started doing some 3d stuff, and I was having trouble thinking about how to do recoil of a gun properly. Just so you know the gun is set up like a gun in any cod game (that viewpoint). So i was thinking maybe using a sin function to make it move slowly make and forth after shooting. Something like this old code i pulled from one of my older games

batch.draw(Assets.star, starX, (MathUtils.sinDeg(derp) * 50) + 200);
		derp += 10;
		if (derp > 360) {
			derp = 0;
		}

Then I am not sure about the up and down movement, or even left to right (maybe some similar sin function to go left and right but of a lower scale.)

Then for walking I think something similar may be in effect with sin. From what I think that would make it go up and down smoothly. Maybe I will add small screen rotation in with that I am not too sure… Any suggestions here are welcome! I did google the recoil and couldn’t find anything related to video games :-\

Considering different guns are held in different ways, the effect of recoil is going to animate differently.
If it’s a hand gun, you won’t really see it move back on the z axis (well, you will, but the effect will be very slight), the barrel will just rise. Unless, it’s something like a .50 Desert Eagle. Then the movement will be a lot more exaggerated.

If it’s 2-handed weapon, the proper way to hold it generally is to have the butt against the shoulder. This is to help reduce recoil by letting the shoulder absorb it. You’ll see the gun move straight back a bit, but the barrel doesn’t move much in any other direction.

Either motion I don’t think could be represented by a sin wave. The initial force is very quick, and then the recover is a lot slower. (Slower is compared to the initial shot reaction. Recovery should be quick too if you know how to shoot a gun)

pistol recoil: http://www.youtube.com/watch?v=_O-Wedt3N4U
rifle recoil: http://www.youtube.com/watch?v=EHPbASqxkH8

I know I didn’t really give you much help on the actual math, but I figure if you consider how recoil actually plays out it may help a bit. If I had to graph it over the time from the trigger pull to completely re-adjusting for the next shot, it’d probably exponentially decrease (roughly (consider that the beginning of the recoil reaction is faster than the end)) with a change in direction in there somewhere.

Thanks a lot, that will help a lot. Now i just gotta figure out the math

I would model the recoil as a damped spring:

x : the current amount of recoil (this should decay exponentially after firing)
v : rate at which x is changing
k : spring constant
d : damping factor (must be between 0 and 1, where 1 is no damping. Try something like 0.9 for starters)

When the gun is fired you would add something depending on the gun’s power to v:
v += gunpower

Then every frame:
angle += v
v += -k*x
v *= d

You’ll need to find good values of k, d, and gunpower. Also, if there is not enough damping you could get oscillations.

Finally you’ll want to get the angle and backward movement of the gun:
angle = xangleFactor
backMovement = x
moveFactor

Where angleFactor and moveFactor are different for different guns as Troncosco said.

Disclaimer: I didn’t actually test this yet but should work, maybe with a little tweaking. Actually I thought it would be fun to whip up a quick example with Processingjs: http://kramin42.github.io/recoil.htm (click inside the box to simulate firing, try clicking rapidly too)