I'm currently working on a uni project (I'm trying to make game sort of like Wolfenstein3D). I used an raylib example of maze to make a map for my game. But now i have problem with the wall collision and collision w/ the border of the map.
I'm not so good with c++ but i now the basics and still can't figure it out (it's not all of the code, only the code for gameplay screen). I will be really grateful for any advices.
#include "raylib.h"
#include <chrono>
#include <thread>
#include "raylib\raylib\src\rcamera.h"
#include <stdlib.h>
int main(void) {
//Inicjalizacja okna
const int screenWidth = 1200;
const int screenHeight = 800;
InitWindow(screenWidth, screenHeight, "Rack&Ruin");
//Camera init
Camera camera = { 0 };
camera.position = Vector3{ 0.0f, 0.4f, 0.0f }; //Pozycja
camera.target = Vector3{ 0.185f, 0.4f, 0.0f }; //Punkt widzenia
camera.up = Vector3{ 0.0f, 1.0f, 0.0f }; //Rotacja
camera.fovy = 45.0f; //Zakres widzenia Y
camera.projection = CAMERA_PERSPECTIVE;
int cameraMode = CAMERA_FIRST_PERSON; //Kamera ustawiona na widok z pierwszej osoby
//Cursor position
Vector2 cursorPosition = { (float)screenWidth / 2, (float)screenHeight / 2 };
//Map init
Image imageMap = LoadImage("resources/maze_map.png");
Texture2D cubicmap = LoadTextureFromImage(imageMap);
Mesh mesh = GenMeshCubicmap(imageMap, Vector3{ 1.0f, 1.5f, 1.0f });
Model model = LoadModelFromMesh(mesh);
//Textures
Texture2D maptexture = LoadTexture("resources/cubicmap_texture.png");
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = maptexture;
//Checking for collision (loading the map for that)
Color* mapPixels = LoadImageColors(imageMap);
UnloadImage(imageMap);
Vector3 mapPosition = { -1.0f, 0.0f, -9.0f }; //Ustawienie pozycji mapy
int frameCount = 0;
SetTargetFPS(60);
//GAME LOOP
while (!WindowShouldClose())
{
Vector2 mousePos = GetMousePosition();
Vector3 oldCamPos = camera.position;
UpdateCamera(&camera, CAMERA_FIRST_PERSON);
HideCursor();
if (mousePos.x >= screenWidth - 1 || mousePos.x <= screenWidth + 1)
SetMousePosition(screenWidth / 2, screenHeight / 2);
if (mousePos.y >= screenHeight - 1 || mousePos.y <= screenHeight + 1)
SetMousePosition(screenWidth / 2, screenHeight / 2);
// Wall logic
Vector2 playerPos = { camera.position.x, camera.position.z };
float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision)
int playerCellX = (int)(playerPos.x - mapPosition.x + 0.4f);
int playerCellY = (int)(playerPos.y - mapPosition.z + 0.4f);
// Out-of-limits security check
if (playerCellX < 0) playerCellX = 0;
else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1;
// Check map collisions using image data and player position
for (int y = 0; y < cubicmap.height; y++)
{
for (int x = 0; x < cubicmap.width; x++)
{
if ((mapPixels[y * cubicmap.width + x].r == 0) &&
(CheckCollisionCircleRec(playerPos, playerRadius,
Rectangle {mapPosition.x - 0.5f + x * 1.0f , mapPosition.z - 0.5f + y * 1.0f , 1.0f, 1.0f})))
{
// Collision detected, reset camera position
camera.position = oldCamPos;
}
}
}
/*------------------------GRAPHICS-----------------------------*/
BeginDrawing();
ClearBackground(RAYWHITE);
//RYSOWANIE ROZGRYWKI
//3D MODE
BeginMode3D(camera);
//Rysowanie mapy
DrawModel(model, mapPosition, 0.8f, WHITE);
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
}
/*--------------------------------------------------------------------*/
UnloadTexture(maptexture);
UnloadTexture(cubicmap);
UnloadModel(model);
UnloadImageColors(mapPixels);
CloseWindow();
return 0;
}
I planned to just check for the white pixels (because the walls in the png are white pixels) and when player collide with them, he can't walk through them, but it won't work no matter what i do. I also don't know how to create map differently, I thought about raycasting but I don't know how to start with that.