Hey,
so i made quite some progress with my game. But acidently i encountered a strange Problem.
I basicly set the position of the Camera to the player.
So if the player moves arount the camera follows him.
The player sets its position in the Act method to the position of the box 2d body.
The Player is basicly an extended actor, he has a sprite and some other features.
Now i made some background and a Menue Layer.
And if the player moves fast, the Menue moves for some pixels, depending on the speed, i guess 1-6 pixels in each direction, the background effects also.
What i mean is, if i create a solid, fixed wall left, and a test ball with some physics directly near the wall, basicly touching it, so that it cant move left, because theres the wall, and the player moves fast left, the sprite of the ball moves for a few pixels into the sprite of the Wall, but the physics body of the wall doesnt move.
And if the player moves fast, the other entities, same class as player, appear to move in front of the background even though they stay still.
The Same thing happens with the menues, they move for some pixels if the player moves.
I can fix this, if i call the Act of all actors (also the menues) like 2-3 times each render. but this cant be the solution right ?!
the Funny thing is after i stumpled upon this i checked my other libgdx projects and this “problem” is also in them, so it appears im doing something really really wrong in general.
this is my render method :
@Override
public void draw(){
if (gameMap != null) gameMap.rendermap(this.getSpriteBatch()); // this renders the effects
if (player != null){
this.getCamera().viewportWidth = Gdx.graphics.getWidth();
this.getCamera().viewportHeight = Gdx.graphics.getHeight();
this.getCamera().position.set(player.sprite.getX(),player.sprite.getY(), 0);
this.getSpriteBatch().setProjectionMatrix(this.getCamera().combined);}
inpudhandling();
updateUIPositions();
super.draw();
world.step(1/60f, 6, 2);
}
if i try to render the effects (and the map) after setting the camera, there locations are wrong. (they get the ProjectionMatrix of this stages sprite batch)
Now i tryed to to let the camera be centered on the player with “look at”, but sadly i get a black screen if i do this.
If i cast the Stage Camera to a Orthogonal camera i also can use the update method on it, however it appears to make no difference.
now this gets called by the State Manager :
GameStage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 60f));
GameStage.draw();
if i switch the act and the draw in the Stage Manager makes no difference.
if i do a
super.act(Gdx.graphics.getDeltaTime());
after the
this.getSpriteBatch().setProjectionMatrix(this.getCamera().combined);}
in the draw metho, it appears to work, but then im basicly acting twice. if i only act there and im not letting the Stage Manager call the act the same problem happens.
act of the player :
//getting physics position
Vector2 position = body.getPosition();
// setting position of actor and sprite to physics location
//actor
this.setPosition(position.x-16,position.y-16);
this.setRotation( MathUtils.radiansToDegrees*body.getAngle());
// sprite
actorPos = new Vector2(this.localToStageCoordinates(new Vector2(0,0)));
sprite.setPosition(actorPos.x,actorPos.y);
sprite.setRotation( MathUtils.radiansToDegrees*body.getAngle());
draw of the player :
public void draw(Batch batch,float dt) {
if (this.sprite != null) sprite.draw(batch);
}
menue background act :
if (this.background != null) {background.setPosition(this.getX()+-getWidth()/2, this.getY()+-getHeight()/2);}
draw :
public void draw(Batch batch,float dt) {
super.draw(batch,dt);
}
and its update method in the game :
public void updateUIPositions(){
actionMenue.setBounds(this.getCamera().position.x, this.getCamera().position.y, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
i now have the action menue as an “complete actor”, so i set the sprites position to the actor, but it really makes no difference, if i set it directly, or if i set it to the actor and also set the actor, ive tryed both.
Also as ive mentioned this problem also appears in a other “game” of me, but there i have not been working with Scene 2d, because of that i think im doing something wrong in the draw of the game.
i hope someone can help me.
thanks in advance