如何在libgdx中保持相同的跳跃高度 [英] How to keep the same jump height in libgdx

查看:111
本文介绍了如何在libgdx中保持相同的跳跃高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用libgdx开发2D视频游戏.当我尝试使身体跳起来时,我遇到了一个问题. 向右移动后,它没有按预期跳.(我只能向右移动或跳)

I'm developing a 2D video game using libgdx. I ran into a problem when I try to make jumping a body. It does not jump as expected after making it moving to the right.(I can only move to the right or Jump)

如果身体在向右移动之前跳了起来,一切都会好起来的.但是如果我决定在将身体向右移动之后跳起来.身体不再跳到相同的高度(跳得不那么高).而且我不知道为什么.

If the body jumps before it's moving to the right everything goes fine. But If I decide to make jumping the body after moving it to the right. The body no longer jumps to the same height (It jumps less high). And I don't figure out why..

我跳身体的方法:

    if (player.isPlayerOnGround()) {
        body.applyForceToCenter(0, 200, true);
    }

我将身体向右移动的方法

My method to move the body right

    if (player.isPlayerOnGround()) {
        body.setLinearDamping(0f);
        body.setLinearVelocity(1f,0f);
        isMoving = true;
    }

我停止身体向右移动的方法:

My method to stop the body moving right :

    body.setLinearDamping(5f);
    isMoving = false;

世界使用-9.81f重力,将身体1f用于质量.

The world use a -9.81f gravity and the body 1f for the Mass.

P.S:对不起,我的英语不好,这不是我的母语.

P.S : Sorry for me bad english, it's not my native language.

谢谢.

推荐答案

第一件事:永远不要用力跳跃.根据作用力的时间长短,力量会产生不同的影响.第二:不要使用linearDamping.它使您的物理学浮空而不是真实的.您可以在跳跃方法中使用冲动而不是用力(实际上效果不佳).我正在使用这种方法,并且效果很好

First thing: never use forces to jump. Force has different effects based on how long that force acted. Second: don't use linearDamping. It makes your physics floaty and not real. You could use impulse instead of force in jumping method (it doesn't work very well actually). I'm using this method and it works perfectly

    public void jump() {
        if (jumpDelta >= Constants.PLAYER_JUMP_RATE) {
            grounded = level.getContactListener().numFootContacts > 0;
            if (grounded) {
                body.setLinearVelocity(body.getLinearVelocity().x, 7);
                jumpDelta = 0;
            }
        }
    }

if (jumpDelta >= Constants.PLAYER_JUMP_RATE)防止跳得太快的情况下(例如一次跳两次或多次),grounded = level.getContactListener().numFootContacts > 0;检查玩家是否在平台上,最后,该body.setLinearVelocity(body.getLinearVelocity().x, 7);会改变身体的垂直速度.更改速度比施加脉冲效果更好,因为脉冲不会设置速度,而是会增加速度.因此,如果玩家以-3 m/s的垂直速度向下移动,则其速度将变为4,而不是我们想要的7.

Where if (jumpDelta >= Constants.PLAYER_JUMP_RATE) prevents from too fast jumping (like two or more jumps at once), grounded = level.getContactListener().numFootContacts > 0; checks if player is on platform and finally this body.setLinearVelocity(body.getLinearVelocity().x, 7); changes body's vertical velocity. Changing velocity works better than applying impulse because impulse doesn't set velocity, it increases velocity. So if player was moving down with vertical velocity -3 m/s then its velocity will become 4, not 7 as we wanted.

P.S.代替线性阻尼,我使用这种方法

P.S. Instead of linear damping i use this method

    public void stopMoving() {
        if (grounded) {
            if (Math.abs(body.getLinearVelocity().x) <= 0.5f)
                body.setLinearVelocity(0, body.getLinearVelocity().y);
            else
                body.applyLinearImpulse(-direction * 0.5f, 0,
                        body.getPosition().x, body.getPosition().y, true);
        } else if (Math.abs(body.getLinearVelocity().x) <= 0.1f)
            body.setLinearVelocity(0, body.getLinearVelocity().y);
        else
            body.applyLinearImpulse(-direction * 0.1f, 0, body.getPosition().x,
                    body.getPosition().y, true);
    }

此方法看似过于复杂,但实际上非常简单.第一部分处理身体在地面上的运动,第二部分在空中处理. if语句阻止在停止时更改身体的方向,如果身体向右移动,direction变量可以为1;如果身体向左移动,则direction变量可以为-1;如果身体不移动,则为0.

This method can seem too complex but it's really simple. First part handles body's movement on ground and second in the air. if-statements prevent from changing body's direction while stopping and direction variable can be 1 if body's moving right, -1 if body's moving left and 0 if body isn't moving.

这篇关于如何在libgdx中保持相同的跳跃高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