Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
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  
  Gravity  (Read 2904 times)
0 Members and 1 Guest are viewing this topic.
Offline masteryoom

JGO Coder


Medals: 5
Projects: 3


If you look closely, you might see it turning...


« Posted 2012-12-31 06:01:24 »

This programming of gravity that I am about to show you is probably not the best in the world. It is probably one of the worst. But here I go...
(Any of the code could be messy, not needed or could be done a better way)
Let's talk about gravity first. Gravity is a force that keeps us stuck to Earth. Many things, like gyroscopes, would not work without gravity. We wouldn't even be here talking if it wasn't for gravity. Gravity is very hard to escape. Gravity also builds up as you fall down, so you go faster as you fall down longer. Unless you are making some sort of space game, gravity is an absolute must.

Gravity



So, first you will have to make something to check if you have ground beneath you. I don't care how you do it. Then you need to make something to keep that result.
(There are probably many, many, ways to check)

1  
2  
boolean isSupported;
boolean isGround;

You may be wondering why there is a variable called isGround. This means that there is ground beneath your character. isSupported means that if there is something directly beneath your character.
Now you need some more variables. These are:
1  
2  
3  
4  
5  
6  
final int gravity = 20;
int gravitypull = 1;
boolean isRunning = true;
int jumpingPower = 4;
boolean jumping = false;
int height = 0;


Then you need something to check to see if there is ground below you and if you are jumping.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public void gravityEffect(){
   if (!isSupported){
      if (jumping){
         
         if ((gravity / 2) == height){
            jumpingPower /= 2;
         }else if ((gravity /4) == height){
            jumpingPower /= 4;
         }
         height += jumpingPower;
         System.out.println(height);
      }else if (!isGround | height >= gravity){
         height -= gravitypull;
         jumping = false;
         System.out.println(height);
         gravitypull *= 2;
      }
   }
}


It stops you if your height reaches the gravity's limit.

Finally, you need something to stop you when you are at ground. (I have put 0 in, but you will have to put in the number where the bottom of the ground is)

1  
2  
3  
4  
5  
if (isGround & height <= 0){
      height = 0;
      gravitypull = 1;
      isSupported = true;
   }

It's as simple as that! Grin

Smiley
Offline masteryoom

JGO Coder


Medals: 5
Projects: 3


If you look closely, you might see it turning...


« Reply #1 - Posted 2012-12-31 06:02:28 »

NOT FINISHED!!! (I accidentaly pressed the post button  Tongue

Smiley
Offline masteryoom

JGO Coder


Medals: 5
Projects: 3


If you look closely, you might see it turning...


« Reply #2 - Posted 2012-12-31 06:09:57 »

Finished now.  Wink

Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #3 - Posted 2012-12-31 07:21:05 »

This is not the best way of dealing with gravity in my opinion. Gravity is something that affects objects constantly, whether they are in free-fall or at rest. Therefore, it should be modeled as such in code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
//fields
private float x, y;
private float vx, vy;
private final float g = -10;
private final float jumpingPower = 6;

private final int FPS = 60;
private final float DELTA = 1f/FPS;

public void update() {
    //crap here

    if(/*spacebar is pressed*/)
        vy += jumpingPower; //you probably want a mechanism limiting the frequency of jumps
   
    //gravity affects vy constantly
   vy += g;
    y += vy;
   
    //collision detection
   for(Blocks b : randomBlocks) {
        if(this.intersects(b))
            vy = b.y + height;
    }
}


A side effect of the above code is that you have to deal with the X and Y axes separately, which ultimately means running the collision detection code twice.

Offline counterp

Senior Member


Medals: 11



« Reply #4 - Posted 2013-01-02 15:30:25 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
//fields
private float x, y;
private float vx, vy;
private float elapsedTime = -1;
private final float g = -10;
private final float jumpingPower = 6;

private final int FPS = 60;
private final float DELTA = 1f/FPS;

public void update() {
    //crap here

    if(/*spacebar is pressed*/) {
        vy = jumpingPower; //you probably want a mechanism limiting the frequency of jumps
       elapsedTime = 0;
    }
   
    //gravity affects vy constantly
   if (elapsedTime != -1) {
        vy = elapsedTime * DELTA * jumpingPower + g / 2 * elapsedTime * elapsedTime * DELTA * DELTA; // alternatively, set initial value of vy as the stuff before the + sign, and increment by the stuff after the + sign
       y += vy;
        elapsedTime++; // alternatively, increment by 'DELTA' and get rid of 'DELTA' in equation for solving vy
   }
   
    //collision detection
   for(Blocks b : randomBlocks) {
        if(this.intersects(b)) {
            if (vy >= 0) { // We hit our head on something, start falling
               elapsedTime = -2f * jumpingPower / g; // The time when vy changes from positive to negative, i.e. you start falling. Completely inelastic collision.
           } else { // We landed on a platform, stop moving
               vy = 0;
                elapsedTime = -1;
            }
        }
    }
}


That's my approach, from memory. Might have mistakes?
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.

xsi3rr4x (35 views)
2014-04-15 18:08:23

BurntPizza (31 views)
2014-04-15 03:46:01

UprightPath (46 views)
2014-04-14 17:39:50

UprightPath (29 views)
2014-04-14 17:35:47

Porlus (46 views)
2014-04-14 15:48:38

tom_mai78101 (67 views)
2014-04-10 04:04:31

BurntPizza (127 views)
2014-04-08 23:06:04

tom_mai78101 (227 views)
2014-04-05 13:34:39

trollwarrior1 (192 views)
2014-04-04 12:06:45

CJLetsGame (199 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!