Classy Clash, Character postion changed

Hello!
I don’t know where I went wrong with my code but when I finished it, the position of my character moved from the middle of the screen to the bottom right and I don’t know how to fix it. I am dyslexic so I probably messed up somewhere but I can’t seem to find where I went wrong.




Hi welcome to the forums @meijii for future refrences the code can be copied into the forums in the preformated thext block </> this makes it easier for people to help as we can read the code cleaner.

Ok lets break down your problem by following the code back.

In main
Character knight; knight.setScreenPos(windowWidth, windowHeight);

This sets initial screen position of the knight character based on the window dimensions.

Ok so Lets go look at the code for setScreenPos()

void Character::setScreenPos(int winWidth, int winHeight)
{
    screenPos = {(float)winWidth / 2.0f - 4.0f * (0.5f * (float)texture.width / 10.0f),
                 (float)winHeight / 2.0f - 4.0f * (0.5f * (float)texture.height)
    };
}

(float)winWidth / 2.0f - 4.0f * (0.5f * (float)texture.width / 10.0f) : This positions the character by offsetting the center of the window by the calculated value, effectively centering the character’s texture within the window.

Visual Effect

  • Dividing by 10 : The character’s texture is centered with an offset of approximately one-twentieth of its width. So Changing this value to lower would create and offset so try dividning by 6 instead.

Hi sorry, it my first time posting here so i’ll remember for next time! It didn’t seem to change anything and the character is still stuck at the bottom right.

Can you paste the code in so I can see the breakdown more clearly?

#include "raylib.h"
#include "raymath.h"

class Character
{
public:
    Vector2 getWorldPos() { return worldPos; }
    void setScreenPos(int winWidth, int winHeight);
    void tick(float deltaTime);

private:
    Texture2D texture{LoadTexture("characters/spr_player_right_ide.png")};
    Texture2D idle{LoadTexture("characters/spr_player_right_idle.png")};
    Texture2D run{LoadTexture("characters/spr_player_right_walk.png")};
    Vector2 screenPos{};
    Vector2 worldPos{};
    // 1:right -1:left
    float rightleft{1.f};
    // animation variables
    float runningTime{};
    int frame{};
    const int maxframes{12};
    const float updateTime{1.f / 12.f};
    const float speed{4.f};
};

void Character::setScreenPos(int winWidth, int winHeight) {
    screenPos = {
        (float)winWidth / 2.0f - 4.0f * (0.5f * (float)texture.width / 6.0f),
        (float)winHeight / 2.0f - 4.0f * (0.5f * (float)texture.height)};
};


void Character::tick(float deltaTime)
{
    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.0)
    {
        // set worldPos = WorldPos + direction

        worldPos = Vector2Subtract(worldPos, Vector2Scale(Vector2Normalize(direction), speed));
        direction.x < 0.f ? rightleft = -1.f : rightleft = 1.f;
        texture = run;
    }
    else
    {
        texture = idle;
    }
    // update animation frame
    runningTime += deltaTime;
    if (runningTime >= updateTime)
    {
        frame++;
        runningTime = 0.f;
        if (frame > maxframes)
            frame = 0;
    }
    // draw character
    Rectangle source{frame * (float)texture.width / 6, 0.f, rightleft * (float)texture.width / 6,(float)texture.height};
    Rectangle dest{screenPos.x, screenPos.y, 4.0f *(float)texture.width / 6, 4.0f * (float)texture.height};
    DrawTexturePro(texture, source, dest, Vector2{}, 0.f, WHITE);
};


int main()
{
    const int windowWidth{384};
    const int windowHeight{384};
    InitWindow(windowWidth, windowHeight, "Mei-Mei's Top Down Game!");

    Texture2D map = LoadTexture("nature_tileset/trailZnErrorz.png");
    Vector2 mapPos{0.0, 0.0};

    Character knight;
    knight.setScreenPos(windowWidth, windowHeight);

     



    SetTargetFPS(60);
    while (!WindowShouldClose())
    {
        BeginDrawing();
        ClearBackground(WHITE);

        mapPos = Vector2Scale(knight.getWorldPos(), 1.f);

        // draw map
        DrawTextureEx(map, mapPos, 0.0, 4.0, WHITE);
        knight.tick(GetFrameTime());


        EndDrawing();
    }
    CloseWindow();
};

Here could be an issue, you called 6 instead of 6.0f . When dividing by an integer, the division might be treated as integer division, you want to directly specify that you are trying to divide it by a float.

Try this and let me know

