Gravity

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.

[h1]Gravity[/h1]

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)


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:


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.


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)


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

It’s as simple as that! ;D

NOT FINISHED!!! (I accidentaly pressed the post button :stuck_out_tongue:

Finished now. :wink:

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:


//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.

//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?