Java 2D平台重力改进

时间:2016-05-25 11:16:24

标签: java game-physics

我已经开始用Java制作一个小游戏,并且已经发生碰撞,并且是一种重力形式,玩家的速度不会随着现实生活的下降而增加。我想改进这种重力形式,使其更加逼真,我也有一个碰撞阻挡地板,当玩家走出地板时,它会保持在那个高度而不会下降。

以下是我的Player类:

    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;

    public class Player {
        private int x, y, width, height, lx, ly, dx, dy;
        private final int FallSpeed = 2;
        private final long JumpingTime = 8;
        public boolean jumping = false, falling = true;

        public Player(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            lx = x;
            ly = y;
            dx = x;
            dy = y;
            new Thread(new gravityThread()).start(); // Make user fall in case they are in the air
        }

        public void setX(int x) {
            this.x = x;
            lx = x;
            dx = x;
        }

        public void setY(int y) {
            this.y = y;
            ly = y;
            dy = y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public int getWidth() {
            return width;
        }

        public int getHeight() {
            return height;
        }

        public Rectangle getBounds() {
            return new Rectangle(x, y, width, height);
        }

        public void update() {

        }

        public void move(int ax, int ay) {
            dx += ax;
            dy += ay;
            checkCollisions();
        }

        public void checkCollisions() {
            if (collided()) {
                falling = false;
                dx = lx;
                dy = ly;
            }
            x = dx;
            y = dy;
            lx = x;
            ly = y;
        }

        public boolean collided() {
            Rectangle desired = new Rectangle(dx, dy, width, height);
            for (Rectangle r : CollisionManager.collisions) if (r.intersects(desired)) return true;
            return false;
        }

        public void jump() {
            falling = false;
            jumping = true;
            new Thread(new gravityThread()).start();
        }

        public void render(Graphics2D g2d) {
            g2d.setColor(Color.black);
            g2d.fill(getBounds());
        }

        private class gravityThread implements Runnable {

            int i = 0;

            @Override
            public void run() {
                while(jumping) {
                    try {
                        i++;
                        Thread.sleep(JumpingTime);
                        move(0, -FallSpeed);
                        if (i == 40) {
                            jumping = false;
                            falling = true;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.exit(0);
                    }
                }
                while(falling) {
                    try {
                        Thread.sleep(JumpingTime);
                        move(0, FallSpeed);
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.exit(0);
                    }
                }
            }
        }
    }

在另一个处理将所有内容绘制到窗口的类中,我使用此片段控制玩家移动:

    public void onUpdate() {
            if (this.screenFactory.getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_A))
                player.move(-WalkSpeed, 0);
            if (this.screenFactory.getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_D))
                player.move(WalkSpeed, 0);

            if (this.screenFactory.getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_SPACE)) {
                if (!player.jumping && !player.falling)
                    player.jump();
            }

            if (player.getY() >= screenHeight - player.getHeight()) {
                player.setY(screenHeight - player.getHeight());
                player.falling = false;
            }
            if (player.getY() <= 0)
                player.setY(0);
            if (player.getX() >= screenWidth - player.getWidth())
                player.setX(screenWidth - player.getWidth());
            if (player.getX() <= 0)
                player.setX(0);
    }

1 个答案:

答案 0 :(得分:1)

  • g-force为9.81 m/s²
  • 得到你的“绘制更新速度”,这是s(实际上,你应该每秒更新至少30次。计算时记住这一点)
  • 得到你的长度定义,这是m

Cuple对象的位置取决于当前速度v和每帧的加速度(g力)a。 事情越长,下降的速度就越快......

编辑:在下降/跳跃更加逼真的情况下进行侧向移动,你需要使用sin(),cos()。