簡體   English   中英

Libgdx Box2d跳過

[英]Libgdx Box2d Jumping through

我想像在《馬里奧》游戲中那樣跳躍。 當您在平台下並跳下時,您可以通過對撞機。

當玩家的速度下降時,對撞機應醒來。 我知道我應該使用ContactListener ,但是當我使用contact.setEnable(false)方法時,什么也沒有發生。

我的聯系偵聽器(地面檢查器工作正常)

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) {

        }
    });

我應該在什么地方取得這種效果。

在此處輸入圖片說明

在此處輸入圖片說明

對撞機應該只有一側,有人來應對嗎?

找到了解決方案,希望對您有所幫助。

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) {

        }
    });

當我們的“玩家”裝置與“地面”裝置發生碰撞時,我們必須禁用接觸。 像這樣。

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

而且,當我們的“ groundChecker”與“ ground”碰撞時,我們需要禁用接觸,並且我們需要檢查玩家身體的速度是多少,當它大於0時,應該忽略接觸。

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

這是上述解決方案的演示:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM