2
\$\begingroup\$

I have a problem, when i try to render textures. I read positions of gameObjects, for example coin and create box2d objects circle shape. I have wrote game logic and began make a graphic. But for some reason, textures drawing not in box2d objects, but they shifted to the right and up as mini-map in games.

Code

  batcher.begin();

  sprite = new Sprite(AssetsLoader.coin);

  sprite.setPosition((body.getPosition().x - sprite.getWidth()/2), ((body.getPosition().y - sprite.getHeight()/2)));

  sprite.draw(batcher);
  batcher.end();

This is what happened

enter image description here

As you can see above, sprite position like body position. And something else: when i move my mainHero all rendered coins moving with him. On the screen they save their position, but in world they are moving. So how i can render coin texture exactly into box2d body.

May be something wrang with camera?

cam = new OrthographicCamera();

cam.setToOrtho(false, 400, 600);

or with Batcher?

batcher = new SpriteBatch();

batcher.setProjectionMatrix(world.getCamera().combined);

I will not give all my code, because it is big and no one will try to understand it.I need an idea or just an advice or a hint. Sorry for my English, hello everyone from Belarus :) Where do I look and what a start? Thank you.

\$\endgroup\$
  • \$\begingroup\$ Are the objects in the same space? It seems like you're not compensating for the camera movement? \$\endgroup\$ – Sidar Nov 13 '17 at 18:01
  • \$\begingroup\$ Yeah, the objects in the same space, yes, may be you are right, i am not compensating for the camera movement, But even when I have not done a single step, coins already displayed not on their positions and relatively as if some mythical camera's coordinate.@Sidar \$\endgroup\$ – Dastin_DV Nov 13 '17 at 18:06
  • \$\begingroup\$ Are you scaling your coins and if so does getWidth() scale ? \$\endgroup\$ – Sidar Nov 13 '17 at 18:28
  • \$\begingroup\$ Now i understood. When i make my gamecamera equals to the gameworld dimensions and set its position in cam.position.set(cam.viewportHeight/2, cam.viewportWidth/2, 0); all coins on their positions. @Sidar \$\endgroup\$ – Dastin_DV Nov 13 '17 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.