C++ Raylib Camera2D screen moving

Hello after I’ve done the c++ raylib course I decided to do my own projects, my newest game is a 2 player Top-Down game. But I have a problem and thats the camera moving for each player. The player can move but not the screen. Here’s my code;
(player.cpp)

#include "raylib.h"
#include "player.h"
#include "Fullscreen.h"

Player::Player(int placeOfTheCharacter, Color color) {
    playerColor = color;
    place = placeOfTheCharacter;

    screenWidth = GetMonitorWidth(GetCurrentMonitor());
    screenHeight = GetMonitorHeight(GetCurrentMonitor());

    Vector2 playerPosition = CenterPositionOnScreen(playerWidth, playerHeight, place);
    playerRec = { playerPosition.x, playerPosition.y, static_cast<float>(playerWidth), static_cast<float>(playerHeight) };
    
    playerCamera.offset = CenterPositionOnScreen(playerWidth, playerHeight, place);
    playerCamera.rotation = 0.f;
    playerCamera.target = playerPosition;
    playerCamera.zoom = 1.f;
}

void Player::tick( float dT) {

    if (IsKeyDown(KEY_W) && place == 1) {
        playerCamera.target.y++;
        playerRec.y--;

    }
    if (IsKeyDown(KEY_UP) && place == -1) {
        playerCamera.target.y++;
        playerRec.y--;

    }
    BeginMode2D(playerCamera);
    DrawRectangle(playerRec.x, playerRec.y, playerWidth, playerHeight, playerColor);
    EndMode2D();
}

Fullscreen.cpp:

#include "raylib.h"

#include "Fullscreen.h"

// ... and defined in a .cpp file

float screenWidth = GetMonitorWidth(GetCurrentMonitor());

float screenHeight = GetMonitorHeight(GetCurrentMonitor());

// Function to center the Position based on the screensize (the middle of the half of the screen)

Vector2 CenterPositionOnScreen(int objectWidth, int objectHeight, int place) {

    Vector2 center;

    center.x = (GetScreenWidth() - objectWidth) / 2 - (GetScreenWidth() / 4) * place;

    center.y = (GetScreenHeight() - objectHeight) / 2;

    return center;

}
and snippets from main.cpp:

Player player1{ -1, RED};
    Player player2{ 1, BLUE };

player1.tick( GetFrameTime() );
        player2.tick( GetFrameTime() );

Thanks for every answer and if you want to know something, what I’ve forgotten, tell me

While I’m not personally familiar with the built-in camera in Raylib I can point in the direction of a community of people who may be able to help. You can try asking the community at the Official Raylib Discord server, alternatively you could try asking our own community on Discord as well.

There may also be an example on Raylib’s website that can give some insight on the problem.

thanks for the the link for the Offical Raylib Discord

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

Privacy & Terms