Skip to content

Commit

Permalink
camera: clamp interpolation result
Browse files Browse the repository at this point in the history
This performs bounds tests on the interpolated camera result to avoid
instances where black screens can occur.
  • Loading branch information
lahm86 committed Mar 1, 2025
1 parent a2552eb commit e986f74
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
32 changes: 31 additions & 1 deletion src/libtrx/game/camera/common.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "game/camera/common.h"

#include "game/camera/vars.h"
#include "game/rooms/const.h"
#include "game/pathing.h"
#include "game/rooms.h"
#include "utils.h"

static bool m_IsChunky = false;

Expand All @@ -19,3 +21,31 @@ void Camera_Reset(void)
{
g_Camera.pos.room_num = NO_ROOM;
}

void Camera_ClampInterpResult(void)
{
XYZ_32 *const pos = &g_Camera.interp.result.pos;
const int32_t shift = g_Camera.interp.result.shift;
const ROOM *const room = Room_Get(g_Camera.interp.room_num);
const SECTOR *sector = Room_GetWorldSector(room, pos->x, pos->z);
if (sector->box != NO_BOX) {
goto finish;
}

sector = Room_GetWorldSector(room, g_Camera.pos.x, g_Camera.pos.z);
if (sector->box == NO_BOX) {
goto finish;
}

const BOX_INFO *const box = Box_GetBox(sector->box);
CLAMP(pos->x, box->top, box->bottom);
CLAMP(pos->z, box->left, box->right);

finish:
const int32_t floor = Room_GetHeight(sector, pos->x, pos->y, pos->z);
const int32_t ceiling = Room_GetCeiling(sector, pos->x, pos->y, pos->z);
if (floor != NO_HEIGHT && ceiling != NO_HEIGHT) {
CLAMP(pos->y, ceiling - shift, floor - shift);
}
Room_GetSector(pos->x, pos->y + shift, pos->z, &g_Camera.interp.room_num);
}
7 changes: 2 additions & 5 deletions src/libtrx/game/interpolation.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "game/interpolation.h"

#include "config.h"
#include "game/camera/vars.h"
#include "game/camera.h"
#include "game/effects.h"
#include "game/lara/common.h"
#include "game/lara/hair.h"
Expand Down Expand Up @@ -178,10 +178,7 @@ void Interpolation_Commit(void)
}

g_Camera.interp.room_num = g_Camera.pos.room_num;
Room_GetSector(
g_Camera.interp.result.pos.x,
g_Camera.interp.result.pos.y + g_Camera.interp.result.shift,
g_Camera.interp.result.pos.z, &g_Camera.interp.room_num);
Camera_ClampInterpResult();
}

LARA_INFO *const lara = Lara_GetLaraInfo();
Expand Down
6 changes: 4 additions & 2 deletions src/libtrx/game/rooms/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,10 @@ BOUNDS_32 Room_GetWorldBounds(void)
SECTOR *Room_GetWorldSector(
const ROOM *const room, const int32_t x_pos, const int32_t z_pos)
{
const int32_t x_sector = (x_pos - room->pos.x) >> WALL_SHIFT;
const int32_t z_sector = (z_pos - room->pos.z) >> WALL_SHIFT;
int32_t x_sector = (x_pos - room->pos.x) >> WALL_SHIFT;
int32_t z_sector = (z_pos - room->pos.z) >> WALL_SHIFT;
CLAMP(x_sector, 0, room->size.x - 1);
CLAMP(z_sector, 0, room->size.z - 1);
return Room_GetUnitSector(room, x_sector, z_sector);
}

Expand Down
1 change: 1 addition & 0 deletions src/libtrx/include/libtrx/game/camera/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ extern void Camera_Apply(void);
bool Camera_IsChunky(void);
void Camera_SetChunky(bool is_chunky);
void Camera_Reset(void);
void Camera_ClampInterpResult(void);

0 comments on commit e986f74

Please sign in to comment.