I have changed the code but I’m now getting different errors

 // draw character
    Rectangle source{frame * (float)texture.width / 6.0, 0.f, rightleft * (float)texture.width / 6.0,(float)texture.height};
    Rectangle dest{screenPos.x, screenPos.y, 4.0f *(float)texture.width / 6.0, 4.0f * (float)texture.height};
    DrawTexturePro(texture, source, dest, Vector2{}, 0.f, WHITE);
};

its telling me that frame, rightleft and 4.0f the first one after screenPos.y can not be narrowed from a double or a float,

You have forgot the .f after the 6, you need to specify its a float.

    Rectangle source{frame * (float)texture.width / 6.f, 0.f, rightLeft * (float)texture.width / 6.f, (float)texture.height};
    Rectangle dest{screenPos.x, screenPos.y, 4.0f * (float)texture.width / 6.0f, 4.0f * (float)texture.height};
    DrawTexturePro(texture, source, dest, Vector2{}, 0.f, WHITE);

Once again be careful, there is a distinction between a double and a float.

If the problem still persists please paste the next error message

it’s still telling me that rightleft and 4.0f cannot be narrowed from a float or double
this is the message I get

“message”: "non-constant-expression cannot be narrowed from type ‘double’ to ‘float’ in initializer list

Paste in the direct error, so i can see what line its referring too.

Also paste in your full code again. If you have changed the source and the dest to make sure they have float values you should not be getting them errors

Here is the full code

#include "raylib.h"
#include "raymath.h"

class Character
{
public:
    Vector2 getWorldPos() { return worldPos; }
    void setScreenPos(int winWidth, int winHeight);
    void tick(float deltaTime);

private:
    Texture2D texture{LoadTexture("characters/spr_player_right_ide.png")};
    Texture2D idle{LoadTexture("characters/spr_player_right_idle.png")};
    Texture2D run{LoadTexture("characters/spr_player_right_walk.png")};
    Vector2 screenPos{};
    Vector2 worldPos{};
    // 1:right -1:left
    float rightleft{1.f};
    // animation variables
    float runningTime{};
    int frame{};
    const int maxframes{12};
    const float updateTime{1.f / 12.f};
    const float speed{4.f};
};

void Character::setScreenPos(int winWidth, int winHeight) {
    screenPos = {
        (float)winWidth / 2.0f - 4.0f * (0.5f * (float)texture.width / 6.0f),
        (float)winHeight / 2.0f - 4.0f * (0.5f * (float)texture.height)};
};


void Character::tick(float deltaTime)
{
    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.0)
    {
        // set worldPos = WorldPos + direction

        worldPos = Vector2Subtract(worldPos, Vector2Scale(Vector2Normalize(direction), speed));
        direction.x < 0.f ? rightleft = -1.f : rightleft = 1.f;
        texture = run;
    }
    else
    {
        texture = idle;
    }
    // update animation frame
    runningTime += deltaTime;
    if (runningTime >= updateTime)
    {
        frame++;
        runningTime = 0.f;
        if (frame > maxframes)
            frame = 0;
    }
    // draw character
    Rectangle source{frame * (float)texture.width / 6.0f, 0.f,rightleft * (float)texture.width / 6.0,(float)texture.height};
    Rectangle dest{screenPos.x, screenPos.y, 4.0f *(float)texture.width / 6.0, 4.0f * (float)texture.height};
    DrawTexturePro(texture, source, dest, Vector2{}, 0.f, WHITE);
};


int main()
{
    const int windowWidth{384};
    const int windowHeight{384};
    InitWindow(windowWidth, windowHeight, "Mei-Mei's Top Down Game!");

    Texture2D map = LoadTexture("nature_tileset/trailZnErrorz.png");
    Vector2 mapPos{0.0, 0.0};

    Character knight;
    knight.setScreenPos(windowWidth, windowHeight);

     



    SetTargetFPS(60);
    while (!WindowShouldClose())
    {
        BeginDrawing();
        ClearBackground(WHITE);

        mapPos = Vector2Scale(knight.getWorldPos(), 1.f);

        // draw map
        DrawTextureEx(map, mapPos, 0.0, 4.0, WHITE);
        knight.tick(GetFrameTime());


        EndDrawing();
    }
    CloseWindow();
};

this is the error messages i get is

