Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx body position and shaperenderer position not the same

i'm fairly new to libgdx in general. Basically I have the ball bouncing world and i am just experimenting with it. Before I had a fixed camera position as such:

http://prntscr.com/567hvo

After I have putted the following line in the render method so the camera keeps following the player (which is the circle) :

camera.position.set(player.getPosition().x, player.getPosition().y, 0); 

The player is a Body type.

So when I do this it does follow the player, but the shape renderer acts weird now. Look at what happens:

http://prntscr.com/567i9a

Even though I am referring to the same x and y position of the player to the camera and the shape renderer, they are not in the same position.

Here is my code:

public class Game extends ApplicationAdapter {

World world = new World(new Vector2(0, -9.8f), true);  
Box2DDebugRenderer debugRenderer;  
OrthographicCamera camera;  
static final int PPM = 100;
static float height,width;
Body player;
RayHandler handler;
PointLight l1;

ShapeRenderer shapeRenderer;

@Override  
public void create() {     
    shapeRenderer = new ShapeRenderer();

    height = Gdx.graphics.getHeight();
    width = Gdx.graphics.getWidth();

    //set camera
     camera = new OrthographicCamera();  
     camera.setToOrtho(false, (width)/PPM/2, (height)/PPM/2);
     camera.update();  

     //Ground body  
     BodyDef groundBodyDef =new BodyDef();  
     groundBodyDef.position.set(new Vector2(width/PPM/2/2, 0));  
     Body groundBody = world.createBody(groundBodyDef);  
     PolygonShape groundBox = new PolygonShape();  
     groundBox.setAsBox((width/PPM/2/2), 0.1f);  
     groundBody.createFixture(groundBox, 0.0f);  

     //wall left
     BodyDef wallLeftDef = new BodyDef();
     wallLeftDef.position.set(0, 0f);
     Body wallLeftBody = world.createBody(wallLeftDef);

     PolygonShape wallLeft = new PolygonShape();
     wallLeft.setAsBox(width/PPM/20/2, height/PPM/2);
     wallLeftBody.createFixture(wallLeft,0.0f);

     //wall right
     BodyDef wallRightDef = new BodyDef();
     wallRightDef.position.set((width/PPM)/2, 0f);
     Body wallRightBody = world.createBody(wallRightDef);

     PolygonShape wallRight = new PolygonShape();
     wallRight.setAsBox(width/PPM/20/2, height/PPM/2);
     wallRightBody.createFixture(wallRight,0.0f);



     //Dynamic Body  
     BodyDef bodyDef = new BodyDef();  
     bodyDef.type = BodyType.DynamicBody;  
     bodyDef.position.set((width/PPM)/2/2, (height / PPM)/2/2);  
     player = world.createBody(bodyDef);  
     CircleShape dynamicCircle = new CircleShape();  
     dynamicCircle.setRadius(5f/PPM);  
     FixtureDef fixtureDef = new FixtureDef();  
     fixtureDef.shape = dynamicCircle;  
     fixtureDef.density = 0.4f;  
     fixtureDef.friction = 0.2f;  
     fixtureDef.restitution = 0.6f;  
     player.createFixture(fixtureDef);  
     debugRenderer = new Box2DDebugRenderer();  

     //Lighting

     handler = new RayHandler(world);
     handler.setCombinedMatrix(camera.combined);
     PointLight l1 = new PointLight(handler,5000,Color.CYAN,width/PPM/2,width/PPM/2/2/2,height/PPM/2/2);

     new PointLight(handler,5000,Color.PURPLE,width/PPM/2,width/PPM/2/1.5f,height/PPM/2/2);


     shapeRenderer.setProjectionMatrix(camera.combined);

}  


public void update() { 
    if(Gdx.input.isKeyJustPressed(Keys.UP)) { 
        player.applyForceToCenter(0, 0.75f, true);
    }
    if(Gdx.input.isKeyJustPressed(Keys.RIGHT)) { 
        player.applyForceToCenter(0.5f, 0f, true);
    }
    if(Gdx.input.isKeyJustPressed(Keys.LEFT)) { 
        player.applyForceToCenter(-0.5f, 0f, true);
    }

}



@Override  
public void dispose() {  
}  

@Override  
public void render() {   



     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);  
     debugRenderer.render(world, camera.combined);  
     world.step(1/60f, 6, 2); 
     handler.updateAndRender();




     shapeRenderer.begin(ShapeType.Filled);
     shapeRenderer.setColor(1, 1, 1, 1);
     shapeRenderer.circle(player.getPosition().x, player.getPosition().y, 5f/PPM, 100);
     shapeRenderer.end();

    camera.position.set(player.getPosition().x, player.getPosition().y, 0); 

    camera.update();
    update();


}  

@Override  
public void resize(int width, int height) {  




}  

@Override  
public void pause() {  
}  

@Override  
public void resume() {  
}  

}

like image 398
user3650664 Avatar asked Nov 14 '14 09:11

user3650664


1 Answers

You are only setting the shapeRenderers projectionmatrix once so it is not updating when you render.

You should put

shapeRenderer.setProjectionMatrix(camera.combined);

to your render method right before

 shapeRenderer.begin(ShapeType.Filled);
like image 160
ADDER Avatar answered Sep 23 '22 05:09

ADDER