Average rotations

This commit is contained in:
Miloslav Ciz 2024-09-22 20:19:43 +02:00
parent b181256e58
commit c0aa81e79e
2 changed files with 49 additions and 32 deletions

View file

@ -54,6 +54,7 @@ struct
TPE_Vec3 carPositions[2]; ///* Current and previous position.
TPE_Vec3 carRotations[2]; ///* Current and previous rotation.
LCR_GameUnit wheelRotation;
LCR_GameUnit wheelSteer;
@ -95,6 +96,21 @@ uint8_t _LCR_racingCollisionHandler(uint16_t b1, uint16_t j1, uint16_t b2,
return 1;
}
LCR_GameUnit _LCR_racingSmoothRot(LCR_GameUnit angleNew, LCR_GameUnit angleOld)
{
/* We'll smooth small rotations by averaging the last two angles; bigger
rotations won't be smoothed -- firstly this removes lag for fast rotations
and also deals with the issue of averaging e.g. 1 and 359 degrees. */
LCR_GameUnit diff = angleNew - angleOld;
if (diff > LCR_GAME_UNIT / 8 ||
diff < -1 * LCR_GAME_UNIT / 8)
return angleNew;
return angleOld + diff / 2;
}
/**
Initializes new run.
*/
@ -108,6 +124,11 @@ void LCR_racingRestart(void)
LCR_racing.wheelRotation = 0;
LCR_racing.wheelSteer = 0;
LCR_racing.carPositions[0] = TPE_vec3(0,0,0);
LCR_racing.carPositions[1] = LCR_racing.carPositions[0];
LCR_racing.carRotations[0] = TPE_vec3(0,0,0);
LCR_racing.carRotations[1] = LCR_racing.carRotations[0];
// TODO
}
@ -146,6 +167,11 @@ void LCR_racingInit(void)
LCR_racingRestart();
}
/**
Gets current car transformation intended for rendering, i.e. potentially with
smoothing and interpolation applied to the underlying internal state in the
physics engine.
*/
void LCR_racingGetCarTransform(LCR_GameUnit position[3],
LCR_GameUnit rotation[3], LCR_GameUnit interpolationParam)
{
@ -163,17 +189,24 @@ void LCR_racingGetCarTransform(LCR_GameUnit position[3],
position[0] = v.x;
position[1] = v.y;
position[2] = v.z;
rotation[0] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].x,
LCR_racing.carRotations[1].x);
rotation[1] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].y,
LCR_racing.carRotations[1].y);
rotation[2] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].z,
LCR_racing.carRotations[1].z);
#else
position[0] = LCR_racing.carPositions[0].x;
position[1] = LCR_racing.carPositions[0].y;
position[2] = LCR_racing.carPositions[0].z;
rotation[0] = LCR_racing.carRotations[0].x;
rotation[1] = LCR_racing.carRotations[0].y;
rotation[2] = LCR_racing.carRotations[0].z;
#endif
v = TPE_bodyGetRotation(&(LCR_racing.carBody),0,2,1);
rotation[0] = (v.x * LCR_GAME_UNIT) / TPE_F;
rotation[1] = (v.y * LCR_GAME_UNIT) / TPE_F;
rotation[2] = (v.z * LCR_GAME_UNIT) / TPE_F;
}
void _LCR_drawPhysicsDebugPixel(uint16_t x, uint16_t y, uint8_t color)
@ -347,7 +380,6 @@ void LCR_racingStep(unsigned int input)
TPE_Vec3 tmpVec = LCR_racing.carPositions[0];
LCR_racing.carPositions[0] =
_LCR_TPE_vec3DividePlain(
TPE_vec3TimesPlain(
@ -374,6 +406,13 @@ LCR_racing.carPositions[0] =
LCR_racing.carPositions[1] = tmpVec;
tmpVec = _LCR_TPE_vec3DividePlain(TPE_vec3TimesPlain(
TPE_bodyGetRotation(&(LCR_racing.carBody),0,2,1),
LCR_GAME_UNIT),TPE_F);
LCR_racing.carRotations[1] = LCR_racing.carRotations[0];
LCR_racing.carRotations[0] = tmpVec;
LCR_racing.tick++;
}