Tuesday 15 March 2011

java - Libgdx body position and shaperenderer position not the same -



java - Libgdx body position and shaperenderer position not the same -

i'm new libgdx in general. have ball bouncing world , experimenting it. before had fixed photographic camera position such:

http://prntscr.com/567hvo

after have putted next line in render method photographic camera keeps next player (which circle) :

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

the player body type.

so when follow player, shape renderer acts weird now. @ happens:

http://prntscr.com/567i9a

even though referring same x , y position of player photographic camera , shape renderer, not in same position.

here 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 photographic camera photographic 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() { }

}

you setting shaperenderers projectionmatrix 1 time not updating when render.

you should put

shaperenderer.setprojectionmatrix(camera.combined);

to render method right before

shaperenderer.begin(shapetype.filled);

java camera libgdx shape-rendering

No comments:

Post a Comment