Libgdx box2d动态车身不会在重力作用下坠落

Libgdx box2d动态车身不会在重力作用下坠落,libgdx,box2d,Libgdx,Box2d,我试图制造一个动态的物体,它应该向前移动一点,然后根据它的重力下降(由box2d引擎决定)。问题是,移动到一定距离后,它不是向下移动,而是向上移动。这是我正在使用的代码: public class AngryBirdsTrajectoryPrototype implements ApplicationListener { private SpriteBatch spriteBatch; private Texture backgroundTexture;

我试图制造一个动态的物体,它应该向前移动一点,然后根据它的重力下降(由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);
        }
    }
公共类AngryBirdStratectoryProtoType实现ApplicationListener
{
私人SpriteBatch SpriteBatch;
私有纹理背景纹理;
私人雪碧背景雪碧;
私人静态雪碧球在手中;
私人纹理球\u在手\u纹理;
私有静态com.badlogic.gdx.physics.box2d.Body b2Body;
私有com.badlogic.gdx.physics.box2d.World box2Dworld;
私人布尔值是投球的;
@凌驾
公共void create(){
spriteBatch=新spriteBatch();
纹理.setEnforcePotImages(false);
backgroundTexture=新纹理(Gdx.files.internal(“angrybirds/background.png”);
backgroundTexture.setFilter(TextureFilter.Linear,TextureFilter.Linear);
背景精灵=新精灵(背景纹理);
背景sprite.setPosition(0,0);
ball_in_hand_Texture=新纹理(Gdx.files.internal(“test/ball_in_hand.png”);
球在手上纹理设置过滤器(TextureFilter.Linear,TextureFilter.Linear);
球在手=新精灵(球在手纹理);
this.box2Dworld=new com.badlogic.gdx.physics.box2d.World(newvector2(0.0F,10.0F),true);
}
@凌驾
公共无效呈现(){
Gdx.gl.glClearColor(0.5f,0.5f,0.5f,1f);
Gdx.gl.glClear(GL10.gl\u颜色\u缓冲\u位);
如果(!isBallShooted)
{
isBallShooted=正确;
拍摄(这个世界,27.0华氏度,0.0华氏度);
}
Box2World.台阶(1.0f/60.0f,6,2);
spriteBatch.begin();
背景雪碧画(雪碧画);
数组体=新数组();
box2Dworld.getbody(body);
用于(主体:主体)
{
球在手上。设置位置(30.0F*body.getPosition().x,30.0F*body.getPosition().y);
Gdx.app.error(“,”angleDegree=“+body.getAngle());
}
手握球。抽签(spriteBatch);
spriteBatch.end();
}
@凌驾
公共空间处置(){
spriteBatch.dispose();
backgroundTexture.dispose();
}
公共静态无效快照(com.badlogic.gdx.physics.box2d.World var1、float var2、float var3)
{
BodyDef var4=新的BodyDef();
var4.position.set(ball\u in_hand.getX()/30.0F,ball\u in_hand.getY()/30.0F);
var4.type=BodyType.DynamicBody;
var4.bullet=true;
var4.angularDamping=0.5F;
b2Body=var1.createBody(var4);
CircleShape var6=新的CircleShape();
变量6.设定半径(0.4F);
FixtureDef var7=新的FixtureDef();
var7.密度=0.8F;
var7.shape=var6;
var7.restoration=0.7F;
var7.摩擦力=1.0F;
b2Body.createFixture(var7);
浮动var9=var2*9.0F;
vector2var10=newvector2(var9*(float)Math.cos((double)var3),var9*(float)Math.sin((double)var3));
b2Body.applyForce(var10,b2Body.getWorldCenter(),true);
}
}
这是我得到的输出

这是我想要实现的输出:

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

试试这个:

  • 由于Box2d坐标系,应将重力设置为负数,如下所示:
  • 你应该为你的物理世界创建一个相机,并将你所有的尺寸转换成米。例如,您可以在box2d世界中设置100像素=1米,因此您还需要设置相机的宽度和高度(以米为单位)。这是一本书
  • 您也可以这样获得增量:
    Gdx.graphics.getDeltaTime()


    如果有帮助,请告诉我。

    我认为代码中的问题在于创建方法:

    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)
    

    另外,请使用
    调试渲染器
    来发现错误。

    您的解决方案不起作用。即使进行了您建议的更改,我也会得到相同的结果。您是否使用Box2DebugRenderer进行了检查?您可以这样创建:box2debugrenderer boxDebugRender=new-box2debugrenderer();然后在render()->boxDebugRender.render(world,camera.combined)中;这样你会看到真实的物理形状,也许它们会移动,但图像不会。我不认为按增量设置世界是一个好主意,我以前试过,游戏在低fps下有很多不同,身体会跳得更高。我认为必须有一个固定的值,比如1/60fHm,也许你是对的,在速度慢的设备上可能会有问题,我不支持游戏中的旧设备,所以忘了它,我编辑了答案。
    this.box2Dworld = new com.badlogic.gdx.physics.box2d.World(new Vector2(0.0F, -10.0F), true)