【问题标题】:Sprite image leaves its dynamic body after collide with static ground body in LibgdxSprite 图像在与 Libgdx 中的静态地面体碰撞后离开其动态体
【发布时间】:2016-09-02 08:12:46
【问题描述】:

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

    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();'

我的坠落体代码

  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;


    }

我的地面尸体代码

 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);
    }

create 方法中处理联系人的代码

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

                }

            }

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

【问题讨论】:

  • 你为什么使用ballBody.applyForceToCenter(20f,20f,true)?评论/删除整个beginContact() 方法。球体的恢复应该处理弹跳。
  • 感谢您的回复,我已经尝试过并删除了所有联系人监听器部分,问题是一样的,第二次接触后精灵正在分离身体

标签: android css libgdx box2d


【解决方案1】:

兄弟,只需将这行代码添加到您的每个身体中可能会有所帮助

body.setFixedRotation=true;

【讨论】:

  • 谢谢,它对我有用,但不知道它是如何解决问题的任何解释?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 2013-03-22
  • 2020-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多