Add drifts

This commit is contained in:
Miloslav Ciz 2024-12-10 16:17:52 +01:00
parent b5cfda8253
commit c145933004
4 changed files with 58 additions and 5 deletions

View file

@ -40,10 +40,14 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_CAR_STEER_SPEED (LCR_GAME_UNIT / 17)
#define LCR_CAR_STEER_MAX (LCR_GAME_UNIT / 2)
#define LCR_CAR_DRIFT_THRESHOLD_1 (LCR_GAME_UNIT / 4)
#define LCR_CAR_DRIFT_THRESHOLD_0 (LCR_GAME_UNIT / 200)
// multipliers (in 8ths) of friction and acceleration on concrete:
#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_JOINTS 5
#define LCR_CAR_CONNECTIONS 10
@ -62,10 +66,10 @@ struct
TPE_Vec3 carPositions[2]; ///* Current and previous position in game units.
TPE_Vec3 carRotations[2]; ///* Current and previous rotation in game units.
uint8_t carDrifting; ///* Whether or not the car is currently in drift.
TPE_Vec3 carOKPositions[LCR_CAR_JOINTS];
uint8_t carNotOKCount;
TPE_Vec3 carOKPositions[LCR_CAR_JOINTS];
uint8_t carNotOKCount;
LCR_GameUnit wheelRotation;
@ -74,9 +78,9 @@ uint8_t carNotOKCount;
LCR_GameUnit carSpeed; ///* Signed speed (negative if backwards)
} LCR_racing;
TPE_Vec3 _LCR_TPE_vec3DividePlain(TPE_Vec3 v, TPE_Unit d)
{
v.x /= d; v.y /= d; v.z /= d;
@ -431,6 +435,7 @@ void LCR_racingRestart(void)
LCR_racing.wheelRotation = 0;
LCR_racing.wheelSteer = 0;
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];
@ -782,6 +787,8 @@ printf("------\n");
}
}
TPE_Unit driftFriction = 0; // average wheel friction (absolute value)
for (int i = 0; i < 4; ++i)
if (LCR_racing.wheelCollisions & (0x01 << i)) // wheel on ground?
{
@ -807,7 +814,12 @@ printf("------\n");
determined by the dot product (angle) of the axis and velocity */
TPE_Vec3 fric = TPE_vec3Times(ja,(TPE_vec3Dot(ja,jv) *
_LCR_applyMaterialFactor(LCR_CAR_STEER_FRICTION,groundMat)) / TPE_F);
_LCR_applyMaterialFactor(
LCR_racing.carDrifting ?
(LCR_CAR_STEER_FRICTION * LCR_CAR_DRIFT_FACTOR) / 8 :
LCR_CAR_STEER_FRICTION,groundMat)) / TPE_F);
driftFriction += TPE_vec3Len(fric);
jv = TPE_vec3Minus(jv,fric); // subtract the friction
@ -816,6 +828,21 @@ printf("------\n");
LCR_racing.carBody.joints[i].velocity[2] = jv.z;
}
driftFriction /= 4;
if ((!LCR_racing.carDrifting) &&
driftFriction > LCR_CAR_DRIFT_THRESHOLD_1)
{
LCR_LOG1("drift start");
LCR_racing.carDrifting = 1;
}
else if (LCR_racing.carDrifting &&
driftFriction < LCR_CAR_DRIFT_THRESHOLD_0)
{
LCR_LOG1("drift end");
LCR_racing.carDrifting = 0;
}
/* The following fixes "sticking to a wall" issue by adding a small spin
to the car when trying to steer at very small velocity. */
if (steering &&