简体   繁体   English

Libgdx Box2d跳过

[英]Libgdx Box2d Jumping through

I want to make a jump like in the Mario game. 我想像在《马里奥》游戏中那样跳跃。 When you are under the platform and you jump, you can then pass through the collider. 当您在平台下并跳下时,您可以通过对撞机。

When a player's velocity is going down, colliders should wake up. 当玩家的速度下降时,对撞机应醒来。 I know that I should use ContactListener , but when I'm using the contact.setEnable(false) method nothing happens. 我知道我应该使用ContactListener ,但是当我使用contact.setEnable(false)方法时,什么也没有发生。

My contact listener (ground checker works perfectly) 我的联系侦听器(地面检查器工作正常)

world.setContactListener(new ContactListener() {
        @Override
        public void beginContact(Contact contact) {
            if(contact.getFixtureA().getBody().getUserData() == "ground" && contact.getFixtureB().getUserData() == "groundChecker"){
                character.isGrounded = true;
                System.out.println(" Colliding");
            }
        }

        @Override
        public void endContact(Contact contact) {

        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {

        }
    });

What and where should I put to get effect like this. 我应该在什么地方取得这种效果。

enter image description here 在此处输入图片说明

enter image description here 在此处输入图片说明

Collider should have only one side, have somebody deal with it? 对撞机应该只有一侧,有人来应对吗?

Found a solution I hope it will be helpfull. 找到了解决方案,希望对您有所帮助。

world.setContactListener(new ContactListener() {
        @Override
        public void beginContact(Contact contact) {
            //setting isGrounded boolean variable in our character class, but we need to check "player" velocity, because we don't want to enable jumping when only ground will passed througt
            if(contact.getFixtureA().getBody().getUserData() == "ground" && contact.getFixtureB().getUserData() == "groundChecker" && (contact.getFixtureB().getBody().getLinearVelocity().y < 0 || contact.getFixtureB().getBody().getLinearVelocity().y == 0)){
                character.isGrounded = true;
            }
        }

        @Override
        public void endContact(Contact contact) {

        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
            //we have to disable contact when our "player" fixture collide with "ground" fixture
            if(contact.getFixtureA().getBody().getUserData() == "ground" && contact.getFixtureB().getUserData() == "player"){
                contact.setEnabled(false);
            }
            //and we need to disable contact when our "groundChecker" will collide with "ground" and we need to check what velocity.y of player body is, when it is bigger than 0 contact should be falsed
            if(contact.getFixtureA().getBody().getUserData() == "ground"  && contact.getFixtureB().getUserData() == "groundChecker" && contact.getFixtureB().getBody().getLinearVelocity().y > 0){
                contact.setEnabled(false);
            }
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {

        }
    });

We have to disable contact when our "player" fixture collide with "ground" fixture. 当我们的“玩家”装置与“地面”装置发生碰撞时,我们必须禁用接触。 Like this. 像这样。

if(contact.getFixtureA().getBody().getUserData() == "ground" && contact.getFixtureB().getUserData() == "player"){
                contact.setEnabled(false);
}

And we need to disable contact when our "groundChecker" will collide with "ground" and we need to check what velocity.y of player body is, when it is bigger than 0, contact should be falsed. 而且,当我们的“ groundChecker”与“ ground”碰撞时,我们需要禁用接触,并且我们需要检查玩家身体的速度是多少,当它大于0时,应该忽略接触。

if(contact.getFixtureA().getBody().getUserData() == "ground"  && contact.getFixtureB().getUserData() == "groundChecker" && contact.getFixtureB().getBody().getLinearVelocity().y > 0){
                contact.setEnabled(false);
}

Here is a demo of the above solution: 这是上述解决方案的演示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM