Problem with bounds!


here is my code:
#include “raylib.h”

#include “raymath.h”

#include “Character.h”

#include “Prop.h”

/////////////// MAIN //////////

int main()

{

const int windowWidth = 1024;                                                // width

const int windowHeight = 768;                                                // height

InitWindow(windowWidth, windowHeight, "2DAdventureDemo"); // load window

Vector2 mapPos = {0.0f, 0.0f};

const float mapScale{4.0f};

SetTargetFPS(60); // fps

   

Character knight{windowWidth, windowHeight};

//PROP

Prop props[2]

{

    Prop{Vector2{600.0f, 300.0f}, LoadTexture("nature_tileset/Rock.png")},

    Prop{Vector2{400.0f, 500.0f}, LoadTexture("nature_tileset/Log.png")}

};

   

Texture2D Map = LoadTexture("nature_tileset/OpenWorldMap24x24.png"); // load texture

while (!WindowShouldClose())

{

    BeginDrawing();

    ClearBackground(WHITE);

    mapPos = Vector2Scale(knight.GetWorldPos(), -1.0f);

    // DRAW MAP

    DrawTextureEx(Map, mapPos, 0.0, mapScale, WHITE);

        for (auto Prop : props)

        {

            Prop.Render(knight.GetWorldPos());

        }

       

    knight.Tick(GetFrameTime());

    //Map bounds

    if (knight.GetWorldPos().x < 0.f || knight.GetWorldPos().y < 0.f || knight.GetWorldPos().x + windowWidth > Map.width * mapScale || knight.GetWorldPos().y + windowHeight > Map.height * mapScale)

    {

        knight.UndoMovement();

       

    }

           

    EndDrawing();

}

CloseWindow();

}
here is the character cpp:
#include “Character.h”

#include “raymath.h”

Character::Character(int winWidth, int winHeight)

{

width = texture.width / maxframes;

height = texture.height;

ScreenPos = {

    static_cast<float>(winWidth) / 2.0f - scale * (0.5f * width),

    static_cast<float>(winHeight) / 2.0f - scale * (0.5f * height)

            };

}

// TICK

void Character::Tick(float deltaTime)

{

worldPosLastFrame = WorldPos;

Vector2 direction{};

if (IsKeyDown(KEY_A))

{

    direction.x -= 1.0;

}

if (IsKeyDown(KEY_D))

{

    direction.x += 1.0;

}

if (IsKeyDown(KEY_W))

{

    direction.y -= 1.0;

}

if (IsKeyDown(KEY_S))

{

    direction.y += 1.0;

}

if (Vector2Length(direction) != 0)

{

    WorldPos = Vector2Add(WorldPos, Vector2Scale(Vector2Normalize(direction), speed));

    direction.x < 0.f ? RightLeft = -1.0f : RightLeft = 1.0f;

    texture = Run;

}

else

{

    texture = Idle;

}

RunningTime += deltaTime;

if (RunningTime >= updateTime)

{

    frame++;

    RunningTime = 0.f;

    if (frame > maxframes)

    {

        frame = 0.f;

    }

}

// DRAW KNIGHT

Rectangle source{frame * width, 0.0f, RightLeft * width, height};

Rectangle dest{ScreenPos.x, ScreenPos.y, scale * width, scale * height};

DrawTexturePro(texture, source, dest, Vector2{}, 0.f, WHITE);

}

void Character::UndoMovement()

{

WorldPos = worldPosLastFrame;

}
Here is the header:
#include “raylib.h”

#include “raymath.h”

class Character

{

public:

Character(int winWidth, int winHeight);

void Tick(float deltaTime);

Vector2 GetWorldPos()

{

    return WorldPos;

}

void UndoMovement();

private:

   

 // right left movement

float RightLeft = 1.0f;

// Anim variables

float RunningTime{};

int frame{};

int maxframes{6};

float updateTime{1.f / 12.f};

float speed{4.0f};

float width{};

float height{};

Texture2D texture{LoadTexture("characters/knight_idle_spritesheet.png")};

Texture2D Idle{LoadTexture("characters/knight_idle_spritesheet.png")};

Texture2D Run{LoadTexture("characters/knight_run_spritesheet.png")};

Vector2 ScreenPos{};

Vector2 WorldPos{};

Vector2 worldPosLastFrame{};

float scale{4.0f};

};
i have a problem with the bounds as you see in the picture, it starts way to early and cuts off large portion of the map how do i fix this?

Short answer: set windowWidth and windowHeight to 600 as per the course.

Longer answer:

DrawTextureEx does some weird scaling based on the initial width and height set for the game window, causing the map to look larger than it should be. I don’t know the underlying mechanics as to why but for now please make sure that both windowWidth and windowHeight are set to 600.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms