【问题标题】:LibGDX Box2d Android crash "Fatal signal 11 (SIGSEGV), code 1, fault addr..."LibGDX Box2d Android 崩溃“致命信号 11 (SIGSEGV),代码 1,故障地址...”
【发布时间】:2016-04-01 14:17:18
【问题描述】:

我编写了一个带有推球的简单测试 android 应用程序。 当我在我的设备上推动超过 20-30 个球时,应用程序崩溃并出现错误

03-31 17:07:08.414 6778-6778/com.example.balls A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x58 in tid 6778 (bm.examle.balls)

在模拟器上,这个错误发生在 5-10 个球上。

我开始使用 LibGDX 学习 Box2D 物理。 我读到 Box2D 可以处理数千个身体。

所以,我认为我不了解使用 Box2D 的某些内容。谁能给我一些或建议或具有许多互动机构的程序示例?

libgdx核心项目的代码:

public class BallGame extends ApplicationAdapter {

BallParameters ballParameters;

volatile boolean stopWorld = false;

//parameters from Android Application
public BallGame(BallParameters ballParameters){
    this.ballParameters = ballParameters;
}

World world;
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;

//factor to adjust the size
private final float n = 0.05f;

@Override
public void create () {
    //create camera
    camera = new OrthographicCamera(Gdx.graphics.getWidth()*n, Gdx.graphics.getHeight()*n);
    camera.position.set(new Vector3(Gdx.graphics.getWidth()/2f*n, Gdx.graphics.getHeight()/2f*n, 10f));
    camera.near = 1f;
    camera.far = 20f;
    camera.update();
    //create world
    world = new World(new Vector2(0, -50), true);
    debugRenderer = new Box2DDebugRenderer();
    //It creates borders around the edges of the screen
    createGround();
    createLeftWall();
    createRightWall();
    createCelling();
}

@Override
public void render () {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    debugRenderer.render(world, camera.combined);
    doPhysicsStep(Gdx.graphics.getDeltaTime());
}

//physics step, (from libgdx documentation https://github.com/libgdx/libgdx/wiki/Box2d#stepping-the-simulation)
private float accumulator = 0;
private float TIME_STEP = 1/60f;
private void doPhysicsStep(float deltaTime) {
    if(stopWorld) return;
    float frameTime = Math.min(deltaTime, 0.25f);
    accumulator += frameTime;
    while (accumulator >= TIME_STEP) {
        world.step(TIME_STEP, 6, 2);
        accumulator -= TIME_STEP;
    }
}

//creates a ball and applies linear impulse with parameters from android application
//blocks stepping the world during creating ball
//calls from android application
public void pushBall(){
    stopWorld = true;
    Body ball = createBall(40*n, 40*n);
    int velocity = ballParameters.getVelocity();
    int angleDeg = ballParameters.getAngle();
    int vx = (int) (velocity*Math.cos(Math.toRadians(angleDeg)));
    int vy = (int) (velocity*Math.sin(Math.toRadians(angleDeg)));
    ball.applyLinearImpulse(vx, vy, ball.getPosition().x, ball.getPosition().y, true);
    stopWorld = false;
}

//creates a ball in a fixed place
private Body createBall(float x, float y){
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(x, y);
    Body body = world.createBody(bodyDef);
    CircleShape circle = new CircleShape();
    circle.setRadius(50*n);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = 0f;
    fixtureDef.friction = 0f;
    fixtureDef.restitution = 1f;
    body.createFixture(fixtureDef);
    circle.dispose();
    return body;
}

希望你能帮帮我!

【问题讨论】:

    标签: android libgdx box2d


    【解决方案1】:

    您似乎遇到了内存问题。正如推球时发生的那样,您应该首先查看 pushBall 方法进行调查。我怀疑volatile boolean stopWorld

    不知道你在哪里调用 pushBall 方法,但是你应该在设置为 true 之前检查 stopWorld,如果你想确保 stopWorld 在代码之前不会被修改块被另一个 pushBall 调用处理:

    public void pushBall() {
        if(stopWorld) {
            return;
        }
    
        stopWorld = true;
        Body ball = createBall(40*n, 40*n);
        int velocity = ballParameters.getVelocity();
        int angleDeg = ballParameters.getAngle();
        int vx = (int) (velocity*Math.cos(Math.toRadians(angleDeg)));
        int vy = (int) (velocity*Math.sin(Math.toRadians(angleDeg)));
        ball.applyLinearImpulse(vx, vy, ball.getPosition().x, ball.getPosition().y, true);
        stopWorld = false;
    }
    

    如果这对解决问题没有帮助,请尝试从 stopWorld 字段定义中删除 volatile。

    volatile boolean stopWorldboolean stopWorld

    【讨论】:

    • 你是对的。推新球时出现问题。该错误与世界对象的同步有关。我已经编辑了this 之类的代码。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2019-03-05
    • 2017-01-05
    • 2017-11-05
    • 2018-03-29
    相关资源
    最近更新 更多