我一直在使用光线投射引擎,但是我似乎无法弄清为什么它只有在地图边缘时才能正确渲染
float px = 16/2;
float py = 16/2;
float pa = 0;
float fov = 3.14159 / 4.0f;
float depth = 16;
int mx = 16, my = 16;
std::wstring m_map;
void Start() override
{
m_map += L"################";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"#..............#";
m_map += L"################";
}
void Update() override
{
for (int x = 0;x < width;x++)
{
float rayAngle = (pa - fov / 2.0f) + ((float)x / (float)width)*fov;
float distToWall = 0.0f;
bool bhitwall = false;
float eyex = sinf(rayAngle);
float eyey = cosf(rayAngle);
if (!bhitwall && distToWall <= depth)
{
distToWall += 0.1f;
int ntestx = (int)(px + eyex * distToWall);
int ntesty = (int)(py + eyey * distToWall);
if (ntestx < 0 || ntestx >= mx || ntesty < 0 || ntesty >= my)
{
bhitwall = false;
distToWall = depth;
}
else
{
if (m_map[ntestx + ntesty] == L'#')
{
bhitwall = true;
}
}
}
float ceiling = (float)(height / 2.0f) -height / (float)distToWall;
float floor = height - ceiling;
int shade = 128;
if (distToWall < depth / 4.0) { shade = 128; }
else if (distToWall < depth / 3.0) { shade = 64; }
else if (distToWall < depth / 2.0f) { shade = 32; }
else if (distToWall < depth) { shade = 16; }
else { shade = 0; }
for (int y = 0;y < height;y++)
{
int gc = Color3(165, 42, 42);
int sc = Color3(0, 255, 0);
int wc = Color3(shade);
if (y <= ceiling)
{
pixels[y * width + x] = (int)sc;
}
else if (y > ceiling && y <= floor)
{
pixels[y * width + x] = (int)wc;
}
else
{
pixels[y * width + x] = (int)gc;
}
}
}
pa++;
发生的情况是,除非将播放器的x或y位置设置为16,否则如果我不将播放器的位置设置为16时,它的渲染效果几乎就像标准的光线投射引擎一样,那么屏幕只会填充墙壁的颜色,但是应该可以正确渲染在地图上的任何地方