运动体 LIBGDX 中的碰撞检测

问题描述 投票:0回答:2

我设法在 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。

enter image description here

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

java box2d collision-detection libgdx
2个回答
7
投票

运动体不与静止体碰撞。 也许这会对你有所帮助。


1
投票

运动体仅受速度影响,不受力影响 静态物体具有 O 位移,但受到力的影响,即它们在碰撞时产生冲量,而动态物体两者都有

© www.soinside.com 2019 - 2024. All rights reserved.