box2d动态体在重力作用下不会坠落

时间:2014-06-08 09:33:13

标签: libgdx box2d

我正在努力创造一个动态的身体,它应该向前移动一点,然后根据其重力下降(由box2d引擎决定)。问题是,在移动到一定距离后,而不是向下移动,它正在上升。这是我正在使用的代码:

 public class AngryBirdsTrajectoryPrototype implements ApplicationListener
    {
        private SpriteBatch spriteBatch;
        private Texture backgroundTexture;
        private Sprite backgroundSprite;
        private static Sprite ball_in_hand;
        private Texture ball_in_hand_Texture;
        private static com.badlogic.gdx.physics.box2d.Body b2Body;
        private com.badlogic.gdx.physics.box2d.World box2Dworld;
        private boolean isBallShooted;

        @Override
        public void create() {
            spriteBatch = new SpriteBatch();

            Texture.setEnforcePotImages(false);

            backgroundTexture = new Texture(Gdx.files.internal("angrybirds/background.png"));
            backgroundTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

            backgroundSprite = new Sprite(backgroundTexture);
            backgroundSprite.setPosition(0, 0);

            ball_in_hand_Texture = new Texture(Gdx.files.internal("test/ball_in_hand.png"));
            ball_in_hand_Texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

            ball_in_hand = new Sprite(ball_in_hand_Texture);

            this.box2Dworld = new com.badlogic.gdx.physics.box2d.World(new Vector2(0.0F, 10.0F), true);
        }

        @Override
        public void render() {
            Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1f);
            Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

            if(!isBallShooted)
            {
                isBallShooted = true;
                shoot(this.box2Dworld, 27.0F, 0.F);
            }

            box2Dworld.step(1.0f/60.0f, 6, 2);

            spriteBatch.begin();
            backgroundSprite.draw(spriteBatch);

            Array<Body> bodies = new Array<Body>();

            box2Dworld.getBodies(bodies);

            for (Body body : bodies) 
            {
                ball_in_hand.setPosition(30.0F * body.getPosition().x, 30.0F * body.getPosition().y);

                Gdx.app.error("", "angleDegree= " + body.getAngle());
            }

            ball_in_hand.draw(spriteBatch);
            spriteBatch.end();
        }

        @Override
        public void dispose() {
            spriteBatch.dispose();
            backgroundTexture.dispose();
        }


        public static void shoot(com.badlogic.gdx.physics.box2d.World var1, float var2, float var3) 
        {
            BodyDef var4 = new BodyDef();
            var4.position.set(ball_in_hand.getX() / 30.0F, ball_in_hand.getY() / 30.0F);
            var4.type = BodyType.DynamicBody;
            var4.bullet = true;
            var4.angularDamping = 0.5F;
            b2Body = var1.createBody(var4);
            CircleShape var6 = new CircleShape();
            var6.setRadius(0.4F);
            FixtureDef var7 = new FixtureDef();
            var7.density = 0.8F;
            var7.shape = var6;
            var7.restitution = 0.7F;
            var7.friction = 1.0F;
            b2Body.createFixture(var7);
            float var9 = var2 * 9.0F;
            Vector2 var10 = new Vector2(var9 * (float)Math.cos((double)var3), var9 * (float)Math.sin((double)var3));
            b2Body.applyForce(var10, b2Body.getWorldCenter(), true);
        }
    }

这是我得到的输出。

enter image description here

这是我想要实现的输出:

enter image description here

我已经尝试了所有可能的选项,但没有一个可行。请帮我。提前谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

  1. 由于Box2d坐标系,您应该将重力设置为负值,如下所示:

        this.world = new World(new Vector2(0, -9.8f), true);
    
  2. 此外,您应该为您的物理世界创建相机并将所有尺寸转换为米,例如您可以在box2d世界中设置100pixels = 1米,因此您需要设置相机的宽度以米为单位的高度。以下是一个完整的工作示例:http://agmprojects.com/blog/setting-up-box2d-with-libgdx

  3. 你也可以像这样获得delta:Gdx.graphics.getDeltaTime()

    如果有帮助,请告诉我。

答案 1 :(得分:0)

我认为问题在于你在create方法中的代码 'this.box2Dworld = new com.badlogic.gdx.physics.box2d.World(new Vector2(0.0F,10.0F),true'

你应该在y方向使用-ve的重力值,比如  this.box2Dworld = new com.badlogic.gdx.physics.box2d.World(new Vector2(0.0F,-10.0F),true) 请使用debugrenderer发现错误