1

一段时间以来,我一直在尝试为光线投射器制作 DDA 算法。出于某种原因,我遇到了很多麻烦。我的代码的工作方式是将玩家放入以恒定速率旋转的地图中。绿色圆圈应该落在玩家在最近的墙上看的任何地方。当线条更垂直而不是水平时,它似乎几乎可以工作,但它仍然有点偏离。当它通过 y=x 或 y=-x 线的那一刻,它变得疯狂,似乎射向无穷大。我怀疑它与坡度有关,但坦率地说我不太确定。

我一直在密切关注这个视频,但无济于事。我希望有人能在我的代码中发现错误,以便我可以继续这个项目。

这是我的代码的精简版本:

int[][] map = {
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};

float fov;
Player p;
void setup() {
    size(800, 600);
    background(0);
    stroke(255);
    
    fov = PI/2;
    p = new Player(12.5, 12.5, -PI/4);
}

void draw() {
    background(0);
    p.map_();
    p.ddaTest4();
    p.dir += PI/512;
    p.dir %= TWO_PI;
}

class Player {
    float dir;
    PVector pos = new PVector();
    

    Player (float x, float y, float dir) {
        this.dir = dir;
        this.pos.x = x;
        this.pos.y = y;
    }

    // this draws the map array as rectangles
    void map_() {
        noStroke();
        float sf = height/map.length;
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map.length; j++) {
                
                if (map[j][i] != 0) {
                    stroke(0);
                    fill(255);
                    rect(i*sf, j*sf, height/sf, height/sf);
                } else {
                    stroke(255);
                    fill(0);
                    rect(i*sf, j*sf, height/sf, height/sf);
                }
            }
        }
    }

    void ddaTest4() {
        float sf = height/map.length;
        PVector rayDir = PVector.fromAngle(dir);
        PVector stepScale  = new PVector(sqrt(((rayDir.y/rayDir.x) * (rayDir.y/rayDir.x)) + 1), sqrt(((rayDir.x/rayDir.y) * (rayDir.x/rayDir.y)) + 1));

        PVector mapPos = new PVector((int)pos.x, (int)pos.y);
        PVector rayLength = new PVector();

        PVector step = new PVector(1, 1);

        if (rayDir.x < 0) {
            step.x = -1;
            rayLength.x = (pos.x - mapPos.x) * stepScale.x;
        } else {
            rayLength.x = ((mapPos.x + 1) - mapPos.x) * stepScale.x;
        }
        if (rayDir.y < 0) {
            step.y = -1;
            rayLength.x = (pos.y - mapPos.y) * stepScale.y;
        } else {
            rayLength.x = ((mapPos.y + 1) - mapPos.y) * stepScale.y;
        }

        boolean hit = false;
        float distance = 0;
        while(!hit) {
            if (rayLength.x < rayLength.y) {
                mapPos.x += step.x;
                distance = rayLength.x;
                rayLength.x += stepScale.x;
            } else {
                mapPos.y += step.y;
                distance = rayLength.y;
                rayLength.y += stepScale.y;
            }

            if (map[(int)mapPos.y][(int)mapPos.x] != 0) {
                hit = true;
            }
        }

        PVector hitPoint = PVector.add(pos, PVector.mult(rayDir, distance));
        fill(0, 255, 0);
        stroke(0, 255, 0);
        line(pos.x*sf, pos.y*sf, hitPoint.x*sf, hitPoint.y*sf);
        ellipse(hitPoint.x*sf, hitPoint.y*sf, 5, 5);
    }
    
}

PS:对不起,如果我的问题表达得不好,我真的不知道该怎么说。

4

0 回答 0