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