0
\$\begingroup\$

I am developing a 2D survival game. In these days, without make some features, I am fixing some bug. There is one which I got stuck on: my map has a background made with grass, earth, desert, water and dungeon texture. Above them blocks are rendered. My problem is that block's texture is rendered a bit twisted. I cannot understand if the background is rendered wrongly or block's.

p.s. My block aren't a simple 2D texture, this is due to at the beginning: I wanted to make a 2D game with a perspective camera in order to seems it like Factorium; but later I changed idea. Now I keep using blocks as 3D model made up with Blockbench. I've model them in order to use only positive cords (blockbench makes axises begin from center-down of block, so I tried to move translate of 8,8)

enter image description here As you can see, every block has an offset increased on the left.

enter image description here As you can see, I translated of 8, 8

Player with camera class:

public abstract class Player3D extends DynamicEntity {

private StackItem mouseStack = StackItem.EMPTY;

private OrthographicCamera camera;
public PlayerHud playerHud;
public boolean inventoryOpen;
public PlayerInventory inventory;   //Player inventory
float radians;
int viewportH, viewportW;

public Player3D(int x, int y) {
    super("player/player.obj");
    this.setPosition(new Vector3(x, y, 0));
    CircleShape shape = new CircleShape();
    shape.setRadius(0.9f);
    this.setPhysic(shape, BodyDef.BodyType.DynamicBody, new FixtureDef());
    this.playerHud = new PlayerHud();
}

/**
 * Set camera about the player
 */
public void setCamera(WorldSize worldSize){
    this.camera = new OrthographicCamera(Gdx.graphics.getWidth()/32, Gdx.graphics.getHeight()/32);
    this.camera.position.set(this.getPlayerPos());
    this.camera.position.add(0f, 0f, 1f);
    this.camera.lookAt(this.getPosition());
    this.camera.update();

    this.viewportW = (int)this.camera.viewportWidth/2;
    this.viewportH = (int)this.camera.viewportHeight/2;
    this.moveCamera(worldSize.getWidth(), worldSize.getHeight());
}

/**
 * Called by render(), it provides listener on WASD in order to
 * move player, notice that camera is constraints to that, but won't when map edge has been got
 */
public void move(FlatWorld flatWorld, int worldWidth, int worldHeight) {

    int x = 0, y = 0;
    if (Gdx.input.isKeyPressed(Input.Keys.W))
        y = 1;
    if (Gdx.input.isKeyPressed(Input.Keys.S))
        y = -1;
    if (Gdx.input.isKeyPressed(Input.Keys.A))
        x = -1;
    if (Gdx.input.isKeyPressed(Input.Keys.D))
        x = 1;

    if(x != 0 || y != 0) {
        if (this.canPlayerMove(flatWorld, new Vector3(x, y, 0), worldWidth, worldHeight)) {
            this.modelInstance.transform.translate(x, y, 0);
            this.moveCamera(worldWidth, worldHeight);
        }
    }
    this.renderModel(null);
}

/**
 * Called in order to move camera within screen size. If player is almost to the edge,
 * camera will stop following it
 * @param worldWidth
 * @param worldHeight
 */
public void moveCamera(int worldWidth, int worldHeight){
        float newCameraX = Math.min(Math.max(this.getPlayerX(), this.viewportW), worldWidth - this.viewportW);
        float newCameraY = Math.min(Math.max(this.getPlayerY(), this.viewportH), worldHeight - this.viewportH);
        this.camera.position.set(newCameraX, newCameraY, this.camera.position.z);
        this.camera.update();
}

/**
 * Get a copy of the current position of the player, translate as the future one;
 *
 * @param translateCords how much player has to be moved
 * @return if the player can me moved<br> e.g. if the camera will not be over the map's edge
 * after have been moved
 */
private boolean canPlayerMove(FlatWorld flatWorld, Vector3 translateCords, int worldWidth, int worldHeight) {
    Vector3 playerPos = this.modelInstance.copy().transform.translate(translateCords).getTranslation(new Vector3());
    int xPlayer = (int)playerPos.x;
    int yPlayer = (int)playerPos.y;
    //Checking future player pos
    if(playerPos.x > 0 && playerPos.x < worldWidth && playerPos.y > 0 && playerPos.y < worldHeight){
        Block block = flatWorld.getBlocks().getBlockAt((int)playerPos.x, (int)playerPos.y);
        if(block != null)
            return block.getTypeBlock() == TypeBlock.WALKTHROUGH;
        return  flatWorld.getBlocksCourt().getCourt()[yPlayer][xPlayer] != FlatBlock.WATER;
    }
    return false;
}

/**
 * Called when mouse is moved, in order to rotate player towards pointer<br>
 * Mouse pointer cords begins from top-left corner. In order to make it relative
 * to the centre of the window, it is got via Camera#unproject
 */
public boolean rotatePlayerToMouse() {
    //get mouse pos relative to the centre of screen
    Vector3 mousePos = this.camera.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));
    Vector3 playerPos = this.getPosition();
    radians = (float) Math.atan2(mousePos.y - playerPos.y, mousePos.x - playerPos.x);
    radians -= Math.PI / 2; //It is relative to X-axis, but I need it relative to Y one
    this.modelInstance.transform.setFromEulerAnglesRad(0, 0, radians).setTranslation(playerPos);
    return true;
}


/**
 * Player owns camera, so when it is rendered, null will be passed as camera
 * @param camera
 */
@Override
public void renderModel(Camera camera) {
    super.renderModel(this.camera);
}

public Vector3 getPlayerPos(){ return this.getPosition();}
public int getPlayerX(){ return (int)getPlayerPos().x;}
public int getPlayerY(){ return (int)getPlayerPos().y;}

public OrthographicCamera getCamera(){ return camera; }

public StackItem getMouseStack() {
    return mouseStack;
}

public float getRadians() {
    return radians;
}

public void setMouseStack(StackItem mouseStack) {
    this.mouseStack = mouseStack;
}

}

Render background class:

    int scale = 32;
    public void renderWorld(Camera camera) {
    int startX = (int) (camera.position.x - camera.viewportWidth/2);
    int startY = (int) (camera.position.y - camera.viewportHeight/2);
    int width = (int) (camera.position.x + camera.viewportWidth/2);
    int height = (int) (camera.position.y + camera.viewportHeight/2);

    this.spriteBatch.begin();
    for (int x = startX; x < width; x++) {
        for (int y = startY; y < height; y++) {
            this.spriteBatch.draw(this.court[y][x], (x - startX) * scale, (y - startY) * scale, scale, scale);
        }
    }
    this.spriteBatch.end();
}

In order to render model block I use Model, ModelInstance and ModelBatch

\$\endgroup\$
2
  • \$\begingroup\$ I don't see a "twist" here, just a difference in scale or spacing. Am I missing something? \$\endgroup\$
    – DMGregory
    Apr 23 '21 at 15:43
  • \$\begingroup\$ Yeah, sorry. Google translate... \$\endgroup\$
    – DynoZ
    Apr 23 '21 at 15:57

You must log in to answer this question.

Browse other questions tagged .