Fix some camera bugs

This commit is contained in:
Miloslav Ciz 2024-09-11 00:26:42 +02:00
parent b6d28c2886
commit ffbe827f22
2 changed files with 42 additions and 7 deletions

5
game.h
View file

@ -170,9 +170,14 @@ uint8_t LCR_gameStep(uint32_t time)
LCR_keyStates[i] = LCR_keyPressed(i) ?
(LCR_keyStates[i] < 255 ? LCR_keyStates[i] + 1 : 255) : 0;
//S3L_logTransform3D(LCR_renderer.scene.camera.transform);
if ((LCR_racing.tick % 32) == 0)
{
printf("speed: %d\n",LCR_racingGetCarSpeed());
}
uint32_t sleep = 0;
if (LCR_keyStates[LCR_KEY_B] == 1)

View file

@ -898,6 +898,14 @@ void LCR_rendererMoveCamera(LCR_GameUnit forwRightUpOffset[3],
((f.z * forwRightUpOffset[0] + r.z * forwRightUpOffset[1] +
u.z * forwRightUpOffset[2]) * S3L_F) / LCR_GAME_UNIT;
LCR_renderer.scene.camera.transform.rotation.y +=
(yawPitchOffset[0] * S3L_F) / LCR_GAME_UNIT;
LCR_renderer.scene.camera.transform.rotation.x +=
(yawPitchOffset[1] * S3L_F) / LCR_GAME_UNIT;
/*
LCR_renderer.scene.camera.transform.rotation.y = S3L_wrap(
LCR_renderer.scene.camera.transform.rotation.y +
(yawPitchOffset[0] * S3L_F) / LCR_GAME_UNIT,
@ -907,6 +915,8 @@ void LCR_rendererMoveCamera(LCR_GameUnit forwRightUpOffset[3],
LCR_renderer.scene.camera.transform.rotation.x +
(yawPitchOffset[1] * S3L_F) / LCR_GAME_UNIT,
-1 * S3L_F / 4,S3L_F / 4);
*/
#define chk(o,c,l) \
if (LCR_renderer.scene.camera.transform.translation.c o l) \
@ -1189,6 +1199,9 @@ void _LCR_rendererLoadMapChunk(uint8_t chunk, int8_t x, int8_t y, int8_t z)
S3L_Unit _LCR_smoothRotation(S3L_Unit angleOld, S3L_Unit angleNew,
unsigned int amount)
{
// angleOld = S3L_wrap(angleOld,S3L_F);
// angleNew = S3L_wrap(angleNew,S3L_F);
S3L_Unit angleDiff = angleNew - angleOld;
if (angleDiff == 0)
@ -1198,16 +1211,17 @@ S3L_Unit _LCR_smoothRotation(S3L_Unit angleOld, S3L_Unit angleNew,
if (angleDiffAbs > S3L_F / 2) // consider e.g. 350 degrees minus 1 degree
{
angleDiffAbs -= S3L_F / 2;
angleDiff += (angleDiff > 0) ? -1 * (S3L_F / 2) : (S3L_F / 2);
angleDiffAbs = S3L_F - angleDiffAbs;
angleDiff = (angleDiff > 0) ? -1 * angleDiffAbs : angleDiffAbs;
}
if (angleDiffAbs > (3 * S3L_F) / 8) // angle too big, rotate immediately
return angleNew;
// if (angleDiffAbs > (3 * S3L_F) / 8) // angle too big, rotate immediately
// return angleNew;
/*
if (angleDiffAbs < S3L_F / 32)
return angleOld;
printf("%d %d %d\n",angleOld,angleNew,
angleOld + (angleDiff >> amount)
);
*/
return angleOld + (angleDiff >> amount); // smoothly interpolate
@ -1439,6 +1453,15 @@ void LCR_rendererCameraFollow(void)
LCR_renderer.scene.camera.transform.rotation.x = _LCR_smoothRotation(
transPrev.rotation.x,LCR_renderer.scene.camera.transform.rotation.x,2);
/*
printf("aaa %d %d %d\n",
transPrev.rotation.y,
LCR_renderer.scene.camera.transform.rotation.y,
_LCR_smoothRotation(
transPrev.rotation.y,LCR_renderer.scene.camera.transform.rotation.y,3)
);
*/
LCR_renderer.scene.camera.transform.rotation.y = _LCR_smoothRotation(
transPrev.rotation.y,LCR_renderer.scene.camera.transform.rotation.y,3);
#endif
@ -1454,6 +1477,13 @@ void LCR_rendererSetWheelState(LCR_GameUnit rotation, LCR_GameUnit steer)
void LCR_rendererDraw(void)
{
// first make sure rotations are in correct range:
LCR_renderer.scene.camera.transform.rotation.y = S3L_wrap(
LCR_renderer.scene.camera.transform.rotation.y, S3L_F);
LCR_renderer.scene.camera.transform.rotation.x = S3L_clamp(
LCR_renderer.scene.camera.transform.rotation.x,-1 * S3L_F / 4,S3L_F / 4);
LCR_renderer.previousTriID = -1;
S3L_newFrame();