精灵图像在与Libgdx中的静态地面体碰撞后离开其动态体

时间:2016-09-02 08:12:46

标签: android css libgdx box2d

我在游戏中有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 method

中处理联系人的代码
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);

                }

            }

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

1 个答案:

答案 0 :(得分:0)

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

body.setFixedRotation=true;