Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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 1231 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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
1  
2  
3  
4  
5  
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  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: 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.
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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: http://kramin42.github.io/recoil.htm (click inside the box to simulate firing, try clicking rapidly too)

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (22 views)
2014-11-25 11:53:45

Norakomi (19 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (68 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!