Do some tuning

This commit is contained in:
Miloslav Ciz 2024-09-10 21:49:23 +02:00
parent 4754b33dfe
commit b6d28c2886
4 changed files with 70 additions and 16 deletions

10
game.h
View file

@ -170,6 +170,9 @@ uint8_t LCR_gameStep(uint32_t time)
LCR_keyStates[i] = LCR_keyPressed(i) ? LCR_keyStates[i] = LCR_keyPressed(i) ?
(LCR_keyStates[i] < 255 ? LCR_keyStates[i] + 1 : 255) : 0; (LCR_keyStates[i] < 255 ? LCR_keyStates[i] + 1 : 255) : 0;
if ((LCR_racing.tick % 32) == 0)
printf("speed: %d\n",LCR_racingGetCarSpeed());
uint32_t sleep = 0; uint32_t sleep = 0;
if (LCR_keyStates[LCR_KEY_B] == 1) if (LCR_keyStates[LCR_KEY_B] == 1)
@ -193,6 +196,8 @@ uint8_t LCR_gameStep(uint32_t time)
LCR_game.nextRacingTickTime += LCR_RACING_TICK_MS; LCR_game.nextRacingTickTime += LCR_RACING_TICK_MS;
} }
sleep = (3 * (LCR_game.nextRacingTickTime - time)) / 4;
if (time >= LCR_game.nextRenderFrameTime) if (time >= LCR_game.nextRenderFrameTime)
{ {
LCR_GameUnit physicsInterpolationParam = LCR_GAME_UNIT - LCR_GameUnit physicsInterpolationParam = LCR_GAME_UNIT -
@ -260,7 +265,10 @@ LCR_GameUnit physicsInterpolationParam = LCR_GAME_UNIT -
} }
} }
else else
sleep = LCR_game.nextRenderFrameTime - time; {
uint32_t tmp = (3 * (LCR_game.nextRenderFrameTime - time)) / 4;
sleep = tmp < sleep ? tmp : sleep;
}
if (sleep) if (sleep)
LCR_sleep(sleep); LCR_sleep(sleep);

View file

