Add air brake

This commit is contained in:
Miloslav Ciz 2024-12-03 22:59:07 +01:00
parent be322fb37a
commit bac61bf05a
2 changed files with 50 additions and 18 deletions

2
game.h
View file

@ -237,7 +237,7 @@ uint8_t LCR_gameStep(uint32_t time)
if ((LCR_racing.tick % 32) == 0)
{
printf("speed: %d\n",LCR_racingGetCarSpeed());
printf("speed: %d\n",LCR_racingGetCarSpeedSigned());
}

View file

@ -34,7 +34,7 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
//#define LCR_CAR_AIR_FRICTION ((LCR_GAME_UNIT * 3) / 4)
#define LCR_CAR_AIR_FRICTION 32
#define LCR_CAR_STAND_FRICTION_MULTIPLIER 16
#define LCR_CAR_STAND_FRICTION_MULTIPLIER 32
#define LCR_CAR_STEER_FRICTION (TPE_F)
#define LCR_CAR_ELASTICITY (TPE_F / 150)
//#define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 90)
@ -79,7 +79,8 @@ uint8_t carNotOKCount;
LCR_GameUnit wheelSteer;
LCR_GameUnit carSpeed;
LCR_GameUnit carSpeed; ///* Signed speed (negative if backwards)
} LCR_racing;
@ -382,11 +383,21 @@ TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist)
TPE_ENV_END
}
LCR_GameUnit LCR_racingGetCarSpeed(void)
LCR_GameUnit LCR_racingGetCarSpeedUnsigned(void)
{
return LCR_racing.carSpeed >= 0 ? LCR_racing.carSpeed :
(-1 * LCR_racing.carSpeed);
}
LCR_GameUnit LCR_racingGetCarSpeedSigned(void)
{
return LCR_racing.carSpeed;
}
uint8_t _LCR_racingCollisionHandler(uint16_t b1, uint16_t j1, uint16_t b2,
uint16_t j2, TPE_Vec3 p)
{
@ -587,7 +598,7 @@ acc -=
*/
acc =
acc / (1 + (LCR_racing.carSpeed / LCR_CAR_AIR_FRICTION));
acc / (1 + (LCR_racingGetCarSpeedUnsigned() / LCR_CAR_AIR_FRICTION));
/*
if (acc < 0)
@ -627,7 +638,7 @@ uint32_t LCR_racingStep(unsigned int input)
LCR_LOG2("racing step start");
uint32_t result = 0;
TPE_Vec3 carForw, carRight, carUp;
TPE_Vec3 carForw, carRight, carUp, carVel;
uint8_t groundMat = LCR_BLOCK_MATERIAL_CONCRETE; // material under wheels
int groundBlockIndex = -1;
@ -645,6 +656,11 @@ uint32_t LCR_racingStep(unsigned int input)
carUp = TPE_vec3Cross(carForw,carRight);
carVel = TPE_vec3(
LCR_racing.carBody.joints[4].velocity[0],
LCR_racing.carBody.joints[4].velocity[1],
LCR_racing.carBody.joints[4].velocity[2]);
if ((LCR_racing.wheelCollisions & 0x0f) != 0x0f) // EXPERIMENTAL: don't apply gravity with all wheels on ground
TPE_bodyApplyGravity(&(LCR_racing.carBody),LCR_GRAVITY);
@ -686,17 +702,27 @@ 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 & (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.carSpeed < 0)) ||
((input & LCR_RACING_INPUT_BACK) && (LCR_racing.carSpeed > 0)))
LCR_racing.carBody.friction *= 2 * LCR_CAR_STAND_FRICTION_MULTIPLIER;
if (input)
{
unsigned char steering = 0;
if ((input & LCR_RACING_INPUT_BACK) &&
(LCR_racing.wheelCollisions & 0x0f) == 0x00) // in air?
{
LCR_LOG2("air brake");
for (int i = 0; i < LCR_CAR_JOINTS - 1; ++i)
for (int j = 0; j < 3; ++j)
LCR_racing.carBody.joints[i].velocity[j] =
LCR_racing.carBody.joints[LCR_CAR_JOINTS - 1].velocity[j];
}
// TODO: magic constants
@ -704,7 +730,7 @@ if (!(input & (LCR_RACING_INPUT_FORW | LCR_RACING_INPUT_BACK)))
{
LCR_GameUnit rotateBy =
(LCR_racing.wheelCollisions & 0x0f) ? // on ground slow down wheel rot.
(LCR_racingGetCarSpeed() / 16) : LCR_GAME_UNIT / 32;
(LCR_racingGetCarSpeedUnsigned() / 16) : LCR_GAME_UNIT / 32;
if (!(input & LCR_RACING_INPUT_BACK))
rotateBy *= -1;
@ -801,12 +827,18 @@ if (!(input & (LCR_RACING_INPUT_FORW | LCR_RACING_INPUT_BACK)))
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_racing.carSpeed = (TPE_vec3Len(carVel) * LCR_GAME_UNIT)
/ LCR_PHYSICS_UNIT;
if (TPE_vec3Dot(carVel,carForw) < 0)
LCR_racing.carSpeed *= -1;
TPE_Vec3 tmpVec = LCR_racing.carPositions[0];
TPE_Vec3 wheelAverage = _LCR_TPE_vec3DividePlain(