Help with raycaster

Joined
Dec 22, 2023
Messages
35
Reaction score
5
Hello,i've been trying to create a raycaster game with the help with this site Lodev site,but it does not work properly and it just stops after some seconds,can you guys help?


C:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "./ray/include/raylib.h"
#include "./ray/include/raymath.h"
#include "./cbmp/cbmp.h"
#define SCRW 800
#define SCRH 600
#define WW 30 // world width
#define WH 30 // world height
#define PLRS 1.5 // player speed
#define WALL 1
#define AIR 0
#define BLK_S 30.0f // block size 1



typedef struct plr {
    Vector2 dir;
    Vector2 pos;
    Vector2 cam;
} plr;
int gui = 1;
plr *mplr = NULL;


// Vector2 plr = (Vector2){1.0f,1.0f}; // player pos
// Vector2 plrd = (Vector2){30,0}; // player dir
// Vector2 cam = ()
int WORLD[WW][WH] = {AIR};


void handle_key(plr *p){
        if(WORLD[(int)(p->pos.x/BLK_S)+1][(int)(p->pos.y/BLK_S)] != WALL){
            if(IsKeyDown(KEY_D)) p->pos.x+= PLRS; // right move
        }
        if(WORLD[(int)(p->pos.x/BLK_S)-1][(int)(p->pos.y/BLK_S)] != WALL){
            if(IsKeyDown(KEY_A)) p->pos.x-= PLRS; // left  move
        }
        if(WORLD[(int)(p->pos.x/BLK_S)][(int)(p->pos.y/BLK_S)+1] != WALL){
            if(IsKeyDown(KEY_S)) p->pos.y+= PLRS; // down  move
        }   
        if(WORLD[(int)(p->pos.x/BLK_S)][(int)(p->pos.y/BLK_S)-1] != WALL){
            if(IsKeyDown(KEY_W)) p->pos.y-= PLRS; // up    move
        }
    if(IsKeyDown(KEY_Q)) CloseWindow();
    if(IsKeyPressed(KEY_G)) gui = !gui;
    // ROTATION:
        if(IsKeyDown(KEY_LEFT)){
        mplr->dir = Vector2Rotate(mplr->dir,-0.1);
        mplr->cam = Vector2Rotate(mplr->cam,-0.1);
        }
        if(IsKeyDown(KEY_RIGHT)){
        mplr->dir = Vector2Rotate(mplr->dir,0.1);
        mplr->cam = Vector2Rotate(mplr->cam,0.1);
        }
}


void init_world(char *image){
        BMP *data = bopen(image);
    unsigned char r = 0;
    unsigned char g = 0;
    unsigned char b = 0;
    for(int x = 0;x<WW;x++){
        for(int y = 0;y<WH;y++){
            get_pixel_rgb(data,x,WH-y-1,&r,&g,&b);
            if((r+g+b) == 0){
                WORLD[x][y] = WALL;
            }
        }
    }
}




