java - 对 box2d 主体施加力不起作用

标签 java libgdx box2d

我试图通过对物体施加力来移动它。但它不是在水平方向移动并形成轨迹,而是只是掉落下来,就像我没有对其施加力一样。这是代码:

public class Box2dTest implements ApplicationListener
{
    private OrthographicCamera camera;
    World box2Dworld;
    Box2DDebugRenderer debugRenderer;
    private Body box2dBody;
    Sprite ball_sprite;
    SpriteBatch spriteBatch;

    @Override
    public void create()
    {
        camera = new OrthographicCamera();
        camera.setToOrtho(false);

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

        debugRenderer = new Box2DDebugRenderer();

        createDynamicBody(110f, 150f);

        box2dBody.applyForce(new Vector2(30 * 1000, 0), box2dBody.getWorldCenter(), true);

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

        ball_sprite = new Sprite(ball_in_hand_Texture);
        ball_sprite.setPosition(110f, 150f);

        spriteBatch = new SpriteBatch();
    }

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

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

        camera.update();

        debugRenderer.render(box2Dworld, camera.combined);

        spriteBatch.begin();

        spriteBatch.draw(ball_sprite, 30 * box2dBody.getPosition().x, 30 * box2dBody.getPosition().y);

        spriteBatch.end();
    }

    private void createDynamicBody(float x, float y)
    {
        // First we create a body definition
        BodyDef bodyDef = new BodyDef();
        // We set our body to dynamic, for something like ground which doesn't move we would set it to StaticBody
        bodyDef.type = BodyType.DynamicBody;
        bodyDef.bullet = true;
        // Set our body's starting position in the world
//      bodyDef.position.set(x / 60.0f, y / 60.0f);

        //bodyDef.position.set(x / 30.0f, y / 30.0f);

        bodyDef.position.set(x, y);

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

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

        // Create a fixture definition to apply our shape to
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = circle;
        fixtureDef.density = 0.5f; 
        fixtureDef.friction = 0.4f;
        fixtureDef.restitution = 0.6f; // Make it bounce a little bit

        // Create our fixture and attach it to the body
        Fixture fixture = box2dBody.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();
    }
}

请帮助我。过去三天我一直在尝试解决这个问题,但没有成功。提前致谢。

最佳答案

我认为你创造了一个非常大的世界,在这个世界中施加该值的力量将不会产生任何效果。事实上,施加任何值的力都不会产生任何效果,因为 box2d 世界仅限于较低的值。

我已经很长时间没有在 Box2d 上工作了,但我确实遇到了同样的问题。尝试一个 12x20 左右的小世界并相应地渲染你的 Sprite 。然后尝试用力,一定会有效果的。

关于java - 对 box2d 主体施加力不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138557/

相关文章:

java - 如何在 android 中压缩 Pixmap/使用 cim 文件?

android - 适用于 Android 的最高效的 2D 图形引擎是什么?

java - 获取 ul 中 li 元素的数量,而不存储在列表中

java - libgdx 中的声音问题

java - Libgdx Scene2d 按钮就像 Minecraft 中一样

android - 如何制作将周围物体吹走的爆炸动画?

java - libgdx 和 box2d 中多边形内的多边形

java - 比较两个数组列表以返回最新日期

java - 原始图像大小 VS 显示图像大小

java - 启动时出现 Eclipse 错误 : Error notifying a preference change listener