1

I am developing a game in and engine. Its crashing with very annoying error message. Crash is occurs when transform body to change the position. I am using body.setTransform method to change the position. My code is following.

    public Scene onCreateScene() {

            this.mScene = new Scene();
            this.mScene.setBackground(new Background(0, 0, 0));
            this.mScene.setOnSceneTouchListener(this);
            this.mScene.setOnAreaTouchListener(this);
            this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, 10), false);

            // Some Code.

            faceBox = new Sprite(bx, CAMERA_HEIGHT-mPlayerTextureRegion.getHeight()-25, this.mPlayerTextureRegion, this.getVertexBufferObjectManager());
            body = PhysicsFactory.createCircleBody(mPhysicsWorld, faceBox.getRotationCenterX(), faceBox.getRotationCenterY(), faceBox.getWidthScaled() * 0.6f, BodyType.DynamicBody, FIXTURE_DEF);//(this.mPhysicsWorld, faceBox, BodyType.DynamicBody, FIXTURE_DEF);
            body.setFixedRotation(true);
            body.setUserData("player");

            this.mScene.attachChild(faceBox);
            this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(faceBox, body, true, true));

            // Some Code.

            this.mEngine.registerUpdateHandler(new FPSLogger(){
            @Override public void onUpdate(float pSecondsElapsed) {
                try{
                        if(ball.getPosition().x<=n.getPosition().x&&ball.getPosition().y<body.getPosition().y-1.3f&&difficulty){
                            body.setTransform(ball.getPosition().x-0.6f<(l.getPosition().x+2*(l.getPosition().x-l.getWorldCenter().x)+0.83f)?(l.getPosition().x+2*(l.getPosition().x-l.getWorldCenter().x)+0.83f):ball.getPosition().x-0.6f,body.getPosition().y,body.getAngle());
                        }   
                }catch(Exception e){
                        e.printStackTrace();
                    }

            // Some Code.

            });
            }

        return this.mScene;
}

Error log from logcat is.

 03-03 13:02:16.374: A/libc(5892): /Users/ngramlich/Workspace/gdk/graphic_engines/AndEngine/AndEnginePhysicsBox2DExtension/jni/Box2D/Dynamics/b2Body.cpp:395: void b2Body::SetTransform(const b2Vec2&, float32): assertion "m_world->IsLocked() == false" failed
 03-03 13:02:16.375: A/libc(5892): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1), thread 5936 (Thread-474)
Navdeep Soni
  • 449
  • 4
  • 15

1 Answers1

0

It crashes when your PhysicsWorld is locked. You should check whether is locked or not with isLocked method. Your code should be use this.

if(!mPhysicsWorld.isLocked())
    body.setTransform(ball.getPosition().x-0.6f<(l.getPosition().x+2*(l.getPosition().x-l.getWorldCenter().x)+0.83f)?(l.getPosition().x+2*(l.getPosition().x-l.getWorldCenter().x)+0.83f):ball.getPosition().x-0.6f,body.getPosition().y,body.getAngle());
Raj Joshi
  • 2,669
  • 2
  • 30
  • 37