【问题标题】:box2d dynamic body is not falling under gravitybox2d 动态体不会在重力作用下下落
【发布时间】:2014-06-08 09:33:13
【问题描述】:

我正在尝试制作一个动态的物体,它应该向前移动一点,然后根据它的重力下降(由 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);
        }
    }

这是我得到的输出。

这是我想要实现的输出:

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

【问题讨论】:

    标签: libgdx box2d


    【解决方案1】:

    试试这个:

    1. 由于 Box2d 坐标系,您应该将重力设置为负数,如下所示:
    enter code here`this.world = new World(new Vector2(0, -9.8f), true);
    
    1. 您应该为您的物理世界创建一个相机并将所有尺寸转换为米。例如,您可以在 box2d 世界中设置 100 像素 = 1 米,因此您还需要以米为单位设置相机的宽度和高度。这是full working example

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

    如果有帮助请告诉我。

    【讨论】:

    • 您的解决方案不起作用先生。即使进行了您推荐的更改,我也得到了相同的结果。
    • 你用 Box2DDebugRenderer 检查了吗?你可以这样创建: Box2DDebugRenderer boxDebugRender = new Box2DDebugRenderer();然后在你的 render()-> boxDebugRender.render(world, camera.combined);这样您将看到真实的物理形状,也许它们会移动,但图像不会。
    • 我不认为按增量设置世界是一个好主意,我之前尝试过,并且在低fps下游戏玩法有很大不同,身体往往会跳得更高..我认为必须有一个固定值,例如 1/60f
    • 嗯,也许你是对的,这可能是慢设备上的问题,我的游戏不支持旧设备,所以忘记了,我编辑了答案。
    【解决方案2】:

    我认为您代码中的问题出在 create 方法中:

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

    您应该在 y 方向使用负重力值,例如:

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

    另外,请使用debug-renderer 找出错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多