我在跳跃方面存在一致性问题

时间:2013-07-09 23:52:05

标签: java lwjgl slick2d

我已经建立了一个非常基本的平台游戏,&我现在正在设置它。

除了跳跃之外,一切都很完美,我的跳跃长度非常不一致。它们的范围从很短到很长。

我不习惯使用Slick2D,JWJGL或delta整数,请帮忙!

以下是该级别的完整代码:

package game;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class LevelOne extends BasicGameState{

    Animation walkingLeft, walkingRight, still;
    int[] duration = {200,200,200};

    Image p;
    Image map;

    public String mouse = "Mouse not detected";
    public String gpos = "null";
    public int health = 1000;
    float CharX = 0;
    float CharY = 0;
    float PosX = CharX + 100;
    float PosY = CharY + 300;
    boolean jumping = false; 
    float verticalSpeed = 0.0f;


    public LevelOne(int state){

    }

    public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
        p = new Image("res/char.png");
        map = new Image("res/map.png");


        Image[] Left = {p.getSubImage(0,32,32,32), p.getSubImage(32,32,32,32), p.getSubImage(64,32,32,32)};
        Image[] Right = {p.getSubImage(0,64,32,32), p.getSubImage(0,64,32,32), p.getSubImage(0,64,32,32)};


        walkingLeft = new Animation(Left, duration, false);
        walkingRight = new Animation(Right, duration, false);
        still = walkingLeft;
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
        g.setColor(Color.magenta);
        g.drawString("Developers Build", 650, 100);
        g.setColor(Color.white);
        g.drawString(mouse, 650, 150);
        g.drawString(gpos,650, 200);                   //Developers Build Sidebar thing
        g.drawString("Class: ", 650, 250);
        g.drawString("" + this.getClass(), 670, 270);
        g.drawString("State: " + this.getID(), 650, 300);
        g.drawString("Stage: Charlie", 650, 350);
        g.drawRect(620, 70, 400, 320);


        map.draw(CharX, CharY);                 //Floor



        g.setColor(Color.white);
        if(health < 500){
            g.setColor(Color.red);
        }
        g.drawString("Health: " + health, 10, 50);  
        g.setColor(Color.white);                                                //Health & Level
        g.drawString("Level: 1", 10, 70);


        still.draw(PosX, PosY);                       //Draw Character





    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
        Input input = gc.getInput();

        int MouseX = Mouse.getX();
        int MouseY = Mouse.getY();
        mouse = "JPos: " + MouseX + " , " + MouseY;
        gpos = "GPos: " + MouseX + " , " + (460 - MouseY);


        if(input.isKeyDown(Input.KEY_D)){
            still = walkingRight;
            CharX -= delta * .2f;
        }

        if(input.isKeyDown(Input.KEY_A)){
            still = walkingLeft;
            CharX += delta * .2f;
            if(CharX > 100){
                CharX -= delta *.2f;
            }
        }

        if (input.isKeyPressed(Input.KEY_W) && !jumping) { 
            verticalSpeed = -1f * delta;//negative value indicates an upward movement 
            jumping = true;
       }

       if (jumping) { 
            verticalSpeed += .0099f * delta;//change this value to alter gravity strength          
       }

       if(PosY > 300){         
           jumping = false;
           verticalSpeed = 0;          
           PosY = 300;
       }

       PosY += verticalSpeed;



        if(health == 0){
            CharX = 0;
            health = 1000;
        }


    }

    public int getID(){
        return 2;
    }
}

3 个答案:

答案 0 :(得分:0)

您需要按delta缩放跳跃速度。现在你只是否定delta,这意味着你的跳跃力等于每毫秒帧之间的经过时间。这太变了。

您的代码应该读取类似的内容:

float JUMP_POWER = .1f;
if(input.isKeyPressed(Input.KEY_W) && !jumping){
    verticalSpeed = -delta*JUMP_POWER;
}

这意味着你的角色在跳跃时的垂直速度为每毫秒0.1个像素。如果delta是以毫秒为单位:)不确定是否是。如果你没有看到角色移动位置,只需增加跳跃力。

您这样做是因为每个动作都需要按时间缩放。物理在现实生活中是多么简单。速度随时间改变位置。因此,为了模拟这一点,您需要计算相对于时间步之间的时间的速度。

答案 1 :(得分:0)

如果delta是某种“经过时间”的值,那么你就不应该乘以它。跳跃速度与帧速率无关。只需选择一个好的速度:

if (input.isKeyPressed(Input.KEY_W) && !jumping)
{
    verticalSpeed = -1.0f;
    jumping = true;
}

玩一下这个值。

答案 2 :(得分:0)

你实际上错过了最后一块拼图,如果你回想一下物理课,实际上是以恒定加速度改变位置:

d =(v * t)+(1/2 * a * t ^ 2)

其中 d是距离 v是速度 是时候了 a是加速度

像...一样的东西。

if (input.isKeyPressed(Input.KEY_W) && !jumping) { 
        verticalSpeed = JUMP_POWER;
        jumping = true;
}
PosY = PosY + (verticalSpeed * deltaTime) + (0.5f * gravity * deltaTime * deltaTime);
verticalSpeed = gravity * deltaTime;

因此,您不仅需要按时间倍增速度,而且要使游戏在任何FPS上运行相同,您还需要添加1/2 * a * t ^ 2来计算正确的距离。

相关问题