2D游戏中的跳跃数学

Pab*_*noz 8 java math physics 2d

我在J2ME工作,我的gameloop执行以下操作:

public void run() {
        Graphics g = this.getGraphics();
        while (running) {
            long diff = System.currentTimeMillis() - lastLoop;
            lastLoop = System.currentTimeMillis();
            input();
            this.level.doLogic();
            render(g, diff);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                stop(e);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

所以它只是一个基本的游戏循环,该doLogic()函数调用场景中角色的所有逻辑函数并render(g, diff)调用场景animateChar中每个角色的animChar函数,然后,Character类中的函数设置屏幕中的所有内容,如下所示:

protected void animChar(long diff) {
        this.checkGravity();
        this.move((int) ((diff * this.dx) / 1000), (int) ((diff * this.dy) / 1000));
        if (this.acumFrame > this.framerate) {
            this.nextFrame();
            this.acumFrame = 0;
        } else {
            this.acumFrame += diff;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这确保了我必须根据机器从一个循环到另一个循环所花费的时间来移动所有东西(记住它是电话,而不是游戏装备).我确信这不是实现这种行为的最有效方式,所以我完全乐于在评论中批评我的编程技巧,但在这里我的问题是:当我让我的角色跳跃时,我所做的就是我把他的dy为负值,比如200,我将布尔跳转设置为true,这使得角色上升,然后我调用checkGravity()了这个函数,确保上升的所有内容都必须关闭,checkGravity同时检查字符是否为在平台上,所以为了你的时间我会把它剥掉一点:

public void checkGravity() {
        if (this.jumping) {
            this.jumpSpeed += 10;
            if (this.jumpSpeed > 0) {
                this.jumping = false;
                this.falling = true;
            }
            this.dy = this.jumpSpeed;
        }
        if (this.falling) {
            this.jumpSpeed += 10;
            if (this.jumpSpeed > 200) this.jumpSpeed = 200;
            this.dy = this.jumpSpeed;
            if (this.collidesWithPlatform()) {
                this.falling = false;
                this.standing = true;
                this.jumping = false;
                this.jumpSpeed = 0;
                this.dy = this.jumpSpeed;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

所以,问题是,这个函数无论diff如何都会更新dy,使得角色在慢速机器中像超人一样飞行,我不知道如何实现diff因子,这样当一个角色跳跃时,他的速度就会下降与游戏速度成比例的方式.任何人都可以帮我解决这个问题吗?或者指出如何以正确的方式在J2ME中进行2D跳转.

小智 5

你不应该根据经过的时间调整jumpSpeed吗?也就是说,速度可能会以-75 /秒的速度变化,因此你的差异应该是应用于jumpSpeed的变化量的权重.

所以将diff传递给checkGrav并执行类似... jumpSpeed + =(diff*(rate_per_second))/ 1000;

(假设diff以毫秒为单位)

(理想情况下,这会使它像真正的引力一样:D)