@ -14,7 +14,7 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_RACING_INPUT_BACK 0x04 #define LCR_RACING_INPUT_BACK 0x04
#define LCR_RACING_INPUT_LEFT 0x08 #define LCR_RACING_INPUT_LEFT 0x08
#define LCR_PHYSICS_UNIT 1024 ///< length of map square for physics engine #define LCR_PHYSICS_UNIT 2048 ///< length of map square for physics engine
#include "map.h" #include "map.h"
#include "tinyphysicsengine.h" #include "tinyphysicsengine.h"
@ -23,12 +23,22 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_CAR_CONNECTIONS 10 #define LCR_CAR_CONNECTIONS 10
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 100) #define LCR_GRAVITY (LCR_PHYSICS_UNIT / 100)
#define LCR_CAR_FORWARD_FRICTION TPE_F / 14 #define LCR_CAR_FORWARD_FRICTION (TPE_F / 11)
#define LCR_CAR_TURN_FRICTION (4 * TPE_F / 4)
#define LCR_CAR_ELASTICITY (TPE_F / 100)
#define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 15)
#define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 16)
#define LCR_CAR_TURN_MAX (LCR_GAME_UNIT / 4)
/*
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 100)
#define LCR_CAR_FORWARD_FRICTION (TPE_F / 14)
#define LCR_CAR_TURN_FRICTION (3 * TPE_F / 4) #define LCR_CAR_TURN_FRICTION (3 * TPE_F / 4)
#define LCR_CAR_ELASTICITY (TPE_F / 100) #define LCR_CAR_ELASTICITY (TPE_F / 100)
#define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 20) #define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 18)
#define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 3) #define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 3)
#define LCR_CAR_TURN_MAX (LCR_GAME_UNIT / 4) #define LCR_CAR_TURN_MAX (LCR_GAME_UNIT / 4)
*/
struct struct
{ {
@ -36,6 +46,7 @@ struct
TPE_Body carBody; TPE_Body carBody;
TPE_Joint carJoints[LCR_CAR_JOINTS]; TPE_Joint carJoints[LCR_CAR_JOINTS];
TPE_Connection carConnections[LCR_CAR_CONNECTIONS]; TPE_Connection carConnections[LCR_CAR_CONNECTIONS];
uint32_t tick;
uint8_t wheelCollisions; /**< In individual bits records for each car wheel uint8_t wheelCollisions; /**< In individual bits records for each car wheel
whether it's currently touching the ground. whether it's currently touching the ground.
Lower bits record current collisions, higher Lower bits record current collisions, higher
@ -89,6 +100,8 @@ uint8_t _LCR_racingCollisionHandler(uint16_t b1, uint16_t j1, uint16_t b2,
*/ */
void LCR_racingRestart(void) void LCR_racingRestart(void)
{ {
LCR_racing.tick = 0;
TPE_bodyActivate(&(LCR_racing.carBody)); TPE_bodyActivate(&(LCR_racing.carBody));
LCR_racing.wheelCollisions = 0; LCR_racing.wheelCollisions = 0;
@ -233,28 +246,32 @@ void LCR_racingStep(unsigned int input)
if (input & (LCR_RACING_INPUT_FORW | LCR_RACING_INPUT_BACK)) if (input & (LCR_RACING_INPUT_FORW | LCR_RACING_INPUT_BACK))
{ {
// TODO: in air always rotate wheels LCR_GameUnit rotateBy =
(LCR_racing.wheelCollisions & 0x0f) ? // on ground slow down wheel rot.
(LCR_racingGetCarSpeed() / 16) : LCR_GAME_UNIT / 32;
LCR_racing.wheelRotation = LCR_racing.wheelRotation + if (!(input & LCR_RACING_INPUT_BACK))
(LCR_racingGetCarSpeed() / 32) rotateBy *= -1;
% LCR_GAME_UNIT;
LCR_racing.wheelRotation =
(LCR_racing.wheelRotation + rotateBy) % LCR_GAME_UNIT;
if (LCR_racing.wheelRotation < 0) if (LCR_racing.wheelRotation < 0)
LCR_racing.wheelRotation += LCR_GAME_UNIT; LCR_racing.wheelRotation += LCR_GAME_UNIT;
} }
if (input & LCR_RACING_INPUT_RIGHT) if (input & LCR_RACING_INPUT_RIGHT)
{ {
steering = 2; steering = 2;
LCR_racing.wheelSteer = TPE_min( LCR_racing.wheelSteer = TPE_min(
LCR_racing.wheelSteer + LCR_CAR_TURN_SPEED, LCR_racing.wheelSteer + LCR_CAR_TURN_SPEED,
LCR_CAR_TURN_MAX); LCR_CAR_TURN_MAX);
} }
else if (input & LCR_RACING_INPUT_LEFT) else if (input & LCR_RACING_INPUT_LEFT)
{ {
steering = 1; steering = 1;
LCR_racing.wheelSteer = TPE_max( LCR_racing.wheelSteer = TPE_max(
LCR_racing.wheelSteer - LCR_CAR_TURN_SPEED, LCR_racing.wheelSteer - LCR_CAR_TURN_SPEED,
-1 * LCR_CAR_TURN_MAX); -1 * LCR_CAR_TURN_MAX);
@ -350,6 +367,8 @@ LCR_racing.carPositions[0] =
); );
LCR_racing.carPositions[1] = tmpVec; LCR_racing.carPositions[1] = tmpVec;
LCR_racing.tick++;
} }
void LCR_physicsDebugDraw(LCR_GameUnit camPos[3], LCR_GameUnit camRot[2], void LCR_physicsDebugDraw(LCR_GameUnit camPos[3], LCR_GameUnit camRot[2],

View file

@ -98,12 +98,32 @@ void LCR_rendererSetCarTransform(LCR_GameUnit position[3],
LCR_renderer.carModel->transform.translation.z = LCR_renderer.carModel->transform.translation.z =
(position[2] * LCR_RENDERER_UNIT) / LCR_GAME_UNIT; (position[2] * LCR_RENDERER_UNIT) / LCR_GAME_UNIT;
// TODO: make a separate function that does the smoothing (updateCarTransform)
LCR_renderer.carModel->transform.rotation.x =
_LCR_smoothRotation(LCR_renderer.carModel->transform.rotation.x,
S3L_wrap((rotation[0] * S3L_F) / LCR_GAME_UNIT,S3L_F),1);
LCR_renderer.carModel->transform.rotation.y = S3L_wrap((rotation[1] *
S3L_F) / LCR_GAME_UNIT,S3L_F); // don't smooth for faster reaction?
/*
LCR_renderer.carModel->transform.rotation.y =
_LCR_smoothRotation(LCR_renderer.carModel->transform.rotation.y,
S3L_wrap((rotation[1] * S3L_F) / LCR_GAME_UNIT,S3L_F),1);
*/
LCR_renderer.carModel->transform.rotation.z =
_LCR_smoothRotation(LCR_renderer.carModel->transform.rotation.z,
S3L_wrap((rotation[2] * S3L_F) / LCR_GAME_UNIT,S3L_F),1);
/*
LCR_renderer.carModel->transform.rotation.x = S3L_wrap((rotation[0] * LCR_renderer.carModel->transform.rotation.x = S3L_wrap((rotation[0] *
S3L_F) / LCR_GAME_UNIT,S3L_F); S3L_F) / LCR_GAME_UNIT,S3L_F);
LCR_renderer.carModel->transform.rotation.y = S3L_wrap((rotation[1] * LCR_renderer.carModel->transform.rotation.y = S3L_wrap((rotation[1] *
S3L_F) / LCR_GAME_UNIT,S3L_F); S3L_F) / LCR_GAME_UNIT,S3L_F);
LCR_renderer.carModel->transform.rotation.z = S3L_wrap((rotation[2] * LCR_renderer.carModel->transform.rotation.z = S3L_wrap((rotation[2] *
S3L_F) / LCR_GAME_UNIT,S3L_F); S3L_F) / LCR_GAME_UNIT,S3L_F);
*/
} }
void _LCR_pixelFuncc3D(S3L_PixelInfo *pixel) void _LCR_pixelFuncc3D(S3L_PixelInfo *pixel)
@ -1166,7 +1186,8 @@ void _LCR_rendererLoadMapChunk(uint8_t chunk, int8_t x, int8_t y, int8_t z)
/** /**
Serves for smoothing out angle change, e.g. that of camera rotation. Serves for smoothing out angle change, e.g. that of camera rotation.
*/ */
S3L_Unit _LCR_smoothRotation(S3L_Unit angleOld, S3L_Unit angleNew) S3L_Unit _LCR_smoothRotation(S3L_Unit angleOld, S3L_Unit angleNew,
unsigned int amount)
{ {
S3L_Unit angleDiff = angleNew - angleOld; S3L_Unit angleDiff = angleNew - angleOld;
@ -1181,10 +1202,15 @@ S3L_Unit _LCR_smoothRotation(S3L_Unit angleOld, S3L_Unit angleNew)
angleDiff += (angleDiff > 0) ? -1 * (S3L_F / 2) : (S3L_F / 2); angleDiff += (angleDiff > 0) ? -1 * (S3L_F / 2) : (S3L_F / 2);
} }
if (angleDiffAbs > S3L_F / 4) // angle too big, rotate immediately if (angleDiffAbs > (3 * S3L_F) / 8) // angle too big, rotate immediately
return angleNew; return angleNew;
return angleOld + angleDiff / 4; // smoothly interpolate /*
if (angleDiffAbs < S3L_F / 32)
return angleOld;
*/
return angleOld + (angleDiff >> amount); // smoothly interpolate
} }
/** /**
@ -1402,6 +1428,7 @@ void LCR_rendererCameraFollow(void)
#if LCR_SETTING_SMOOTH_ANIMATIONS #if LCR_SETTING_SMOOTH_ANIMATIONS
// now average with previous transform to smooth the animation out: // now average with previous transform to smooth the animation out:
S3L_vec3Add(&(LCR_renderer.scene.camera.transform.translation), S3L_vec3Add(&(LCR_renderer.scene.camera.transform.translation),
transPrev.translation); transPrev.translation);
@ -1410,10 +1437,10 @@ void LCR_rendererCameraFollow(void)
LCR_renderer.scene.camera.transform.translation.z /= 2; LCR_renderer.scene.camera.transform.translation.z /= 2;
LCR_renderer.scene.camera.transform.rotation.x = _LCR_smoothRotation( LCR_renderer.scene.camera.transform.rotation.x = _LCR_smoothRotation(
transPrev.rotation.x,LCR_renderer.scene.camera.transform.rotation.x); transPrev.rotation.x,LCR_renderer.scene.camera.transform.rotation.x,2);
LCR_renderer.scene.camera.transform.rotation.y = _LCR_smoothRotation( LCR_renderer.scene.camera.transform.rotation.y = _LCR_smoothRotation(
transPrev.rotation.y,LCR_renderer.scene.camera.transform.rotation.y); transPrev.rotation.y,LCR_renderer.scene.camera.transform.rotation.y,3);
#endif #endif
} }

View file

@ -97,7 +97,7 @@
0 turns off car animation completely (may be faster and smaller), 1 turns on 0 turns off car animation completely (may be faster and smaller), 1 turns on
highest quality animation, higher values lower animation quality and may highest quality animation, higher values lower animation quality and may
increase performance. */ increase performance. */
#define LCR_SETTING_CAR_ANIMATION_SUBDIVIDE 4 #define LCR_SETTING_CAR_ANIMATION_SUBDIVIDE 2
#endif #endif
#ifndef LCR_SETTING_CAMERA_HEIGHT #ifndef LCR_SETTING_CAMERA_HEIGHT