Continue HUD

This commit is contained in:
Miloslav Ciz 2024-12-18 00:18:31 +01:00
parent b177e924dd
commit 7c3eac7d9f
4 changed files with 117 additions and 32 deletions

55
game.h
View file

@ -126,13 +126,13 @@ struct
{
uint8_t state;
uint32_t stateStartTime;
uint32_t time;
uint32_t nextRenderFrameTime;
uint32_t nextRacingTickTime;
uint8_t controlMode;
uint8_t debugDraw;
uint8_t checkpointsTaken;
uint32_t runTime;
} LCR_game;
void LCR_drawPixelXYUnsafe(unsigned int x, unsigned int y,
@ -188,14 +188,22 @@ LCR_GameUnit LCR_carSpeedKMH(void)
void LCR_gameResetRun(void)
{
LCR_GameUnit carTransform[6];
LCR_LOG0("resetting run");
LCR_game.checkpointsTaken = 0;
LCR_mapReset();
LCR_racingRestart();
LCR_rendererUnmarkCPs();
LCR_racingGetCarTransform(carTransform,carTransform + 3,0);
LCR_rendererSetCarTransform(carTransform,carTransform + 3);
LCR_rendererCameraReset();
LCR_gameSetState(LCR_GAME_STATE_RUN_STARTING);
LCR_game.runTime = 0;
}
//void LCR_gameStartRun(const uint8_t *map)
void LCR_gameStartRun(const char *mapStr)
{
LCR_mapLoadFromStr(mapStr);
@ -218,7 +226,6 @@ void LCR_gameInit(void)
LCR_game.nextRacingTickTime = 0;
LCR_game.controlMode = LCR_CONTROL_MODE_FREECAM;
LCR_game.debugDraw = 0;
LCR_gameStartRun(LCR_maps[0]);
}
@ -228,6 +235,11 @@ void LCR_gameEnd(void)
LCR_LOG0("ending");
}
void LCR_gameUpdateRendererCarTransform(void)
{
}
uint8_t LCR_gameStep(uint32_t time)
{
LCR_LOG2("game step start");
@ -246,9 +258,7 @@ uint8_t LCR_gameStep(uint32_t time)
LCR_game.controlMode = LCR_game.controlMode == LCR_CONTROL_MODE_FREECAM ?
LCR_CONTROL_MODE_DRIVE : LCR_CONTROL_MODE_FREECAM;
else if (LCR_keyStates[LCR_KEY_B] == 30)
LCR_racingRestart();
else if (LCR_keyStates[LCR_KEY_B] == 60)
LCR_game.debugDraw = !LCR_game.debugDraw;
LCR_gameResetRun();
while (time >= LCR_game.nextRacingTickTime)
{
@ -294,11 +304,11 @@ uint8_t LCR_gameStep(uint32_t time)
{
LCR_LOG1("finished");
// TODO
}
}
LCR_game.runTime++;
LCR_game.nextRacingTickTime += LCR_RACING_TICK_MS;
}
@ -382,23 +392,26 @@ LCR_GameUnit physicsInterpolationParam = LCR_GAME_UNIT -
LCR_EFFECTIVE_RESOLUTION_Y -
LCR_rendererComputeTextHeight(2) - 20,0,2);
str[0] = '1'; // TODO
str[1] = '2';
str[2] = '\'';
str[3] = '3';
str[4] = '4';
str[5] = 0;
val = ((LCR_game.runTime * LCR_RACING_TICK_MS)) / 1000; // seconds
LCR_rendererDrawText(str,20,
LCR_EFFECTIVE_RESOLUTION_Y -
str[3] = '0' + (val % 60) / 10;
str[4] = '0' + val % 10;
val = (val / 60) % 100; // minutes
str[0] = '0' + val / 10;
str[1] = '0' + val % 10;
str[2] = '\'';
str[5] = 0;
LCR_rendererDrawText(str,20,LCR_EFFECTIVE_RESOLUTION_Y -
LCR_rendererComputeTextHeight(2) - 20,0,2);
if (LCR_game.debugDraw)
{
LCR_GameUnit camTr[7];
LCR_rendererGetCameraTransform(camTr,camTr + 3,camTr + 6);
LCR_physicsDebugDraw(camTr,camTr + 3,camTr[6]);
}
#if LCR_SETTING_DEBUG_PHYSICS_DRAW
LCR_GameUnit camTr[7];
LCR_rendererGetCameraTransform(camTr,camTr + 3,camTr + 6);
LCR_physicsDebugDraw(camTr,camTr + 3,camTr[6]);
#endif
}
else
{

View file

@ -512,6 +512,39 @@ LCR_GameUnit _LCR_racingSmoothRot(LCR_GameUnit angleNew, LCR_GameUnit angleOld)
return angleOld + diff / 2;
}
void _LCR_racingUpdateCarPosRot(void)
{
TPE_Vec3 tmpVec = LCR_racing.carPositions[0];
TPE_Vec3 wheelAverage = _LCR_TPE_vec3DividePlain(
TPE_vec3Plus(
TPE_vec3Plus(
LCR_racing.carBody.joints[0].position,
LCR_racing.carBody.joints[1].position),
TPE_vec3Plus(
LCR_racing.carBody.joints[2].position,
LCR_racing.carBody.joints[3].position)),4);
LCR_racing.carPositions[0] = _LCR_TPE_vec3DividePlain(
TPE_vec3TimesPlain(wheelAverage,LCR_GAME_UNIT),LCR_PHYSICS_UNIT);
LCR_racing.carPositions[0] = // smooth the position
TPE_vec3KeepWithinBox(LCR_racing.carPositions[1],LCR_racing.carPositions[0],
TPE_vec3(
LCR_PHYSICS_UNIT / 64, // TODO: constant
LCR_PHYSICS_UNIT / 64,
LCR_PHYSICS_UNIT / 64));
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;
}
/**
Initializes new run.
*/
@ -530,12 +563,6 @@ void LCR_racingRestart(void)
LCR_racing.carSpeed = 0;
LCR_racing.carDrifting = 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];
// make the car body:
TPE_makeCenterRectFull(LCR_racing.carJoints,
LCR_racing.carConnections,
@ -582,6 +609,16 @@ for (int i = 0; i < LCR_CAR_JOINTS; ++i)
LCR_racing.carNotOKCount = 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];
_LCR_racingUpdateCarPosRot();
LCR_racing.carPositions[1] = LCR_racing.carPositions[0];
LCR_racing.carRotations[1] = LCR_racing.carRotations[0];
// TODO
}
@ -628,11 +665,11 @@ void LCR_racingGetCarTransform(LCR_GameUnit position[3],
position[2] = v.z;
rotation[0] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].x,
LCR_racing.carRotations[1].x);
LCR_racing.carRotations[1].x);
rotation[1] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].y,
LCR_racing.carRotations[1].y);
LCR_racing.carRotations[1].y);
rotation[2] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].z,
LCR_racing.carRotations[1].z);
LCR_racing.carRotations[1].z);
#else
position[0] = LCR_racing.carPositions[0].x;
position[1] = LCR_racing.carPositions[0].y;
@ -982,7 +1019,9 @@ if (TPE_vec3Dot(carVel,carForw) < 0)
_LCR_racingUpdateCarPosRot();
/*
TPE_Vec3 tmpVec = LCR_racing.carPositions[0];
TPE_Vec3 wheelAverage = _LCR_TPE_vec3DividePlain(
@ -1011,6 +1050,9 @@ if (TPE_vec3Dot(carVel,carForw) < 0)
LCR_racing.carRotations[1] = LCR_racing.carRotations[0];
LCR_racing.carRotations[0] = tmpVec;
*/
TPE_Unit angle = TPE_vec3Dot(carUp,TPE_vec3Normalized(TPE_vec3Minus(
LCR_racing.carBody.joints[4].position,
@ -1020,7 +1062,7 @@ if (TPE_vec3Dot(carVel,carForw) < 0)
{
LCR_LOG2("roof squeezed, applying anti force")
tmpVec = TPE_vec3Times(carUp,LCR_PHYSICS_UNIT / 16); // TODO: 16 magic con.
TPE_Vec3 tmpVec = TPE_vec3Times(carUp,LCR_PHYSICS_UNIT / 16); // TODO: 16 magic con.
angle = TPE_F - 4 * angle; // 4 comes from above TPE_F / 4
@ -1029,7 +1071,16 @@ if (TPE_vec3Dot(carVel,carForw) < 0)
if (angle <= 0)
{
LCR_LOG1("roof flipped over, fixing")
LCR_racing.carBody.joints[4].position = wheelAverage;
LCR_racing.carBody.joints[4].position =
_LCR_TPE_vec3DividePlain(
TPE_vec3Plus(
TPE_vec3Plus(
LCR_racing.carBody.joints[0].position,
LCR_racing.carBody.joints[1].position),
TPE_vec3Plus(
LCR_racing.carBody.joints[2].position,
LCR_racing.carBody.joints[3].position)),4);
angle = 0;
}
@ -1126,6 +1177,7 @@ if (TPE_vec3Dot(carVel,carForw) < 0)
void LCR_physicsDebugDraw(LCR_GameUnit camPos[3], LCR_GameUnit camRot[2],
LCR_GameUnit camFov)
{
#if LCR_SETTING_DEBUG_PHYSICS_DRAW
LCR_LOG2("drawing physics debug overlay");
TPE_Vec3 cPos, cRot, cView;
@ -1144,6 +1196,7 @@ void LCR_physicsDebugDraw(LCR_GameUnit camPos[3], LCR_GameUnit camRot[2],
TPE_worldDebugDraw(&(LCR_racing.physicsWorld),_LCR_drawPhysicsDebugPixel,
cPos,cRot,cView,16,LCR_PHYSICS_UNIT / 4,LCR_racing.tick * 4);
#endif
}
#endif // guard

View file

@ -1621,6 +1621,20 @@ void LCR_rendererCameraFollow(void)
#endif
}
void LCR_rendererCameraReset(void)
{
LCR_renderer.scene.camera.transform.translation =
LCR_renderer.carModel->transform.translation;
LCR_renderer.scene.camera.transform.translation.x -=
S3L_sin(LCR_renderer.carModel->transform.rotation.y);
LCR_renderer.scene.camera.transform.translation.z +=
S3L_cos(LCR_renderer.carModel->transform.rotation.y);
LCR_rendererCameraFollow();
}
void LCR_rendererSetWheelState(LCR_GameUnit rotation, LCR_GameUnit steer)
{
#if LCR_ANIMATE_CAR

View file

@ -159,4 +159,9 @@
#define LCR_SETTING_METERS_PER_BLOCK 4
#endif
#ifndef LCR_SETTING_DEBUG_PHYSICS_DRAW
/** If on, physics world will be drawn. */
#define LCR_SETTING_DEBUG_PHYSICS_DRAW 0
#endif
#endif // guard