Tune car parameters

This commit is contained in:
Miloslav Ciz 2024-12-03 00:05:15 +01:00
parent 3e6c205302
commit d6380a0e45
2 changed files with 50 additions and 25 deletions

View file

@ -25,6 +25,7 @@ static const char *LCR_maps[] =
"#=W0s3 #fd190"
"#(s0r0"
"#~t1t2 #~u1t2 #~t1u2"
"#^t0r0"
};
#define LCR_IMAGE_SIZE 64 ///< one-dimension resolution of bitmap image

View file

@ -29,22 +29,23 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
// TODO: move some of this to constants?
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 140)
#define LCR_CAR_FORWARD_FRICTION (TPE_F / 9)
#define LCR_CAR_TURN_FRICTION (TPE_F)
#define LCR_CAR_ELASTICITY (TPE_F / 110)
#define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 16)
#define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 18)
#define LCR_CAR_TURN_MAX ((7 * LCR_GAME_UNIT) / 24)
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 160)
#define LCR_CAR_FORWARD_FRICTION (TPE_F / 180)
#define LCR_CAR_AIR_FRICTION ((LCR_GAME_UNIT * 3) / 4)
#define LCR_CAR_STAND_FRICTION_MULTIPLIER 16
#define LCR_CAR_STEER_FRICTION (TPE_F)
#define LCR_CAR_ELASTICITY (TPE_F / 50)
#define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 90)
#define LCR_CAR_STEER_SPEED (LCR_GAME_UNIT / 32)
#define LCR_CAR_STEER_MAX ((7 * LCR_GAME_UNIT) / 24)
// TODO
#define LCR_CAR_FORWARD_FRICTION_ICE (TPE_F / 200)
#define LCR_CAR_TURN_FRICTION_ICE (TPE_F / 20)
#define LCR_CAR_STEER_FRICTION_ICE (TPE_F / 20)
#define LCR_CAR_ACCELERATION_ICE (LCR_PHYSICS_UNIT / 100)
#define LCR_CAR_FORWARD_FRICTION_DIRT (TPE_F / 7)
#define LCR_CAR_TURN_FRICTION_DIRT (TPE_F / 2)
#define LCR_CAR_TURN_FRICTION_GRASS (4 * (TPE_F / 5))
#define LCR_CAR_STEER_FRICTION_DIRT (TPE_F / 2)
#define LCR_CAR_STEER_FRICTION_GRASS (4 * (TPE_F / 5))
#define LCR_CAR_ACCELERATION_GRASS (LCR_PHYSICS_UNIT / 20)
#define LCR_CAR_JOINTS 5
@ -72,6 +73,10 @@ uint8_t carNotOKCount;
LCR_GameUnit wheelRotation;
LCR_GameUnit wheelSteer;
LCR_GameUnit carSpeed;
} LCR_racing;
TPE_Vec3 _LCR_TPE_vec3DividePlain(TPE_Vec3 v, TPE_Unit d)
@ -375,11 +380,7 @@ TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist)
LCR_GameUnit LCR_racingGetCarSpeed(void)
{
return (TPE_vec3Len(TPE_vec3(
LCR_racing.carBody.joints[4].velocity[0],
LCR_racing.carBody.joints[4].velocity[1],
LCR_racing.carBody.joints[4].velocity[2])) * LCR_GAME_UNIT)
/ LCR_PHYSICS_UNIT;
return LCR_racing.carSpeed;
}
uint8_t _LCR_racingCollisionHandler(uint16_t b1, uint16_t j1, uint16_t b2,
@ -421,6 +422,7 @@ void LCR_racingRestart(void)
LCR_racing.wheelRotation = 0;
LCR_racing.wheelSteer = 0;
LCR_racing.carSpeed = 0;
LCR_racing.carPositions[0] = TPE_vec3(0,0,0);
LCR_racing.carPositions[1] = LCR_racing.carPositions[0];
@ -569,6 +571,14 @@ void _LCR_racingWheelAccelerate(unsigned int wheel, TPE_Vec3 dir,
LCR_CAR_ACCELERATION_GRASS :
LCR_CAR_ACCELERATION);
acc -=
(acc * LCR_racing.carSpeed) / LCR_CAR_AIR_FRICTION;
if (acc < 0)
acc = 0;
LCR_racing.carBody.joints[wheel].velocity[0] +=
(dir.x * acc) / TPE_F;
LCR_racing.carBody.joints[wheel].velocity[1] +=
@ -660,6 +670,14 @@ uint32_t LCR_racingStep(unsigned int input)
LCR_CAR_FORWARD_FRICTION_DIRT :
LCR_CAR_FORWARD_FRICTION);
// TODO: also apply (probably twice as much) when accelerating in opposite direction than vecolcity vec.
if (!(input & (LCR_RACING_INPUT_FORW | LCR_RACING_INPUT_BACK)))
LCR_racing.carBody.friction *= LCR_CAR_STAND_FRICTION_MULTIPLIER;
if (input)
{
unsigned char steering = 0;
@ -687,16 +705,16 @@ uint32_t LCR_racingStep(unsigned int input)
steering = 2;
LCR_racing.wheelSteer = TPE_min(
LCR_racing.wheelSteer + LCR_CAR_TURN_SPEED,
LCR_CAR_TURN_MAX);
LCR_racing.wheelSteer + LCR_CAR_STEER_SPEED,
LCR_CAR_STEER_MAX);
}
else if (input & LCR_RACING_INPUT_LEFT)
{
steering = 1;
LCR_racing.wheelSteer = TPE_max(
LCR_racing.wheelSteer - LCR_CAR_TURN_SPEED,
-1 * LCR_CAR_TURN_MAX);
LCR_racing.wheelSteer - LCR_CAR_STEER_SPEED,
-1 * LCR_CAR_STEER_MAX);
}
if ((LCR_racing.wheelCollisions & 0x0c)) // back wheel on ground?
@ -738,13 +756,13 @@ uint32_t LCR_racingStep(unsigned int input)
determined by the dot product (angle) of the axis and velocity */
TPE_Vec3 fric = TPE_vec3Times(ja,(TPE_vec3Dot(ja,jv) *
(groundMat == LCR_BLOCK_MATERIAL_CONCRETE ?
LCR_CAR_TURN_FRICTION :
LCR_CAR_STEER_FRICTION :
(groundMat == LCR_BLOCK_MATERIAL_DIRT ?
LCR_CAR_TURN_FRICTION_DIRT :
LCR_CAR_STEER_FRICTION_DIRT :
(
groundMat == LCR_BLOCK_MATERIAL_GRASS ?
LCR_CAR_TURN_FRICTION_GRASS :
LCR_CAR_TURN_FRICTION_ICE)))
LCR_CAR_STEER_FRICTION_GRASS :
LCR_CAR_STEER_FRICTION_ICE)))
) / TPE_F);
jv = TPE_vec3Minus(jv,fric); // subtract the friction
@ -765,6 +783,12 @@ uint32_t LCR_racingStep(unsigned int input)
TPE_worldStep(&(LCR_racing.physicsWorld));
LCR_LOG2("stepping physics engine done");
LCR_racing.carSpeed = (TPE_vec3Len(TPE_vec3(
LCR_racing.carBody.joints[4].velocity[0],
LCR_racing.carBody.joints[4].velocity[1],
LCR_racing.carBody.joints[4].velocity[2])) * LCR_GAME_UNIT)
/ LCR_PHYSICS_UNIT;
TPE_Vec3 tmpVec = LCR_racing.carPositions[0];
TPE_Vec3 wheelAverage = _LCR_TPE_vec3DividePlain(