Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Making realistic recoil for 3d gun and realistic walking  (Read 4571 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight

Medals: 28
Projects: 3
Exp: 5 years

« Posted 2013-09-25 00:36:33 »

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(, 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  Undecided

Offline Troncoso

JGO Coder

Medals: 20

« Reply #1 - Posted 2013-09-25 11:22:08 »

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:
rifle recoil:

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.
Offline wreed12345

JGO Knight

Medals: 28
Projects: 3
Exp: 5 years

« Reply #2 - Posted 2013-09-25 21:06:18 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kramin42
« Reply #3 - Posted 2013-09-25 21:37:42 »

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 = x*angleFactor
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: (click inside the box to simulate firing, try clicking rapidly too)

"All science is either physics or stamp collecting." - Ernest Rutherford.
Pages: [1]
  ignore  |  Print  

EgonOlsen (79 views)
2018-06-10 19:43:48

EgonOlsen (59 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (261 views)
2018-05-13 18:23:11

nelsongames (159 views)
2018-04-24 18:15:36

nelsongames (158 views)
2018-04-24 18:14:32

ivj94 (901 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (813 views)
2018-03-24 14:43:53

Solater (177 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!