[{
	
	"owner": "cpptools",
	"severity": 8,
	"message": "non-constant-expression cannot be narrowed from type 'double' to 'float' in initializer list [-Wc++11-narrowing]",
	"source": "gcc",
	"startLineNumber": 67,
	"startColumn": 63,
	"endLineNumber": 67,
	"endColumn": 63
}]
[{
	
	"owner": "cpptools",
	"severity": 8,
	"message": "non-constant-expression cannot be narrowed from type 'double' to 'float' in initializer list [-Wc++11-narrowing]",
	"source": "gcc",
	"startLineNumber": 68,
	"startColumn": 46,
	"endLineNumber": 68,
	"endColumn": 46
}]

The Problem Stilll is there as suspected , you have doubles and float values be careful as
The second 6 is still a double and you have not specified it as a float .

Should be

rightLeft * (float)texture.width / 6.f, (float)texture.height}; // .f specifies float value

also on the next line you have:

It Should be

(float)texture.width / 6.0f, 4.0f * (float)texture.height};

If you do not put .f after it, the engine will take it as double value and this is why you are getting this error.

okii it runs now with no error, i have put (float) infront of the rightleft and fix the other 6’s i forgot but the character is still at the bottom right screen. i will put the updated code below

#include "raylib.h"
#include "raymath.h"

class Character
{
public:
    Vector2 getWorldPos() { return worldPos; }
    void setScreenPos(int winWidth, int winHeight);
    void tick(float deltaTime);

private:
    Texture2D texture{LoadTexture("characters/spr_player_right_ide.png")};
    Texture2D idle{LoadTexture("characters/spr_player_right_idle.png")};
    Texture2D run{LoadTexture("characters/spr_player_right_walk.png")};
    Vector2 screenPos{};
    Vector2 worldPos{};
    // 1:right -1:left
    float rightleft{1.f};
    // animation variables
    float runningTime{};
    int frame{};
    const int maxframes{12};
    const float updateTime{1.f / 12.f};
    const float speed{4.f};
};

void Character::setScreenPos(int winWidth, int winHeight) {
    screenPos = {
        (float)winWidth / 2.0f - 4.0f * (0.5f * (float)texture.width / 6.0f),
        (float)winHeight / 2.0f - 4.0f * (0.5f * (float)texture.height)};
};


void Character::tick(float deltaTime)
{
    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.0)
    {
        // set worldPos = WorldPos + direction

        worldPos = Vector2Subtract(worldPos, Vector2Scale(Vector2Normalize(direction), speed));
        direction.x < 0.f ? rightleft = -1.f : rightleft = 1.f;
        texture = run;
    }
    else
    {
        texture = idle;
    }
    // update animation frame
    runningTime += deltaTime;
    if (runningTime >= updateTime)
    {
        frame++;
        runningTime = 0.f;
        if (frame > maxframes)
            frame = 0;
    }
    // draw character
    Rectangle source{frame * (float)texture.width / 6.0f, 0.f,(float)rightleft * (float)texture.width / 6.0f,(float)texture.height};
    Rectangle dest{screenPos.x, screenPos.y, 4.0f *(float)texture.width / 6.0f, 4.0f * (float)texture.height};
    DrawTexturePro(texture, source, dest, Vector2{}, 0.f, WHITE);
};


int main()
{
    const int windowWidth{384};
    const int windowHeight{384};
    InitWindow(windowWidth, windowHeight, "Mei-Mei's Top Down Game!");

    Texture2D map = LoadTexture("nature_tileset/trailZnErrorz.png");
    Vector2 mapPos{0.0, 0.0};

    Character knight;
    knight.setScreenPos(windowWidth, windowHeight);

     



    SetTargetFPS(60);
    while (!WindowShouldClose())
    {
        BeginDrawing();
        ClearBackground(WHITE);

        mapPos = Vector2Scale(knight.getWorldPos(), 1.f);

        // draw map
        DrawTextureEx(map, mapPos, 0.0, 4.0, WHITE);
        knight.tick(GetFrameTime());


        EndDrawing();
    }
    CloseWindow();
};

The only thing I can see is how the texture height and width is calculated on screen

I do not know if you are using your own sprites or following the course but this is the equation that will make the sprite appear on the screen.So if your texture width and height is diffrent than that from the lecture, then it will not calculate properly. you will need to adjust the math to match your sprite

Yes that helps!
Thank you so much!

Glad I could point you in the right direction.

Moving forward make sure to be careful With the Distinction of floats and integers also, as you could see this can also become a problem.

If the problem is solved please mark the soloution, so it can close the topic and also help point others to the correct soloution if the have the same issue.

Any more problems don’t be afraid to ask and create a new topic.

Glad to see students helping each other on finding a solution to problems. Great job!

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

Privacy & Terms