I am trying to render some entities in random positions on the screen. The spawning positions are okay however when I move the player entity (which the camera is focused on) and pass the middle of the screen (then the camera starts moving) the sprites of the other entities also move with me, which I do not want.

I tried giving them postions in the world and not in the screen using cam.unproject(position); which converts psotions relative to the screen into positions relative to the game world, but that did not work either.

They are rendered the same way all other sprites are rendered in libgdx

This is the code where I initialize the entities:

Texture zombie = new Texture("zombie.png");
    float x, y;
    for (int i = 0; i < enemy_amout; i++) {
        x = rm.nextFloat() * (mapWidth * 8f- zombie.getWidth());
        y = rm.nextFloat() * (mapHeight * 8f - zombie.getHeight());
        Vector3 position = new Vector3(x, y, 0);
        enemies[i] = new Entity(zombie, position.x, position.y, 1);

This is what the game looks like when I start it

And this is what it looks like after I moved the player character to the right. As you can see they moved alongside the camera but I want them to remain on their original positions.

  • \$\begingroup\$ Would you mind to show us how you are rendering the Player character \$\endgroup\$ – Sebastien Servouze May 16 '18 at 9:31
  • \$\begingroup\$ So I fixed this problem by adding batch.setPrejwctionMatrix(cam.combined); but now all the sprites are rendering with enormous dimensions even though internally they are stillt 55x66px \$\endgroup\$ – Grual May 18 '18 at 15:43
  • \$\begingroup\$ Hmmm, would you mind show us Player / Ennemies initialization AND drawing ? \$\endgroup\$ – Sebastien Servouze May 21 '18 at 7:25
  • \$\begingroup\$ Enemy initialization is already in the post and this is the player initialization "player = new Entity(new Texture("character.png"), 0, 0, 3);". The Entity class has a sprite object in int that is initialized with the texture in the CConstructor and is drawn by simply calling the draw method of sprite \$\endgroup\$ – Grual May 25 '18 at 5:44

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.