Search code examples
androidlibgdxbox2d

box2dlights not works in libgdx


I use https://github.com/libgdx/box2dlights in my game, But its not working, I get the following error in eclipse

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NoSuchMethodError: com.badlogic.gdx.graphics.glutils.FrameBuffer.getColorBufferTexture()Lcom/badlogic/gdx/graphics/GLTexture; at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120) Caused by: java.lang.NoSuchMethodError: com.badlogic.gdx.graphics.glutils.FrameBuffer.getColorBufferTexture()Lcom/badlogic/gdx/graphics/GLTexture; at box2dLight.LightMap.render(LightMap.java:41) at box2dLight.RayHandler.render(RayHandler.java:338) at box2dLight.RayHandler.updateAndRender(RayHandler.java:269) at com.mygdx.game.box2dTest.render(box2dTest.java:182) at com.badlogic.gdx.Game.render(Game.java:46) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)


my java file is:

package com.mygdx.game;

import box2dLight.PointLight;
import box2dLight.RayHandler;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

public class box2dTest implements Screen, InputProcessor {

MyGdxGame game;

OrthographicCamera camera;
World world;
Box2DDebugRenderer renderer;
float width,height;
FPSLogger logger;

Body circleBody;

RayHandler handler;

private TmxMapLoader maploader;
TiledMap map;
OrthogonalTiledMapRenderer maprenderer;


private Viewport gamePort;



public box2dTest(MyGdxGame game)
{
    this.game=game;
    width = Gdx.graphics.getWidth();
    height = Gdx.graphics.getHeight();

    //gamePort = new StretchViewport(MyGdxGame.V_WIDTH,MyGdxGame.V_HEIGHT,camera);

    camera = new OrthographicCamera(600,440);
    camera.position.set(0,220,0);
    camera.update();

    world = new World(new Vector2(0,-49.8f),false);

    renderer = new Box2DDebugRenderer();

    logger = new FPSLogger();

    BodyDef circleDef = new BodyDef();
    circleDef.type = BodyType.DynamicBody;
    circleDef.position.set(width/2f,height/2f);

    circleBody = world.createBody(circleDef);

    CircleShape circleShape = new CircleShape();
    circleShape.setRadius(13f);

    FixtureDef circleFixture = new FixtureDef();
    circleFixture.shape = circleShape;
    circleFixture.density = 0.3f;
    circleFixture.friction = 0.2f;
    circleFixture.restitution = 0.0f;

    circleBody.createFixture(circleFixture);

    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.position.set(0,3);

    Body groundBody = world.createBody(groundBodyDef);


    PolygonShape groundBox = new PolygonShape();
    groundBox.setAsBox((camera.viewportWidth)*2, 3.0f);

    groundBody.createFixture(groundBox ,0.03f);


    maploader = new TmxMapLoader();
    map = maploader.load("f0c.tmx");
    maprenderer = new OrthogonalTiledMapRenderer(map);

    //-------grounds--------------------------------------------------------------

    BodyDef bDef=new BodyDef();
    Body body;
    PolygonShape shape=new PolygonShape();
    FixtureDef fDef = new FixtureDef();
    for(MapObject object : map.getLayers().get(1).getObjects().getByType(RectangleMapObject.class))
    {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();
        bDef.type = BodyDef.BodyType.StaticBody;
        bDef.position.set(rect.getX() + rect.getWidth()/2,rect.getY() + rect.getHeight()/2);

        body = world.createBody(bDef);
        shape.setAsBox(rect.getWidth()/2,rect.getHeight()/2);
        fDef.shape=shape;
        body.createFixture(fDef);

    }

    //---------------------------END OF GROUNDS-----------------------------

    //--------BOX2D LIGHTS-----//
    handler = new RayHandler(world);
    handler.setCombinedMatrix(camera.combined);
    new PointLight(handler, 5000, Color.CYAN, 100,( width/2)-50, (height/2)+15);

}

public void update(float dt)
{
    HandleInput(dt);
    camera.position.x = circleBody.getPosition().x;
    camera.position.y = circleBody.getPosition().y;
    camera.update();
    maprenderer.setView(camera);


}
public void HandleInput(float dt)
{

    if(Gdx.input.isKeyJustPressed(Input.Keys.SPACE) && circleBody.getLinearVelocity().y<10 && circleBody.getLinearVelocity().y>-14)
    {

    circleBody.applyLinearImpulse(new Vector2(0,19009),circleBody.getWorldCenter(), true);

    }
    if(Gdx.input.isKeyPressed(Input.Keys.A) && circleBody.getLinearVelocity().x<=122 || (Gdx.input.isTouched(0) && (Gdx.input.getX()>0 && Gdx.input.getX()<250)))
    {

    circleBody.applyLinearImpulse(new Vector2(-190,0),circleBody.getWorldCenter(), true);

    }
    if(Gdx.input.isKeyPressed(Input.Keys.D) && circleBody.getLinearVelocity().x>=-122 || (Gdx.input.isTouched(0) && (Gdx.input.getX()>550 && Gdx.input.getX()<840)))
    {

    circleBody.applyLinearImpulse(new Vector2(190,0),circleBody.getWorldCenter(), true);

    }
}

@Override
public void render(float delta) {
    Gdx.input.setInputProcessor(this);

    update(delta);
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    maprenderer.render();

    renderer.render(world, camera.combined);

    //box2dlights
    handler.updateAndRender();
    //end

    world.step(1/60f, 6, 2);

    logger.log();


}

@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub

}

@Override
public void show() {
    // TODO Auto-generated method stub

}

@Override
public void hide() {
    // TODO Auto-generated method stub

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    world.dispose();

}

@Override
public boolean keyDown(int keycode) {

    return false;
}

@Override
public boolean keyUp(int keycode) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean keyTyped(char character) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean touchDown(int X, int Y, int pointer, int button) {
    if(X>250 && X<550 && circleBody.getLinearVelocity().y<10 && circleBody.getLinearVelocity().y>-14)
    {
        circleBody.applyLinearImpulse(new Vector2(0,19009),circleBody.getWorldCenter(), true);
    }
    return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {

    return false;
}

@Override
public boolean scrolled(int amount) {
    // TODO Auto-generated method stub
    return false;
}

}

Solution

  • I believe you have an old version of libGDX. Change gdxVersion field to gdxVersion = "1.7.0"in build.gradle in the root folder of your project.