C:Raycaster无法正常工作

时间:2015-08-05 23:46:10

标签: c 3d terminal ascii raycasting

我试图在终端中运行的C中制作一个ascii raycaster。我不认为以前做过类似的事情。它似乎没有正确打印帧。希望有人可以告诉我问题是什么。这是我的代码:https://ghostbin.com/paste/9vxmz 我期待程序在第一人称中显示迷宫(存储在地图数组中)。我还没有实现控制。基本上,有一个二维char数组,它是终端屏幕的大小。这个数组应该每秒打印一次。屏幕数组从raycaster获取其值。我不确切地知道问题的确切位置,但它并不是一个大的计划。 Plz帮助我对C完全陌生。

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <math.h>
#include <unistd.h>
int getrows();
int getcols();
double diagnalMovementX(double x, double angle);
double diagnalMovementY(double y, double angle);
#define ROWS getrows()              // the amount of horisontal rows in the terminal
#define COLS getcols()              // the amount of verticle columns in the terminal
#define PI 3.14159265358979         // defines pi
#define TAU 2*PI                // makes things easier because TAU is 360 degrees in radians
#define DTOR PI/180             // multiply by this to convert degrees to radians
#define RTOD 180/PI             // multiply by this to convert radians to degrees
int currentframe=0;             // the current frame
const int map[13][12]={             // the actual map
    {1,1,0,1,1,1,1,1,1,1,1,1},
    {1,0,0,0,1,0,0,1,0,0,0,1},
    {1,1,1,0,1,1,0,1,0,1,0,1},
    {1,0,1,0,0,0,0,1,0,1,0,1},
    {1,0,1,1,1,1,0,1,1,1,0,1},
    {1,0,0,0,0,0,0,1,0,0,0,1},
    {1,0,1,0,1,1,1,1,0,1,1,1},
    {1,0,1,0,0,0,0,0,0,0,0,1},
    {1,0,1,1,1,1,1,1,1,1,0,1},
    {1,0,1,0,0,0,0,0,0,0,0,1},
    {1,0,1,1,1,1,1,0,1,1,1,1},
    {1,0,0,0,1,0,0,0,0,0,0,1},
    {1,1,1,1,1,0,1,1,1,1,1,1}
};
int main() {
    char screen[COLS][ROWS];        // this array is modified and printed every frame
    double posX=2, posY=0;          // position for the player
    double direction=0*DTOR;        // direction of the player
    double fov=0*DTOR;          // the feild of vision
    double angleBetweenEachRay=fov/COLS;    // the angle between each ray
    double rayX, rayY;          // the position of the end of the ray
    double rayAngle;            // the angle that the ray travels
    double distanceToWall;          // the distance the player is away from a wall
    int lineHeight;             // how many columns the wall should use on the screen
    int sizeOfBlankSpaces;          // the size that the ceiling and floor should appear on the screen
    int thisIntPreventsCrashes=0;       // prevents a ray from traveling endlessly

    // gameloop starts here
    while (1) {
        // this allows movement
        /* NOT IMPLEMENTED YET */
        // this loop generates the columns of the frame
        for (int i=0; i<COLS; i++) {
            // calculate the ray angle
            rayAngle=direction-fov/2-angleBetweenEachRay+angleBetweenEachRay*(i+1);
            // fix the ray angle if it is not a proper value
            if (rayAngle>TAU) {
                rayAngle-=TAU;
            }
            else if (rayAngle<0) {
                rayAngle+=TAU;
            }
            // makes the ray start from the player
            rayX=posX;
            rayY=posY;
            // increases the ray size
            rayX=diagnalMovementX(rayX, rayAngle);
            rayY=diagnalMovementY(rayY, rayAngle);
            // this code prevents the ray from traveling endlessly and the game from freezing if there is no wall for the ray to hit
            thisIntPreventsCrashes++;
            if (thisIntPreventsCrashes>10) {
                lineHeight=0;
            }
            // checks if the ray hit a wall
            if (map[(int) rayY][(int) rayX]==1) {
                // calculates the distance to the wall using the distance formula
                distanceToWall=sqrt(pow((rayX-posX), 2)+pow((rayY-posY), 2));
                // calculates the size the wall should appear onscreen
                lineHeight=ROWS/(10*distanceToWall);
            }
            // shows X when there is no wall
            if (lineHeight==0) {
                for (int ii=0; ii<ROWS; ii++) {
                    screen[i][ii]='X';
                }
            }
            else {
                // calculates the ceiling and floor size
                sizeOfBlankSpaces=(ROWS-lineHeight)/2;
                // ceiling
                for(int ii=0; ii<sizeOfBlankSpaces; ii++) {
                    screen[i][ii]=' ';
                }
                // walls
                for(int ii=sizeOfBlankSpaces; ii<sizeOfBlankSpaces+lineHeight; ii++) {
                    screen[i][ii]='';
                }
                // floor
                for(int ii=sizeOfBlankSpaces+lineHeight; ii<ROWS; ii++) {
                    screen[i][ii]=' ';
                }
            }
        }
        // this sets the framerate
        sleep(1);
        // this clears the current frame
        fflush(stdout);
        //this code prints a new frame
        for (int iy=0; iy<ROWS; iy++) {
            for (int ix=0; ix<COLS; ix++) {
                printf("%c", screen[iy][ix]);
            }
            printf("\n");
        }
        currentframe++;
    }
    return 0;
}
int getrows() {
    struct winsize w;
    ioctl(0, TIOCGWINSZ, &w);
    return w.ws_row;
}
int getcols() {
    struct winsize w;
    ioctl(0, TIOCGWINSZ, &w);
    return w.ws_col;
}
double diagnalMovementX(double x, double angle) {
    if (angle<TAU && angle>0.5*TAU) {
        x-=0.5*cos(angle);
    }
    else if (angle>0 && angle<0.5*TAU) {
        x+=0.5*cos(angle);
    }
    return x;
}
double diagnalMovementY(double y, double angle) {
    if (angle<TAU && angle>0.5*TAU) {
        if (angle<0.75*TAU) {
            y-=0.5*sin(angle);
        }
        else if (angle>0.75*TAU) {
            y+=0.5*sin(angle);
        }
    }
    else if (angle>0 && angle<0.5*TAU) {
        if (angle<0.25*TAU) {
            y+=0.5*sin(angle);
        }
        else if (angle>0.25*TAU) {
            y-=0.5*sin(angle);
        }
    }
    else if (angle==0 || angle==TAU) {
        y+=0.5;
    }
    else if (angle==0.5*TAU) {
        y-=0.5;
    }
    return y;
}

1 个答案:

答案 0 :(得分:0)

在您的帧打印嵌套fflush(stdout); s

之后

for

相关问题