如何解决如何修复我的 raycaster 中的翘曲墙? 我曾尝试使用余弦校正
我知道这里有许多已回答的问题问了完全相同的问题。但是,我尝试实施该解决方案(余弦校正)并且收效甚微。注意:在这个版本的代码中没有实现。
就我的代码而言,我将上传整个内容,但可能需要修复的部分是 Player 类。剩下的就是相当无聊的东西。我也知道它是基于你没有的图像运行的,但是为了测试,可以随意使用任何只有黑白的 600x600 图像。或者我的联系我。白色表示墙壁。我还将上传扭曲墙的屏幕截图。控件是用于移动的 WASD,加上一些小故障的鼠标控件来更改视图(将被修复)以及按住 m 以查看当前级别的地图。 Screenshot of it running
PImage maze;
int stepSize,renderdistance;
float viewStep,fov,moveSpeed;
float minLine,maxLine;
Player p;
boolean w,s,a,d,map;
void setup() {
size(600,600);
background(0);
strokeWeight(3);
maze = loadImage("testmaze3.png");
stepSize = 1;
fov = PI/4;
viewStep = fov/(width/4);
renderdistance = 300;
moveSpeed = 2;
minLine = 0;
maxLine = 200;
colorMode(RGB,renderdistance);
p = new Player(width/2,height/2,0);
w = false;
s = false;
a = false;
d = false;
}
void draw() {
maze.loadPixels();
if (map) {
pushStyle();
background(maze);
fill(255,0);
nostroke();
ellipse(p.x,p.y,10,10);
stroke(renderdistance);
pushmatrix();
translate(p.x,p.y);
line(0,renderdistance*cos(p.direction),renderdistance*sin(p.direction));
popMatrix();
popStyle();
}
pushmatrix();
translate(p.x,p.y);
if (w) {
if (maze.get(int(p.x + moveSpeed*cos(p.direction)),int(p.y + moveSpeed*sin(p.direction))) != color(renderdistance)) {
p.x += moveSpeed*cos(p.direction);
p.y += moveSpeed*sin(p.direction);
}
}
if (s) {
if (maze.get(int(p.x - moveSpeed*cos(p.direction)),int(p.y - moveSpeed*sin(p.direction))) != color(renderdistance)) {
p.x -= moveSpeed*cos(p.direction);
p.y -= moveSpeed*sin(p.direction);
}
}
if (d) {
if (maze.get(int(p.x - moveSpeed*cos(p.direction-PI/2)),int(p.y - moveSpeed*sin(p.direction-PI/2))) != color(renderdistance)) {
p.x -= moveSpeed*cos(p.direction-PI/2);
p.y -= moveSpeed*sin(p.direction-PI/2);
}
}
if (a) {
if (maze.get(int(p.x - moveSpeed*cos(p.direction+PI/2)),int(p.y - moveSpeed*sin(p.direction+PI/2))) != color(renderdistance)) {
p.x -= moveSpeed*cos(p.direction+PI/2);
p.y -= moveSpeed*sin(p.direction+PI/2);
}
}
popMatrix();
p.direction += ((float)mouseX-(float)pmouseX)/100;
if (!map) {
pushStyle();
nostroke();
fill(0,renderdistance);
rect(0,width,height/2);
fill(0,renderdistance*0.6);
rect(0,height);
popStyle();
p.display();
}
}
void keypressed() {
if (key == 'w') {
w = true;
}
if (key == 's') {
s = true;
}
if (key == 'a') {
a = true;
}
if (key == 'd') {
d = true;
}
if (key == 'm') {
map = true;
}
}
void keyreleased() {
if (key == 'w') {
w = false;
}
if (key == 's') {
s = false;
}
if (key == 'a') {
a = false;
}
if (key == 'd') {
d = false;
}
if (key == 'm') {
map = false;
}
}
class Player {
float x,y,direction;
Player(int x,int y,float direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
void display() {
maze.loadPixels();
for (float i = direction - fov; i < direction + fov; i += viewStep) {
FloatList line = new FloatList();
line = rayCast(i);
float length_ = (height - maxLine - minLine - map(line.get(2),renderdistance,minLine,maxLine));
stroke(renderdistance - line.get(2),0);
if (line.get(3) == 1){
stroke(renderdistance - line.get(2));
}
if (renderdistance - line.get(2) > 1) {
line(map(i,direction - fov/2,direction + fov,width),height/2 + length_/2,map(i,height/2 - length_/2);
}
}
}
FloatList rayCast(float direction) {
FloatList out = new FloatList();
float rayx = x;
float rayy = y;
int steps = 0;
for (int i = 0; i < renderdistance; i++) {
if (maze.get(int(rayx),int(rayy)) != color(0)) {
break;
}
rayx += stepSize * cos(direction);
rayy += stepSize * sin(direction);
steps++;
}
out.append(rayx);
out.append(rayy);
out.append(steps);
if (rayx < 0 || rayx > width || rayy > height || rayy < 0) {
out.append(1);
} else {
out.append(0);
}
return out;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。