raycasting almost working

This commit is contained in:
Cuptain 2024-09-01 19:56:59 +10:00
parent 9ea6ce4023
commit a8eb642e9b

View File

@ -15,6 +15,7 @@
#define TILE_FILLED 1 #define TILE_FILLED 1
#define MAP_WIDTH 10 #define MAP_WIDTH 10
#define MAP_HEIGHT 10 #define MAP_HEIGHT 10
#define WALL_HEIGHT_SCALE FIX16(0.5)
Line l; Line l;
s16 angle = 0; s16 angle = 0;
@ -38,7 +39,7 @@ int dist(int ax, int ay, int bx, int by){
return (ax-bx)*(ax-bx) + (ay-by)*(ay-by); return (ax-bx)*(ax-bx) + (ay-by)*(ay-by);
} }
void castRay(s16 angle){ void castRay(s16 angle, s16 column){
if(angle >= 360) angle = angle - 360; if(angle >= 360) angle = angle - 360;
if(angle < 0) angle = 360 + angle; if(angle < 0) angle = 360 + angle;
u16 r,mx,my,mp,dof; u16 r,mx,my,mp,dof;
@ -140,7 +141,9 @@ void castRay(s16 angle){
dof += 1; dof += 1;
} }
} }
fix16 shortestDist = distH;
if(distV < distH){ if(distV < distH){
shortestDist = distV;
l.pt2.x = fix16ToInt(vx); l.pt2.x = fix16ToInt(vx);
l.pt2.y = fix16ToInt(vy); l.pt2.y = fix16ToInt(vy);
}else { }else {
@ -148,7 +151,21 @@ void castRay(s16 angle){
l.pt2.y = fix16ToInt(hy); l.pt2.y = fix16ToInt(hy);
} }
//Wall height calcs
fix16 wallHeightFix16 = fix16Div(FIX16(SCREEN_HEIGHT), (shortestDist + FIX16(1)));
wallHeightFix16 = fix16Mul(wallHeightFix16, WALL_HEIGHT_SCALE);
int wallHeight = fix16ToInt(wallHeightFix16);
int wallTop = (SCREEN_HEIGHT / 2) - (wallHeight / 2);
int wallBottom = (SCREEN_HEIGHT / 2) + (wallHeight / 2);
if (wallTop < 0) wallTop = 0;
if (wallBottom >= SCREEN_HEIGHT) wallBottom = SCREEN_HEIGHT - 1;
u16 tileTop = wallTop / TILE_HEIGHT;
u16 tileBottom = wallBottom / TILE_HEIGHT;
u16 tileHeight = tileBottom - tileTop + 1;
for (int yTile = tileTop; yTile <= tileBottom; yTile++) {
VDP_setTileMapXY(BG_B, TILE_ATTR_FULL(PAL1, 0, FALSE, FALSE, TILE_FILLED), column, yTile);
}
} }
@ -200,14 +217,23 @@ void drawMap() {
void render() { void render() {
clearScreenWithTile(TILE_EMPTY); clearScreenWithTile(TILE_EMPTY);
drawMap(); s16 castStart = angle - 20;
if(angle >= 360) angle = angle - 360;
if(angle < 0) angle = 360 + angle;
for(int i = 0; i < 40; i++){
castRay(castStart + i, i);
}
//drawMap();
VDP_setTextPlane(BG_B); VDP_setTextPlane(BG_B);
VDP_setTextPalette(2); VDP_setTextPalette(2);
VDP_showFPS(1); VDP_showFPS(1);
s16 castStart = angle - 20; char debugCastText[20];
for(int i = 0; i < 40; i++){ sprintf(debugCastText, "Cast: %d", castStart);
castRay(castStart + i); VDP_drawText(debugCastText, 20, 2);
} angle = angle + 1;
SYS_doVBlankProcess(); SYS_doVBlankProcess();
} }