我设法在 libgdx 中使用 box2d。然而,here 给出的示例代码仅适用于动态体。我尝试使用它并且效果很好,但是当我将 Dynamic 更改为 KinematicBody 时,代码不起作用。这是我的代码

    @Override
        public void create() {
      // Create our body definition
            BodyDef groundBodyDef =new BodyDef();
            groundBodyDef.type = BodyDef.BodyType.StaticBody;
    // Set its world position
            groundBodyDef.position.set(new Vector2(0, 10));

    // Create a body from the defintion and add it to the world
             groundBody = world.createBody(groundBodyDef);

    // Create a polygon shape
            PolygonShape groundBox = new PolygonShape();
    // Set the polygon shape as a box which is twice the size of our view port and 20 high
    // (setAsBox takes half-width and half-height as arguments)
            groundBox.setAsBox(camera.viewportWidth, 10.0f);
    // Create a fixture from our polygon shape and add it to our ground body
            groundBody.createFixture(groundBox, 0.0f);
    // Clean up after ourselves
            groundBox.dispose();

            //endregion


            BodyDef bodyDef = new BodyDef();
    // We set our body to dynamic, for something like ground which doesnt move we would set it to StaticBody
            bodyDef.type = BodyDef.BodyType.KinematicBody;
    // Set our body's starting position in the world
            bodyDef.position.set(bolaX, bolaY);

    // Create our body in the world using our body definition
             body = world.createBody(bodyDef);

    // Create a circle shape and set its radius to 6
            CircleShape circle = new CircleShape();
            circle.setRadius(20f);


    // Create a fixture definition to apply our shape to
            FixtureDef fixtureDef = new FixtureDef();
            fixtureDef.shape = circle;
            fixtureDef.density = 20;

            fixtureDef.friction = 0;
            fixtureDef.restitution = 0.6f; // Make it bounce a little bit

    // Create our fixture and attach it to the body
            Fixture fixture = body.createFixture(fixtureDef);

    // Remember to dispose of any shapes after you're done with them!
    // BodyDef and FixtureDef don't need disposing, but shapes do.
            circle.dispose();

            Gdx.input.setInputProcessor(this);
    }



     @Override
        public void render() {


            world.step(1/60f, 6, 2);



            Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            camera.update();



            //batch.getProjectionMatrix().set(camera.combined);
            batch.begin();
            textureBodies = body.getPosition();
            float angle = MathUtils.radiansToDegrees * body.getAngle();
            //batch.draw(ball,textureBodies.x ,textureBodies.y  );
            batch.draw(ball, textureBodies.x - (bola.getWidth() / 2) , textureBodies.y - (bola.getHeight()/2) , // the bottom left corner of the box, unrotated
                    1f, 1f, // the rotation center relative to the bottom left corner of the box
                    bola.getWidth(), bola.getHeight(), // the width and height of the box
                    1, 1, // the scale on the x- and y-axis
                    angle);
            batch.end();

            debugRenderer.render(world, camera.combined);
                if(Gdx.input.isKeyPressed(Input.Keys.DPAD_UP)){
                    Gdx.app.log("Input Test", "key down: " + "aw ---- x" +  body.getPosition().x + "  y   " + body.getPosition().y);
                    vel += 1;
                    //Gdx.app.log("vel","" + vel);
                    body.setLinearVelocity(0f,vel);
                } else{
                    vel -= 1;
                   // Gdx.app.log("vel","" + body.getPosition().y);
                    body.setLinearVelocity(0f,vel);
                }

  int numContacts = world.getContactCount();
        if (numContacts > 0) {
            Gdx.app.log("contact", "start of contact list");
            for (Contact contact : world.getContactList()) {
                Fixture fixtureA = contact.getFixtureA();
                Fixture fixtureB = contact.getFixtureB();
                Gdx.app.log("contact", "between " + fixtureA.toString() + " and " + fixtureB.toString());
            }
            Gdx.app.log("contact", "end of contact list");
        }


        }

这是图像。您可以看到左侧是 DynamicBody,右侧是 KinematicBody。

获取接触适用于 Dynamic Body,但不适用于 Kinematic Body。你能告诉如何检测运动体中的碰撞吗?

最佳答案

运动体不与静态体碰撞。
可能这会帮助你。

关于java - 运动体中的碰撞检测 LIBGDX,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15736808/

10-10 17:32