Sprite 图像在 Libgdx 中与静态地面 body 碰撞后离开其动态 body

Sprite image leaves its dynamic body after collide with static ground body in Libgdx

我的游戏中有 3 个物体,一个动态的球 body 从上面掉落,一个静态的地面和一个动态的 body 在地面上,当球直接与地面或运动碰撞时 body 在重力下很好,但是当球首先与地面上的动态 body 碰撞然后弹回地面时,一件奇怪的事情发生了,精灵离开 body 并飞出屏幕。 我使用 Box2DDebugRenderer 来解决这个问题。我的地面代码在这里 如果(地面!=空)world.destroyBody(地面);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;

    FixtureDef fixtureDef = new FixtureDef();

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(camera.viewportWidth, 5);

    fixtureDef.shape = shape;
    fixtureDef.friction=0.2f;
    fixtureDef.filter.categoryBits = WORLD_ENTITY;
    fixtureDef.filter.maskBits = PHYSICS_ENTITY;

    ground = world.createBody(bodyDef);
    ground.createFixture(fixtureDef);
    ground.setTransform(0, 0, 0);

    shape.dispose();'

我的坠落密码body

  public Body dropBall() {
        Body body;
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(MathUtils.random(0,50),50);
        body = world.createBody(bodyDef);
        ballBodies.add(body);
        CircleShape circleShape = new CircleShape();
        circleShape.setPosition(new Vector2(0, 0));
        circleShape.setRadius(25*SCALE);
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = circleShape;
        fixtureDef.density = 0.1f;
        fixtureDef.restitution = 0.7f;
        fixtureDef.filter.categoryBits = PHYSICS_ENTITY;
        fixtureDef.filter.maskBits = WORLD_ENTITY;
        body.createFixture(fixtureDef);
              circleShape.dispose();


        lastDropTime = TimeUtils.millis();
        return body;


    }

我在地面上 body 的代码

 private Body createBody(String name, float x, float y, float rotation) {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        Body body = playerPhysics.createBody(name, world, bodyDef, SCALE, SCALE);

        body.setTransform(x, y, rotation);
        body.setLinearDamping(50f);
        body.setGravityScale(0);
        body.setFixedRotation(true);


        return body;
    }

我的渲染方法

  @Override
    public void render() {
        Gdx.gl.glClearColor(0.57f, 0.77f, 0.85f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stepWorld();
        viewport.apply();
        elapsedTime+=Gdx.graphics.getDeltaTime();


         position =playerBody.getPosition();

         float width =player.getRegionWidth()*SCALE;
         float height=player.getRegionHeight()*SCALE;

        batch.begin();
            if (Gdx.input.isKeyPressed(Input.Keys.SPACE))
                 batch.draw(headerAnimation.getKeyFrame(elapsedTime,true),position.x,position.y,width,height);
              else  drawSprite("2",position.x,position.y);

        for (Body body : ballBodies) {
            body.applyTorque(torque, false);
            ball.setRotation((float) Math.toDegrees(body.getAngle()));
            batch.draw(ball, body.getPosition().x-ball.getWidth()/2,body.getPosition().y-ball.getWidth()/2, ball.getOriginX()
                    , ball.getOriginY(), ball.getWidth(), ball.getHeight(), ball.getScaleX(), ball.
                            getScaleY(), ball.getRotation());
        }

        if (TimeUtils.millis() - lastDropTime > 3000) dropBall();

        Iterator<Body> iterator = ballBodies.iterator();
        while (iterator.hasNext()) {
            Body body = iterator.next();
            if (body.getPosition().y<=7&&body.getLinearVelocity().len()<0.4f&&
                    body.getAngularVelocity()<0.4f) {

                world.destroyBody(body);
                iterator.remove();
            }
        }

         batch.end();

        debugRenderer.render(world, camera.combined);
    }

创建方法中处理联系人的代码

public void beginContact(Contact contact) {
                if((contact.getFixtureA().getBody() == ballBody) &&
                        (contact.getFixtureB().getBody()==playerBody)
                        || (contact.getFixtureA().getBody() == playerBody
                        && contact.getFixtureB().getBody()==ballBody)) {
                    ballBody.applyForceToCenter(20f,20f,true);

                }
                if((contact.getFixtureA().getBody() == ballBody) &&
                        (contact.getFixtureB().getBody()==ground)
                        || (contact.getFixtureA().getBody() == ground
                        && contact.getFixtureB().getBody()==ballBody)) {
                    ballBody.applyForceToCenter(0,50f,true);

                }

            }

第二次碰撞即地面有问题。请告诉我解决这个问题?

兄弟,把这行代码添加到你的每一个身体上可能会有帮助

body.setFixedRotation=true;