int main(){
        init_world("./map.bmp");
        mplr = (plr*)malloc(sizeof(plr));   // main player 
        mplr->dir = (Vector2){0,-1}; // direction: up
    mplr->pos = (Vector2){BLK_S,BLK_S}; // x1 y1 on grid
    mplr->cam = (Vector2){1,0}; // camera 1x 1y
        InitWindow(SCRW,SCRH,"Wolfdenbar");
        float x = 0.0f;
        while(!WindowShouldClose()){
            handle_key(mplr);
        BeginDrawing();
        ClearBackground(GRAY);
        if(gui == 1){
        for(int x = 0;x<30;x++){
                for(int y = 0;y<30;y++){
                    if(WORLD[x][y] == WALL){
                            DrawRectangle((SCRW/WW)*x,(SCRH/WH)*y,(SCRW/WW),(SCRH/WH),BLACK);
                    }
                }
        }
        DrawRectangle((SCRW/WW)*(int)(mplr->pos.x/BLK_S),(SCRH/WH)*(int)(mplr->pos.y/BLK_S),(SCRW/WW),(SCRH/WH),BLACK);
        DrawCircle(mplr->pos.x,mplr->pos.y,BLK_S/4,GREEN);
        Vector2 tmp = Vector2Add(mplr->pos,Vector2Scale(mplr->dir,BLK_S));
        Vector2 cam = Vector2Add(tmp,Vector2Scale(mplr->cam,BLK_S));
                DrawLineV(tmp,
                          cam,
                         YELLOW);
        cam = Vector2Subtract(tmp,Vector2Scale(mplr->cam,BLK_S));
                DrawLineV(tmp,
                          cam,
                         YELLOW);
                DrawLine(mplr->pos.x,
                         mplr->pos.y,
                         mplr->pos.x  +  mplr->dir.x  *  BLK_S,
                         mplr->pos.y  +  mplr->dir.y  *  BLK_S,
                         YELLOW);
        }
        // RAY START:
            if(gui == 0){
            for (int x = 0;x <= SCRW;x++){
                int map_x = (int)(mplr->pos.x/WW);
                int map_y = (int)(mplr->pos.y/WH);
            double cam_x = 2 * x/(double)(SCRW) -1.0; //x cord on the camera plane
            Vector2 ray_dir = (Vector2){
                                   mplr->dir.x + mplr->cam.x * cam_x,
                                   mplr->dir.y + mplr->cam.y * cam_x,
                      };
            double delta_dist_x = (ray_dir.x == 0)  ? 2^8 : abs(1 / ray_dir.x );
            double delta_dist_y = (ray_dir.y == 0)  ? 2^8 : abs(1 / ray_dir.y );
            double dist_step_x = 0;
            double dist_step_y = 0;
            int step_x;
            int step_y;
            double wall_dist;
            int hit = 0;
            int side;
            if(ray_dir.x < 0){
                    step_x = -1;
                    dist_step_x = ((int)(mplr->pos.x) - map_x) * delta_dist_x;
            }
            else if (ray_dir.x > 0){
                step_x = 1;
                dist_step_x = (map_x + 1 - (int)(mplr->pos.x)) * delta_dist_x;
            }
            if(ray_dir.y < 0){
                    step_y = -1;
                    dist_step_y = ((int)(mplr->pos.y) - map_y) * delta_dist_y;
            }
            else if (ray_dir.y > 0){
                step_y = 1;
                dist_step_y = (map_y + 1 - (int)(mplr->pos.y)) * delta_dist_y;
            }
            while(hit == 0){
                    if(dist_step_x < dist_step_y){
                            dist_step_x += delta_dist_x;
                            map_x += step_x;
                            side = 0;
                    }
                    if(dist_step_x > dist_step_y){
                            dist_step_y += delta_dist_y;
                            map_y += step_y;
                            side = 1;
                    }
                    printf("%d %d | %d\n",map_x,map_y,WORLD[map_x][map_y]);
                    if(map_x < 0 || map_y < 0 || map_x > WW || map_y > WH) break;
                    if(WORLD[map_x][map_y] == WALL) hit = 1;
            }
            if (side == 0 ) wall_dist = (dist_step_x - delta_dist_x);
            if (side == 1 ) wall_dist = (dist_step_y - delta_dist_y);
            int line_h = (int)(SCRH/wall_dist);


            int draw_start = -line_h / 2 + SCRH / 2;
            if(draw_start < 0) draw_start = 0;
            int draw_end = line_h / 2 + SCRH / 2;
            if(draw_end >= SCRH) draw_end = SCRH - 1;
            Color color = BLACK;
            if(side == 1){
                    color = GRAY;
            }
            DrawLine(x,draw_end,x,draw_start,BLACK);
        }
        // RAY END:
            }

                
        EndDrawing();
        }   
}
Code:
[\code]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,326
Messages
2,571,720
Members
48,532
Latest member
StarValent

Latest Threads

Top