A player-falling system (basically gravity)

2019-02-15 08:33发布

问题:

I am making a game that is similar to Doodle Jump, getting your player as high as possible. Now, I got my player working, and moving. But, the problem is, I don't have gravity, or anything that will make the player fall down onto the ground again. Do you guys have any idea of doing this? I tried having the player get a constant force, being pushed down at all times, but, it's not smooth, and it doesn't act like real falling. Can I have some help with making this player-falling system?

Edit:

    GRAVITY = 10;
    TERMINAL_VELOCITY = 300;
    vertical_speed = 0;

    public void fall(){ 
    this.vertical_speed = this.vertical_speed + GRAVITY;
    if(this.vertical_speed > TERMINAL_VELOCITY){
        this.vertical_speed = TERMINAL_VELOCITY;
    }
    this.y = this.y - this.vertical_speed;
}

I made this, didn't work, shoots my player up in the air.

回答1:

In the real world gravity will increase the rate of a fall by a constant amount over time (9.8 meters per second per second). You could simulate this by giving the player a vertical speed (when they jump or fall off a platform) and then subtracting a constant amount from that value every time round the main game loop so that they accelerate over time. You'll want to put a maximum limit on this (terminal velocity) otherwise when they fall a long way they could hit ludicrous speed fairly quickly. The pseudo-code would look something like this:

const GRAVITY = 10;
const TERMINAL_VELOCITY = 300;

object Player 
{
    int vertical_speed = 0;
    int vertical_position;  

    function fall ()
    {
        this.vertical_speed = this.vertical_speed + GRAVITY;
        if (this.vertical_speed > TERMINAL_VELOCITY)
        {
            this.vertical_speed = TERMINAL_VELOCITY;
        }
        this.vertical_position = this.vertical_position - this.vertical_speed;
    }
}

EDIT: 9.8 Metres Per Second Per Second is correct! Please don't edit it! Acceleration is measured as change in velocity over time, expressed in metres per second per second. 9.8 meters per second per second means that after 1 second a stationary object would have accelerated enough to be travelling at 9.8 m/s. After 2 seconds, it will have attained a speed of 19.6 m/s. After 3 seconds it will have attained a speed of 29.4 m/s and so on.

I honestly don't believe I even had to explain that.



回答2:

Do you know the formula for gravity?

velocity = acceleration * time

acceleration is the gravitational acceleration.

time is the amount of time that has passed.

Also,

distance = 1/2 * acceleration * time**2


回答3:

A formula to compute the height of an entity with gravity at any given time is like so:

       g * t ^ 2
s(t) = --------- + v * t + h
           2    

Where s is the function of time (time to height), g is the gravity factor (9.8 for metres), v is the original upwards velocity, and h is the original height.



回答4:

Instead of having a constant force acting on the person, you need to have the person accelerate while they fall.

They should start falling with 0 velocity. Then, you should increase the force as they fall.

To do this, you will need to update their velocity over time:

Something like this:

if (stillFalling) {
    velocity = velocity + (gravity_constant) * time_interval;
} else {
    velocity = 0;
}

You will want to continuously update the velocity.



回答5:

You will find explanations and a demo on that website. I suggest too that you read a book on physics or at least some wiki article about gravity.



回答6:

From my Experience, do something like this.

public void run() {
    if(velY+g>TerminalVel) {
        velY=TerminalVel;
    } else {
        velY+=g; 
    }
    y+=velY;
}

The method run() should be in a loop.



标签: java gravity