raycaster_fixed.cpp
// (playerX, playerY) is 8 box coordinate bits, 8 inside coordinate bits
// (playerA) is full circle as 1024
(playerX, playerY) is 8 box coordinate bits, 8 inside coordinate bits
(playerA) is full circle as 1024
( playerX playerY ) is 8 box coordinate bits 8 inside coordinate bits ( playerA ) is full circle as 1024
RayCasterFixed::TraceResult RayCasterFixed::Trace(uint16_t screenX)
{
    TraceResult res;
    uint16_t rayAngle = static_cast<uint16_t>(_playerA + g_deltaAngle[screenX]);
    switch (rayAngle % 256) {
    case 1:
    case 254:
        rayAngle--;
        break;
    case 2:
    case 255:
        rayAngle++;
        break;
    }
    rayAngle %= 1024;
    int16_t deltaX;
    int16_t deltaY;
    CalculateDistance(_playerX, _playerY, rayAngle, &deltaX, &deltaY,
                      &res.textureNo, &res.textureX);
    int16_t distance = 0;
    if (_playerA == 0) {
        distance += deltaY;
    } else if (_playerA == 512) {
        distance -= deltaY;
    } else
        switch (_viewQuarter) {
        case 0:
            distance += MulS(g_cos[_viewAngle], deltaY);
            break;
        case 1:
            distance -= MulS(g_cos[INVERT(_viewAngle)], deltaY);
            break;
        case 2:
            distance -= MulS(g_cos[_viewAngle], deltaY);
            break;
        case 3:
            distance += MulS(g_cos[INVERT(_viewAngle)], deltaY);
            break;
        }
    if (_playerA == 256) {
        distance += deltaX;
    } else if (_playerA == 768) {
        distance -= deltaX;
    } else
        switch (_viewQuarter) {
        case 0:
            distance += MulS(g_sin[_viewAngle], deltaX);
            break;
        case 1:
            distance += MulS(g_sin[INVERT(_viewAngle)], deltaX);
            break;
        case 2:
            distance -= MulS(g_sin[_viewAngle], deltaX);
            break;
        case 3:
            distance -= MulS(g_sin[INVERT(_viewAngle)], deltaX);
            break;
        }
    if (distance >= MIN_DIST) {
        res.textureY = 0;
        LookupHeight((distance - MIN_DIST) >> 2, &res.screenY,
                     &res.textureStep);
    } else {
        res.screenY = SCREEN_HEIGHT >> 1;
        res.textureY = g_overflowOffset[distance];
        res.textureStep = g_overflowStep[distance];
    }
    return res;
}
RayCasterFixed :: TraceResult RayCasterFixed :: Trace ( uint16_t screenX ) { TraceResult res ; uint16_t rayAngle = static_cast < uint16_t > ( _playerA + g_deltaAngle [ screenX ] ) ; switch ( rayAngle % 256 ) { case 1 : case 254 : rayAngle -- ; break ; case 2 : case 255 : rayAngle ++ ; break ; } rayAngle %= 1024 ; int16_t deltaX ; int16_t deltaY ; CalculateDistance ( _playerX , _playerY , rayAngle , & deltaX , & deltaY , & res . textureNo , & res . textureX ) ; int16_t distance = 0 ; if ( _playerA == 0 ) { distance += deltaY ; } else if ( _playerA == 512 ) { distance -= deltaY ; } else switch ( _viewQuarter ) { case 0 : distance += MulS ( g_cos [ _viewAngle ] , deltaY ) ; break ; case 1 : distance -= MulS ( g_cos [ INVERT ( _viewAngle ) ] , deltaY ) ; break ; case 2 : distance -= MulS ( g_cos [ _viewAngle ] , deltaY ) ; break ; case 3 : distance += MulS ( g_cos [ INVERT ( _viewAngle ) ] , deltaY ) ; break ; } if ( _playerA == 256 ) { distance += deltaX ; } else if ( _playerA == 768 ) { distance -= deltaX ; } else switch ( _viewQuarter ) { case 0 : distance += MulS ( g_sin [ _viewAngle ] , deltaX ) ; break ; case 1 : distance += MulS ( g_sin [ INVERT ( _viewAngle ) ] , deltaX ) ; break ; case 2 : distance -= MulS ( g_sin [ _viewAngle ] , deltaX ) ; break ; case 3 : distance -= MulS ( g_sin [ INVERT ( _viewAngle ) ] , deltaX ) ; break ; } if ( distance >= MIN_DIST ) { res . textureY = 0 ; LookupHeight ( ( distance - MIN_DIST ) >> 2 , & res . screenY , & res . textureStep ) ; } else { res . screenY = SCREEN_HEIGHT >> 1 ; res . textureY = g_overflowOffset [ distance ] ; res . textureStep = g_overflowStep [ distance ] ; } return res ; }
(playerX, playerY) is 8 box coordinate bits, 8 inside coordinate bits
(playerA) is full circle as 1024
( playerX playerY ) is 8 box coordinate bits 8 inside coordinate bits ( playerA ) is full circle as 1024
// neutralize artefacts around edges // distance = deltaY * cos(playerA) + deltaX * sin(playerA)