2016-03-31 6 views
0

Ich schrieb eine einfache Test-Android-App mit Push-Bälle. Wenn ich mehr als 20-30 Bälle auf dem Gerät drücken, stürzt die App mit FehlernLibGDX Box2d Android Absturz "Fatal Signal 11 (SIGSEGV), Code 1, Fehler addr ..."

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) 

Auf dem Emulator diese Fehler mit 5-10 Kugeln auftreten.

Ich begann mit LibGDX Box2D Physik zu lernen. Ich habe gelesen, dass Box2D Tausende von Körpern verarbeiten kann.

Also, ich denke, dass ich etwas in Box2D nicht verstehe. Kann mir bitte jemand etwas oder einen Rat oder ein Beispiel von Programmen mit vielen interaktiven Körpern geben?

Der Code des Libgdx Kernprojekt:

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; 
} 

Ich hoffe, dass Sie mir helfen können!

Antwort

0

Es scheint, als ob Sie ein Speicherproblem haben. Wie es beim Drücken von Ball der Fall ist, sollten Sie zuerst die PushBall-Methode betrachten, um zu untersuchen. Ich vermute volatile boolean stopWorld.

Sie wissen nicht, wo Sie Pushball-Methode aufrufen, aber sollten Sie stopWorld überprüfen, bevor es auf true setzen, wenn Sie stopWorld sicher sein wollen, wird nicht verändert, bis Codeblock durch einen anderen Anruf zu Pushball verarbeitet wird:

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; 
} 

Wenn dies nicht mit dem Problem hilft, versuchen Sie, volatile aus StopWorld Felddefinition zu entfernen.

volatile boolean stopWorld-boolean stopWorld

+0

sind Sie richtig. Das Problem tritt beim Drücken des neuen Balls auf. Der Fehler ist mit der Synchronisation des Weltobjekts verbunden. Ich habe den Code wie folgt geändert (http://pastebin.com/PSNpbmk9). Vielen Dank! –

+0

Schön zu sehen, dass du dein Problem gelöst hast :) –

Verwandte Themen