0
\$\begingroup\$

I'm using Raylib, and I'm attempting to place tiles on a level, and the algorithm for placing the tiles is working.Player displays behind left walls

The problem is that the player will display over the walls on the bottom right hand side of the isometric grid. Player is drawn on top of the wall

Displays player over wall on all bottom right tiles

What am I doing wrong, and does this involve the painter's algorithm?

Here is the code:

#include "raylib.h"
#include <stdint.h>

//Defines
#define COLUMNS 10
#define ROWS 10

//Player
typedef struct Player
{
    int dirX; 
    int dirY;
    Texture2D playerTexture;
    Vector2 position;
    struct Player* player;
}Player;

//Change from 2D to Isometric
Vector2 twoDtoIso(Vector2 tilePosition);

void movePlayer(Player* player, int8_t newX, int8_t newY);

int main()
{
    // Initialization
    //-------------------------------------------------------------------------

    const uint8_t TILE_WIDTH    =     32;
    const uint8_t TILE_HEIGHT   =     32;
    const uint8_t PLAYER_OFFSET =     32;
    const float SCREEN_WIDTH    = 640.0f;
    const float SCREEN_HEIGHT   = 352.0f;
    const float SCREEN_CENTER_X = 288.0f; 
    const float SCREEN_CENTER_Y =   0.0f;

    InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Isometric Tile placement");

    //These images are loaded from a folder named "images" with 
    //isometric cubes that are 64 pixels by 64 pixels
    Texture2D wallTexture   = LoadTexture("images/wallTile.png");
    Texture2D groundTexture = LoadTexture("images/groundTile.png");

    Player player = {0, 0,LoadTexture("images/player.png"),(Vector2){ SCREEN_CENTER_X, PLAYER_OFFSET }};

    uint8_t levelData[COLUMNS][ROWS] = 
    {
        {1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,2,1}, //<-----The player is designated as 2, ground as 0, and wall tiles as 1
        {1,1,1,1,1,1,1,1,1,1}
    };

    static uint8_t tileType;  
    const uint8_t  NUM_COLUMNS = 10;
    const uint8_t  NUM_ROWS    = 10;
    const int8_t   newX        = 32;
    const int8_t   newY        = 16;

    Vector2 coords;
    Vector2 tilePosition;

    SetTargetFPS(30);              
    //------------------------End Initialization-------------------------------

    // Main game loop
    while (!WindowShouldClose())    
    {
        // Update
        //---------------------------------------------------------------------

        //Player input/movement 

        movePlayer(&player, newX, newY); 

        //--------------------------End Update---------------------------------

        // Draw
        //---------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(BLACK);

            //place tiles
            for (uint8_t i = 0; i < NUM_ROWS; i++)
            {   
                for (uint8_t j = 0; j < NUM_COLUMNS ; j++)
                {
                    tilePosition = (Vector2){(float)j * TILE_WIDTH, (float)i * TILE_HEIGHT};
                    tileType = levelData[i][j];
                    coords = twoDtoIso(tilePosition);

                    if(tileType == 0) //place ground tile
                    {
                         DrawTexture(groundTexture, coords.x + SCREEN_CENTER_X, coords.y + SCREEN_CENTER_Y, WHITE);
                    }
                    else if(tileType == 1) //place wall tile                
                    {
                        DrawTexture(wallTexture, coords.x + SCREEN_CENTER_X, coords.y + SCREEN_CENTER_Y, WHITE);
                    }
                    else //otherwise place the player 
                    {
                        DrawTexture(player.playerTexture, player.position.x, player.position.y, WHITE);
                    }
                }
            }//end for loop     

        EndDrawing();
        //---------------------------------------------------------------------
    }

    // De-Initialization
    //-------------------------------------------------------------------------
    UnloadTexture(groundTexture);    
    UnloadTexture(wallTexture);
    UnloadTexture(player.playerTexture);

    CloseWindow();                // Close window and OpenGL context
    //-------------------------------------------------------------------------

    return 0;
}

Vector2 twoDtoIso(Vector2 tilePosition)
{
    Vector2 tempPoint;
    tempPoint.x =  tilePosition.x - tilePosition.y;
    tempPoint.y = (tilePosition.x + tilePosition.y)/ 2;
    return(tempPoint);
}

void movePlayer(Player* player, int8_t newX, int8_t newY)
{
    if (IsKeyPressed(KEY_LEFT))
    { 
        player->position = (Vector2){ player->position.x - newX, player->position.y + newY };
    }
    if (IsKeyPressed(KEY_RIGHT))
    { 
        player->position = (Vector2){ player->position.x + newX, player->position.y - newY };
    }
    if (IsKeyPressed(KEY_UP))
    { 
        player->position = (Vector2){ player->position.x - newX, player->position.y - newY };
    }
    if (IsKeyPressed(KEY_DOWN))
    { 
        player->position = (Vector2){ player->position.x + newX, player->position.y + newY };
    }
}


```
\$\endgroup\$
1
\$\begingroup\$

You are painting your tiles in the wrong order. Yes, you would use the painters algorithm, which just means, that you draw from back to forward. In your image (I can't really see, where you 0,0 is in the image. 0,0 should be in the top middle of the screen and then like so:

enter image description here

if you "rotate" to change the direction you are viewing you need another draw order. It's normally solved by just inverting a loop from 0->n to n->0 or something (depends on where you 0,0 is on the image)

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thank you so much. I will start working on this now. \$\endgroup\$ – Hawkbirdtree Dec 1 '19 at 17:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.