有人可以帮我修复处理中的 DDA 碰撞算法吗?

问题描述

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

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

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

int[][] map = {
    {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,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,0);
        line(pos.x*sf,pos.y*sf,hitPoint.x*sf,hitPoint.y*sf);
        ellipse(hitPoint.x*sf,hitPoint.y*sf,5,5);
    }
    
}

PS:抱歉,如果我的问题措辞不当,我真的不知道该怎么说。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...