【问题标题】:C: Raycaster not workingC:光线投射器不工作
【发布时间】:2015-08-05 23:46:09
【问题描述】:

我试图用 C 语言制作一个在终端中运行的 ascii raycaster。我认为以前没有做过类似的事情。它似乎没有正确打印帧。希望有人能告诉我问题是什么。这是我的代码:https://ghostbin.com/paste/9vxmz 我期待程序以第一人称显示迷宫(存储在地图数组中)。我还没有实施控制。基本上,有一个二维字符数组,它是终端屏幕的大小。该数组应该每秒打印一次。屏幕数组从 raycaster 获取其值。我不确切知道问题出在哪里,但这并不是一个大程序。请帮助我对 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;
}

【问题讨论】:

  • 您可以将代码添加到问题中吗?我什至无法查看链接。
  • 我将代码添加到问题中。

标签: c 3d terminal ascii raycasting


【解决方案1】:

fflush(stdout);在你的框架打印后嵌套fors

【讨论】:

  • 尝试单帧。你能让它正确渲染吗?
  • 我不能。它只显示随机字符,但每帧显示相同的字符。框架甚至不是终端的全尺寸。
  • 你的输出中有错误for (iy=0; iy&lt;ROWS; iy++) for (ix=0; ix&lt;COLS; ix++) printf("%c", screen[ix][iy]);你应该使用screen[iy][ix]
  • 这肯定有帮助,但我仍然在迷宫中看不到任何东西。屏幕的上半部分是应该是墙壁的字符,而下半部分则什么都没有。
  • 如果你将它设置为只执行一帧,但将程序的输出重定向到一个文本文件,例如myProg &gt; testout.txt
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
  • 2019-11-18
  • 1970-01-01
相关资源
最近更新 更多