Fix some stuff

This commit is contained in:
Miloslav Ciz 2024-09-10 15:30:07 +02:00
parent 08fb45b652
commit 74ea3dcd41
4 changed files with 615 additions and 405 deletions

115
racing.h
View file

@ -22,10 +22,11 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_CAR_JOINTS 5
#define LCR_CAR_CONNECTIONS 10
#define LCR_GRAVITY 5
#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_ELASTICITY (TPE_F / 100)
#define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 8)
struct
{
@ -41,6 +42,8 @@ struct
TPE_Vec3 carPositions[2]; ///* Current and previous position.
LCR_GameUnit wheelRotation;
LCR_GameUnit wheelSteer;
} LCR_racing;
TPE_Vec3 _LCR_TPE_vec3DividePlain(TPE_Vec3 v, TPE_Unit d)
@ -53,12 +56,22 @@ TPE_Vec3 _LCR_TPE_vec3DividePlain(TPE_Vec3 v, TPE_Unit d)
TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist)
{
return TPE_envAABoxInside(point,TPE_vec3(0,0,0),TPE_vec3(
LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS,
(LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS) / 2,
LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS));
}
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;
}
uint8_t _LCR_racingCollisionHandler(uint16_t b1, uint16_t j1, uint16_t b2,
uint16_t j2, TPE_Vec3 p)
{
@ -75,8 +88,11 @@ uint8_t _LCR_racingCollisionHandler(uint16_t b1, uint16_t j1, uint16_t b2,
*/
void LCR_racingRestart(void)
{
TPE_bodyActivate(&(LCR_racing.carBody));
LCR_racing.wheelCollisions = 0;
TPE_bodyDeactivate(&(LCR_racing.carBody));
LCR_racing.wheelRotation = 0;
LCR_racing.wheelSteer = 0;
// TODO
}
@ -111,6 +127,7 @@ void LCR_racingInit(void)
LCR_racing.carBody.friction = LCR_CAR_FORWARD_FRICTION;
LCR_racing.carBody.elasticity = LCR_CAR_ELASTICITY;
LCR_racing.carBody.flags |= TPE_BODY_FLAG_ALWAYS_ACTIVE;
LCR_racingRestart();
}
@ -144,7 +161,6 @@ void LCR_racingGetCarTransform(LCR_GameUnit position[3],
rotation[0] = (v.x * LCR_GAME_UNIT) / TPE_F;
rotation[1] = (v.y * LCR_GAME_UNIT) / TPE_F;
rotation[2] = (v.z * LCR_GAME_UNIT) / TPE_F;
// TODO: also smooth out rotation?
}
@ -166,48 +182,93 @@ int LCR_racingCarWheelTouchesGround(int wheel)
return ((LCR_racing.wheelCollisions & (LCR_racing.wheelCollisions >> 4))
>> wheel) & 0x01;
}
LCR_GameUnit LCR_racingGetWheelRotation(void)
{
return LCR_racing.wheelRotation;
}
LCR_GameUnit LCR_racingGetWheelSteer(void)
{
return LCR_racing.wheelSteer;
}
void _LCR_racingWheelAccelerate(unsigned int wheel, TPE_Vec3 dir)
{
LCR_racing.carBody.joints[wheel].velocity[0] +=
(dir.x * LCR_CAR_ACCELERATION) / TPE_F;
LCR_racing.carBody.joints[wheel].velocity[1] +=
(dir.y * LCR_CAR_ACCELERATION) / TPE_F;
LCR_racing.carBody.joints[wheel].velocity[2] +=
(dir.z * LCR_CAR_ACCELERATION) / TPE_F;
}
void LCR_racingStep(unsigned int input)
{
TPE_Vec3 carForw, carRight, carUp;
TPE_Vec3 vel = TPE_vec3(0,0,0);
// TPE_Vec3 vel = TPE_vec3(0,0,0);
carForw = TPE_vec3Normalized(TPE_vec3Plus(
TPE_vec3Minus(LCR_racing.carBody.joints[0].position,
LCR_racing.carBody.joints[2].position),
TPE_vec3Minus(LCR_racing.carBody.joints[1].position,
LCR_racing.carBody.joints[3].position)));
carRight = TPE_vec3Normalized(TPE_vec3Plus(
TPE_vec3Minus(LCR_racing.carBody.joints[0].position,
LCR_racing.carBody.joints[1].position),
TPE_vec3Minus(LCR_racing.carBody.joints[2].position,
LCR_racing.carBody.joints[3].position)));
carUp = TPE_vec3Cross(carForw,carRight);
if (input)
{
// TODO: magic constants
if (input & LCR_RACING_INPUT_FORW)
vel.y = LCR_PHYSICS_UNIT / 8;
LCR_racing.wheelRotation =
(LCR_racing.wheelRotation + 5) % LCR_GAME_UNIT;
if (input & LCR_RACING_INPUT_BACK)
vel.z = LCR_PHYSICS_UNIT / 32;
LCR_racing.wheelRotation -= 4;
while (LCR_racing.wheelRotation < 0)
LCR_racing.wheelRotation += LCR_GAME_UNIT;
if (input & LCR_RACING_INPUT_RIGHT)
vel.x = LCR_PHYSICS_UNIT / 32;
if (input & LCR_RACING_INPUT_LEFT)
vel.x = -1 * LCR_PHYSICS_UNIT / 32;
LCR_racing.wheelSteer = TPE_min(
LCR_racing.wheelSteer + 64,LCR_GAME_UNIT / 2);
TPE_bodyAccelerate(&(LCR_racing.carBody),vel);
if (input & LCR_RACING_INPUT_LEFT)
LCR_racing.wheelSteer = TPE_max(
LCR_racing.wheelSteer - 64,-1 * LCR_GAME_UNIT / 2);
if ((LCR_racing.wheelCollisions & 0x03) == 0x03) // back wheels on gr.?
{
if (input & LCR_RACING_INPUT_FORW)
{
_LCR_racingWheelAccelerate(0,carForw);
_LCR_racingWheelAccelerate(1,carForw);
}
else if (input & LCR_RACING_INPUT_BACK)
{
_LCR_racingWheelAccelerate(0,TPE_vec3TimesPlain(carForw,-1));
_LCR_racingWheelAccelerate(1,TPE_vec3TimesPlain(carForw,-1));
}
}
}
TPE_bodyApplyGravity(&(LCR_racing.carBody),LCR_GRAVITY);
if ((!(input & LCR_RACING_INPUT_LEFT)) &&
(!(input & LCR_RACING_INPUT_RIGHT)))
LCR_racing.wheelSteer /= 2;
if ((LCR_racing.wheelCollisions & 0x0f) != 0x0f) // EXPERIMENTAL: don't apply gravity with all wheels on ground
TPE_bodyApplyGravity(&(LCR_racing.carBody),LCR_GRAVITY);
LCR_racing.wheelCollisions <<= 4;
TPE_worldStep(&(LCR_racing.physicsWorld));
carForw = TPE_vec3Normalized(TPE_vec3Plus(
TPE_vec3Minus(LCR_racing.carBody.joints[2].position,
LCR_racing.carBody.joints[0].position),
TPE_vec3Minus(LCR_racing.carBody.joints[3].position,
LCR_racing.carBody.joints[1].position)));
carRight = TPE_vec3Normalized(TPE_vec3Plus(
TPE_vec3Minus(LCR_racing.carBody.joints[1].position,
LCR_racing.carBody.joints[0].position),
TPE_vec3Minus(LCR_racing.carBody.joints[3].position,
LCR_racing.carBody.joints[2].position)));
carUp = TPE_vec3Cross(carForw,carRight);
if (TPE_vec3Dot(carUp,TPE_vec3Minus(LCR_racing.carBody.joints[4].position,
LCR_racing.carBody.joints[0].position)) < 0)
{