The problem is that it does not work out correctly for me to draw a texture inside an object.

Ie I need to get this result here ( in the picture an example from the Internet ):

enter image description here

If we consider the example of my game, the final result should be like this:

enter image description here

But I can’t draw the textures so that they are inside the CyrcleShape object.

Initially, this is how it turned out for me:

enter image description here

But I managed to manually adjust the position of the texture. Only if at the same time the player makes a jump, then y textures slightly lags behind y CircleShape`s .

The reason for this is extremely incomprehensible to me. The only thing I have is a couple of assumptions.

1st assumption: Incorrect change of coordinates in the class PlayScreen .

 // в данном классе у меня есть обработчик событий пользователя, с помощью которых он управляет игроком @Override public void handleInput(float dt) { // do nothing player.setStay(true); // jumping if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) { player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true); player.setJump(true); player.setStay(false); light.setPosition(player.b2body.getPosition().x, player.b2body.getPosition().y); } // turn right if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2) { player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true); // player.getVelocity().x += Player.SPEED; player.setMoveRight(true); player.setMoveleft(false); player.setStay(false); } // turn left if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2) { player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true); //player.getVelocity().x =- Player.SPEED; player.setMoveRight(false); player.setMoveleft(true); player.setStay(false); } // escape button - game on pause if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) { GameLoader.currentIndex = GameLoader.MENU_PAUSE_STATE; GameLoader.gameLoader.addState(new MenuPauseScene()); GameLoader.gameLoader.setNewState(); } } 

2nd assumption: Incorrect rendering and updating of the position of the texture in the class Player

  // при рисовании уровня, сначала я вызываю update(), а потом уже render() public void update(float dt) { setPosition((b2body.getPosition().x - getWidth() / 2 ) *1, (b2body.getPosition().y - getHeight() /2 ) * MyGdxGame.PPM); } public void render(SpriteBatch batch, float dt) { stateTime += Gdx.graphics.getDeltaTime(); // загружаю анимацию и как раз пытаюсь костыльно выравнивать текстуру: // чтобы она была внутри фигуры, а не еще где-нибудь if (stay) { currentFrame = stayAnimation.getKeyFrame(stateTime, true); batch.draw(currentFrame, getX() + 170 , getY() - 27, 300f, 300f ); } if (moveRight) { currentFrame = walkAnimation.getKeyFrame(stateTime, true); batch.draw(currentFrame, getX() + 170 , getY() - 27, 300f, 300f ); } if (moveleft) { currentFrame = walkAnimation.getKeyFrame(stateTime, true); batch.draw(currentFrame, getX() + 170 , getY() - 27, 300f, 300f ); } if (isJump) { currentFrame = walkAnimation.getKeyFrame(stateTime, true); batch.draw(currentFrame, getX() + 170 , getY() - 27, 300f, 300f ); } } 

3rd guess: My textures are wrong

Here are the textures that I drew yesterday

resource number 1 resource number 2

And the most interesting thing is that the wrong rendering of the texture (lagging behind the object) occurs only when jumping. If the player simply walks, then nothing is noticed.

I just can not understand why my texture cannot move synchronously with CircleShape .

PS: I just recently started learning libGDX , so please understand me correctly.

  • In my opinion, you lost pixels per meter and when drawing do not take the coefficient into account - katso
  • @katso Don't you know how to fix this? - SlandShow
  • @katso Now just noticed that the texture is behind the object only when jumping. If you just walk, then everything will be fine - SlandShow

1 answer 1

The problem turned out to be in OrthographicCamera .

The error was that I had an incorrectly configured orthographic camera. It is because of this that I did not have the synchronicity in the movement of the texture itself and CircleShape .

Thereby, it was necessary just to set up the camera correctly.

And instead:

  gameCam = new OrthographicCamera((MyGdxGame.V_WIDTH / MyGdxGame.PPM) * 2, (MyGdxGame.V_HEIGHT / MyGdxGame.PPM )* 2 ); gamePort = new FitViewport( MyGdxGame.V_WIDTH / MyGdxGame.PPM, MyGdxGame.V_HEIGHT / MyGdxGame.PPM, gameCam); hud = new Hud(game.batch); 

Write here such code:

 gameCam = new OrthographicCamera((MyGdxGame.V_WIDTH / MyGdxGame.PPM) * 2, (MyGdxGame.V_HEIGHT / MyGdxGame.PPM )* 2 ); gameCam.position.set(player.getPosition().x,player.getPosition().y,0);