Optimize rendering

This commit is contained in:
Miloslav Ciz 2025-04-24 16:55:35 +02:00
parent c22f17ef8c
commit 5265552c57
8 changed files with 117 additions and 89 deletions

View file

@ -6,10 +6,15 @@
Licar: racing module
This implements the racing physics and logic as well as replays and other
related things. It's possible to use this module alone if one wants to
related stuff. It's possible to use this module alone if one wants to
implement a program that doesn't need graphics, I/O etc.
Some comments:
- This module uses tinyphysicsengine, a small and simple physics engine that
uses "balls and springs" to model bodies (here the car) and kind of
"signed distance functions" to model environment (the map). The car is
strictly a soft body, but it's very "stiff" so that it behaves almost like
a rigid body.
- Replays are internally stored as follows: the replay consists of 16 bit
words representing changes in input at specific frame. In lowest 4 bits the
new input state is recorded, the remaining 12 bits record physics frame
@ -91,8 +96,6 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_CAR_GRASS_FACTOR 5
#define LCR_CAR_DIRT_FACTOR 3
#define LCR_CAR_ICE_FACTOR 1
//#define LCR_CAR_DRIFT_FACTOR 2 ///< only affects steering friction
#define LCR_CAR_DRIFT_FACTOR 2 ///< only affects steering friction
#define LCR_CAR_DRIFT_THRESHOLD_1 (LCR_GAME_UNIT / 10)
@ -218,7 +221,6 @@ void LCR_replayOutputStr(void (*printChar)(char))
#define PUTD(order) \
printChar('0' + (LCR_racing.replay.achievedTime / order) % 10);
PUTD(1000000) PUTD(100000) PUTD(10000)
PUTD(1000) PUTD(100) PUTD(10) PUTD(1)
#undef PUTD
@ -252,7 +254,7 @@ int LCR_replayLoadFromStr(char (*nextChar)(void),
LCR_racing.replay.eventCount = 0;
LCR_racing.replay.achievedTime = 0;
// has to be like this to force correct evaluation order:
// Has to be like this to force correct evaluation order:
c = nextChar() == LCR_RACING_VERSION1;
c |= (nextChar() == LCR_RACING_VERSION2) << 1;
@ -431,8 +433,10 @@ int LCR_replayRecordEvent(uint32_t frame, uint8_t input)
}
/**
Helper function for _LCR_racingEnvironmentFunction, returns closest point on
a map block placed at coordinate origin.
Helper function for _LCR_racingEnvironmentFunction, for given arbitrary point
returns the closest point on map block (of given type) placed at coordinate
origin (and in default orientation). This function defines shapes of map
blocks.
*/
TPE_Vec3 _LCR_racingBlockEnvFunc(TPE_Vec3 point, const uint8_t *block)
{
@ -758,7 +762,7 @@ TPE_Vec3 _LCR_racingBlockEnvFunc(TPE_Vec3 point, const uint8_t *block)
/**
For tinyphysicsengine, function that defines the shape of the static physics
world, returns closest point to any given point in space.
world. For any given point in space returns the environment's closest point.
*/
TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist)
{
@ -821,7 +825,7 @@ TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist)
TPE_ENV_NEXT(_LCR_racingBlockEnvFunc(point, // check it
LCR_currentMap.blocks + blockNum * LCR_BLOCK_SIZE),point)
// narrow the search range:
// Narrow the search range:
if (i % 2 == 0 && blockNum > start)
start = blockNum;
@ -869,8 +873,7 @@ uint8_t _LCR_racingCollisionHandler(uint16_t b1, uint16_t j1, uint16_t b2,
LCR_racing.carBody.joints[j1].velocity[0],
LCR_racing.carBody.joints[j1].velocity[1],
LCR_racing.carBody.joints[j1].velocity[2]),
TPE_vec3Minus(p,
LCR_racing.carBody.joints[j1].position)));
TPE_vec3Minus(p,LCR_racing.carBody.joints[j1].position)));
LCR_racing.crashState |= ((speed >= LCR_CAR_CRASH_SPEED_BIG) << 1) |
(speed >= LCR_CAR_CRASH_SPEED_SMALL);
@ -1527,10 +1530,10 @@ uint32_t LCR_racingStep(unsigned int input)
{
LCR_LOG2("roof squeezed, applying anti force")
TPE_Vec3 tmpVec = TPE_vec3Times(carUp,LCR_PHYSICS_UNIT / 16); // TODO: 16 magic con.
TPE_Vec3 tmpVec =
TPE_vec3Times(carUp,LCR_PHYSICS_UNIT / 16); // 16: magic const.
angle = TPE_F - 4 * angle; // 4 comes from above TPE_F / 4
tmpVec = TPE_vec3Times(tmpVec,angle);
if (angle <= 0)
@ -1540,7 +1543,7 @@ uint32_t LCR_racingStep(unsigned int input)
angle = 0;
}
// accelerate roof and wheels away from each other
// Accelerate roof and wheels away from each other:
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
{
LCR_racing.carBody.joints[i].velocity[0] += (i == 4 ? 1 : -1) * tmpVec.x;