Rework drifting

This commit is contained in:
Miloslav Ciz 2025-04-16 22:31:56 +02:00
parent 74bd36c893
commit 51bc7ada30
3 changed files with 41 additions and 33 deletions

View file

@ -36,7 +36,7 @@
physics version.
*/
typedef int32_t LCR_GameUnit; ///< abstract game unit
typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_RACING_VERSION1 '0' ///< first part of physics eng. version
#define LCR_RACING_VERSION2 '0' ///< second part of physics eng. version
@ -84,9 +84,6 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_CAR_WHEEL_AIR_ROTATION_SPEED (LCR_GAME_UNIT / 32)
#define LCR_CAR_WHEEL_GROUND_SPEED_DIV 8
#define LCR_CAR_DRIFT_THRESHOLD_1 (LCR_GAME_UNIT / 4)
#define LCR_CAR_DRIFT_THRESHOLD_0 (LCR_GAME_UNIT / 200)
#define LCR_CAR_CRASH_SPEED_SMALL 400
#define LCR_CAR_CRASH_SPEED_BIG 800
@ -94,8 +91,13 @@ 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)
#define LCR_CAR_DRIFT_THRESHOLD_0 (LCR_GAME_UNIT / 227)
#define LCR_CAR_JOINTS 5
#define LCR_CAR_CONNECTIONS 10
@ -1122,7 +1124,7 @@ int _LCR_racingCarShapeOK(void)
LCR_racing.carBody.connections[i].joint1].position,
LCR_racing.carBody.joints[
LCR_racing.carBody.connections[i].joint2].position),
LCR_racing.carBody.connections[i].length) < TPE_F / 16; // TODO: const
LCR_racing.carBody.connections[i].length) < TPE_F / 16; // 16: magic con.
return r;
}
@ -1140,6 +1142,7 @@ uint32_t LCR_racingStep(unsigned int input)
uint8_t groundMat = LCR_BLOCK_MATERIAL_CONCRETE; // material under wheels
uint8_t onAccel = 0; // standing on accelerator?
int groundBlockIndex = -1;
TPE_Unit driftFriction = 0; // average wheel friction (absolute value)
if (LCR_racing.playingReplay)
{
@ -1182,7 +1185,7 @@ uint32_t LCR_racingStep(unsigned int input)
LCR_racing.carBody.joints[4].velocity[2]);
/* Apply gravity like this: if all wheels are on ground, we don't apply
gravity to roof. This helps prevent sliding. */
gravity to roof. This helps prevent sliding when standing still. */
for (int i = 0; i < 5; ++i)
if (i < 4 || (((LCR_racing.wheelCollisions |
@ -1247,8 +1250,8 @@ uint32_t LCR_racingStep(unsigned int input)
if(!(input & (LCR_RACING_INPUT_FORW | LCR_RACING_INPUT_BACK)))
LCR_racing.carBody.friction *= LCR_CAR_STAND_FRICTION_MULTIPLIER;
else if (
((input & LCR_RACING_INPUT_FORW) && (LCR_racing.carSpeeds[0] < 0)) ||
((input & LCR_RACING_INPUT_BACK) && (LCR_racing.carSpeeds[0] > 0)))
((input & LCR_RACING_INPUT_FORW) && (LCR_racing.carSpeeds[0] < 0)) ||
((input & LCR_RACING_INPUT_BACK) && (LCR_racing.carSpeeds[0] > 0)))
LCR_racing.carBody.friction *= 2 * LCR_CAR_STAND_FRICTION_MULTIPLIER;
if (input)
@ -1316,8 +1319,6 @@ uint32_t LCR_racingStep(unsigned int input)
}
}
TPE_Unit driftFriction = 0; // average wheel friction (absolute value)
for (int i = 0; i < 4; ++i)
if (LCR_racing.wheelCollisions & (0x11 << i)) // wheel on ground?
{
@ -1359,19 +1360,6 @@ uint32_t LCR_racingStep(unsigned int input)
driftFriction /= 4; // divide by 4 wheels
if ((!LCR_racing.carDrifting) &&
driftFriction > LCR_CAR_DRIFT_THRESHOLD_1)
{
LCR_LOG2("drift start");
LCR_racing.carDrifting = 1;
}
else if (LCR_racing.carDrifting &&
driftFriction < LCR_CAR_DRIFT_THRESHOLD_0)
{
LCR_LOG2("drift end");
LCR_racing.carDrifting = 0;
}
if (steering &&
(input & (LCR_RACING_INPUT_FORW | LCR_RACING_INPUT_BACK)) &&
(LCR_racing.wheelCollisions & 0x33))
@ -1386,6 +1374,23 @@ uint32_t LCR_racingStep(unsigned int input)
}
}
if ((!LCR_racing.carDrifting) &&
driftFriction >
(LCR_CAR_DRIFT_THRESHOLD_1 >> (( // back key initiates drift easily
((input & (LCR_RACING_INPUT_FORW | LCR_RACING_INPUT_BACK))
== (LCR_RACING_INPUT_FORW | LCR_RACING_INPUT_BACK))) << 2)))
{
LCR_LOG1("drift start");
LCR_racing.carDrifting = 1;
}
else if (LCR_racing.carDrifting &&
(driftFriction < LCR_CAR_DRIFT_THRESHOLD_0 ||
LCR_racingGetCarSpeedUnsigned() < 5))
{
LCR_LOG1("drift end");
LCR_racing.carDrifting = 0;
}
if ((!(input & LCR_RACING_INPUT_LEFT)) &&
(!(input & LCR_RACING_INPUT_RIGHT)))
LCR_racing.wheelSteer /= 2;
@ -1437,7 +1442,7 @@ uint32_t LCR_racingStep(unsigned int input)
if ((LCR_racing.carBody.flags & TPE_BODY_FLAG_UNRESOLVED) ||
!_LCR_racingCarShapeOK()) // bad?
{
LCR_LOG1("car bad, simplifying physics");
LCR_LOG2("using simplified physics");
for (int i = 0; i < LCR_CAR_JOINTS; ++i) // use the first step positions
LCR_racing.carBody.joints[i] = joints[i];