2023-09-16 22:52:03 +02:00
|
|
|
/**
|
|
|
|
racing module: implements the racing physics and logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LCR_RACING_H
|
|
|
|
#define _LCR_RACING_H
|
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
typedef int32_t LCR_GameUnit; ///< abstract game unit
|
2023-09-16 22:52:03 +02:00
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
#define LCR_GAME_UNIT 1024 ///< length of map square in LCR_GameUnits
|
2024-09-04 23:26:05 +02:00
|
|
|
|
2024-11-21 00:16:00 +01:00
|
|
|
#define LCR_RACING_INPUT_FORW 0x01
|
|
|
|
#define LCR_RACING_INPUT_RIGHT 0x02
|
|
|
|
#define LCR_RACING_INPUT_BACK 0x04
|
|
|
|
#define LCR_RACING_INPUT_LEFT 0x08
|
|
|
|
|
|
|
|
#define LCR_RACING_EVENT_CP_TAKEN 0x0001
|
|
|
|
#define LCR_RACING_EVENT_FINISHED 0x0002
|
|
|
|
#define LCR_RACING_EVENT_CRASH_SMALL 0x0004
|
|
|
|
#define LCR_RACING_EVENT_CRASH_BIG 0x0008
|
2024-09-05 22:51:11 +02:00
|
|
|
|
2024-12-03 22:31:19 +01:00
|
|
|
#define LCR_PHYSICS_UNIT 4096 ///< length of map square for physics engine
|
2024-09-28 01:47:05 +02:00
|
|
|
|
|
|
|
#define TPE_RESHAPE_TENSION_LIMIT 10
|
2024-09-29 21:41:06 +02:00
|
|
|
#define TPE_RESHAPE_ITERATIONS 5
|
2023-09-16 22:52:03 +02:00
|
|
|
|
|
|
|
#include "map.h"
|
|
|
|
#include "tinyphysicsengine.h"
|
|
|
|
|
2024-09-23 23:31:30 +02:00
|
|
|
// TODO: move some of this to constants?
|
2024-09-26 21:29:36 +02:00
|
|
|
|
2024-12-03 00:05:15 +01:00
|
|
|
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 160)
|
|
|
|
#define LCR_CAR_FORWARD_FRICTION (TPE_F / 180)
|
2024-12-03 22:31:19 +01:00
|
|
|
#define LCR_CAR_AIR_FRICTION 32
|
|
|
|
|
2024-12-03 22:59:07 +01:00
|
|
|
#define LCR_CAR_STAND_FRICTION_MULTIPLIER 32
|
2024-12-03 00:05:15 +01:00
|
|
|
#define LCR_CAR_STEER_FRICTION (TPE_F)
|
2024-12-03 22:31:19 +01:00
|
|
|
#define LCR_CAR_ELASTICITY (TPE_F / 150)
|
|
|
|
#define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 20)
|
|
|
|
|
|
|
|
#define LCR_CAR_STEER_SPEED (LCR_GAME_UNIT / 16)
|
2024-12-03 00:05:15 +01:00
|
|
|
#define LCR_CAR_STEER_MAX ((7 * LCR_GAME_UNIT) / 24)
|
|
|
|
|
2024-12-05 00:37:50 +01:00
|
|
|
// 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
|
2024-10-06 21:53:11 +02:00
|
|
|
|
2024-09-23 23:31:30 +02:00
|
|
|
#define LCR_CAR_JOINTS 5
|
|
|
|
#define LCR_CAR_CONNECTIONS 10
|
2024-09-09 19:16:51 +02:00
|
|
|
|
2024-09-04 23:26:05 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
TPE_World physicsWorld;
|
|
|
|
TPE_Body carBody;
|
2024-09-06 00:58:32 +02:00
|
|
|
TPE_Joint carJoints[LCR_CAR_JOINTS];
|
|
|
|
TPE_Connection carConnections[LCR_CAR_CONNECTIONS];
|
2024-09-10 21:49:23 +02:00
|
|
|
uint32_t tick;
|
2024-09-09 19:16:51 +02:00
|
|
|
uint8_t wheelCollisions; /**< In individual bits records for each car wheel
|
|
|
|
whether it's currently touching the ground.
|
|
|
|
Lower bits record current collisions, higher
|
|
|
|
bits the previous state (for averaging). */
|
|
|
|
|
2024-09-29 20:52:52 +02:00
|
|
|
TPE_Vec3 carPositions[2]; ///* Current and previous position in game units.
|
|
|
|
TPE_Vec3 carRotations[2]; ///* Current and previous rotation in game units.
|
2024-09-09 19:16:51 +02:00
|
|
|
|
2024-09-28 01:47:05 +02:00
|
|
|
|
|
|
|
TPE_Vec3 carOKPositions[LCR_CAR_JOINTS];
|
|
|
|
uint8_t carNotOKCount;
|
|
|
|
|
|
|
|
|
2024-09-10 15:30:07 +02:00
|
|
|
LCR_GameUnit wheelRotation;
|
|
|
|
LCR_GameUnit wheelSteer;
|
2024-12-03 00:05:15 +01:00
|
|
|
|
|
|
|
|
2024-12-03 22:59:07 +01:00
|
|
|
|
|
|
|
LCR_GameUnit carSpeed; ///* Signed speed (negative if backwards)
|
2024-12-03 00:05:15 +01:00
|
|
|
|
2024-09-04 23:26:05 +02:00
|
|
|
} LCR_racing;
|
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
TPE_Vec3 _LCR_TPE_vec3DividePlain(TPE_Vec3 v, TPE_Unit d)
|
|
|
|
{
|
2024-09-23 23:31:30 +02:00
|
|
|
v.x /= d; v.y /= d; v.z /= d;
|
2024-09-09 19:16:51 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2024-09-23 20:21:08 +02:00
|
|
|
TPE_Vec3 _LCR_racingBlockEnvFunc(TPE_Vec3 point, const uint8_t *block)
|
|
|
|
{
|
2024-10-04 00:59:15 +02:00
|
|
|
TPE_Vec3 v, vBest;
|
|
|
|
TPE_Unit d, dBest = TPE_INFINITY;
|
|
|
|
|
2024-09-23 20:21:08 +02:00
|
|
|
uint8_t bx, by, bz;
|
|
|
|
LCR_mapBlockGetCoords(block,&bx,&by,&bz);
|
2024-09-26 21:29:36 +02:00
|
|
|
|
2024-09-26 14:56:39 +02:00
|
|
|
TPE_Vec3 blockOffset = TPE_vec3(
|
|
|
|
(((int) bx) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT,
|
|
|
|
(((int) by) - LCR_MAP_SIZE_BLOCKS / 2) * (LCR_PHYSICS_UNIT / 2),
|
|
|
|
(((int) bz) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT);
|
2024-09-23 20:21:08 +02:00
|
|
|
|
2024-09-26 14:56:39 +02:00
|
|
|
point = TPE_vec3Minus(point,blockOffset); // shift to origin
|
2024-09-23 20:21:08 +02:00
|
|
|
|
2024-09-26 14:56:39 +02:00
|
|
|
uint8_t transform =
|
|
|
|
LCR_mapBlockOppositeTransform(LCR_mapBlockGetTransform(block));
|
2024-09-24 14:48:45 +02:00
|
|
|
|
2024-09-26 21:29:36 +02:00
|
|
|
LCR_TRANSFORM_COORDS(transform,point.x,point.y,point.z,LCR_PHYSICS_UNIT,
|
|
|
|
(LCR_PHYSICS_UNIT / 2))
|
2024-09-26 14:56:39 +02:00
|
|
|
|
2024-09-26 21:29:36 +02:00
|
|
|
point = TPE_vec3Minus(point,
|
|
|
|
TPE_vec3(LCR_PHYSICS_UNIT / 2,LCR_PHYSICS_UNIT / 4,LCR_PHYSICS_UNIT / 2));
|
2024-09-24 14:48:45 +02:00
|
|
|
|
|
|
|
switch (block[0])
|
|
|
|
{
|
|
|
|
case LCR_BLOCK_FULL:
|
|
|
|
case LCR_BLOCK_BOTTOM:
|
|
|
|
case LCR_BLOCK_LEFT:
|
|
|
|
case LCR_BLOCK_BOTTOM_LEFT:
|
|
|
|
case LCR_BLOCK_BOTTOM_LEFT_FRONT:
|
|
|
|
case LCR_BLOCK_FULL_ACCEL:
|
|
|
|
case LCR_BLOCK_FULL_FAN:
|
|
|
|
{
|
|
|
|
TPE_Vec3
|
|
|
|
offset = TPE_vec3(0,0,0),
|
|
|
|
size = TPE_vec3(LCR_PHYSICS_UNIT / 2,LCR_PHYSICS_UNIT / 4,
|
|
|
|
LCR_PHYSICS_UNIT / 2);
|
|
|
|
|
|
|
|
if (block[0] == LCR_BLOCK_BOTTOM ||
|
|
|
|
block[0] == LCR_BLOCK_BOTTOM_LEFT ||
|
|
|
|
block[0] == LCR_BLOCK_BOTTOM_LEFT_FRONT)
|
|
|
|
{
|
|
|
|
offset.y -= LCR_PHYSICS_UNIT / 8;
|
|
|
|
size.y = LCR_PHYSICS_UNIT / 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block[0] == LCR_BLOCK_LEFT ||
|
|
|
|
block[0] == LCR_BLOCK_BOTTOM_LEFT ||
|
|
|
|
block[0] == LCR_BLOCK_BOTTOM_LEFT_FRONT)
|
|
|
|
{
|
|
|
|
offset.x -= LCR_PHYSICS_UNIT / 4;
|
|
|
|
size.x = LCR_PHYSICS_UNIT / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block[0] == LCR_BLOCK_BOTTOM_LEFT_FRONT)
|
|
|
|
{
|
|
|
|
offset.z -= LCR_PHYSICS_UNIT / 4;
|
|
|
|
size.z = LCR_PHYSICS_UNIT / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
point = TPE_envAABox(point,offset,size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-10-04 00:59:15 +02:00
|
|
|
#define _CHECK_NEXT(check)\
|
|
|
|
v = check;\
|
|
|
|
d = TPE_dist(point,v);\
|
|
|
|
if (d < dBest) {\
|
|
|
|
vBest = v;\
|
|
|
|
dBest = d;}\
|
|
|
|
if (dBest == 0) {\
|
|
|
|
point = vBest;\
|
|
|
|
break;}
|
|
|
|
|
|
|
|
case LCR_BLOCK_RAMP_CURVED_WALL:
|
|
|
|
_CHECK_NEXT(TPE_envAABox(point,TPE_vec3(5 * LCR_PHYSICS_UNIT / 12,0,0),
|
|
|
|
TPE_vec3(LCR_PHYSICS_UNIT / 12,LCR_PHYSICS_UNIT / 4,LCR_PHYSICS_UNIT
|
|
|
|
/ 2)));
|
2024-10-06 21:53:11 +02:00
|
|
|
// fall through
|
2024-10-04 00:59:15 +02:00
|
|
|
|
|
|
|
case LCR_BLOCK_RAMP_CURVED_PLAT:
|
|
|
|
_CHECK_NEXT(TPE_envAABox(point,TPE_vec3(0,0,5 * LCR_PHYSICS_UNIT / 12),
|
|
|
|
TPE_vec3(LCR_PHYSICS_UNIT / 2,LCR_PHYSICS_UNIT / 4,LCR_PHYSICS_UNIT / 12
|
|
|
|
)));
|
2024-10-06 21:53:11 +02:00
|
|
|
// fall through
|
2024-10-04 00:59:15 +02:00
|
|
|
|
2024-10-03 00:24:28 +02:00
|
|
|
case LCR_BLOCK_RAMP_CURVED:
|
|
|
|
{
|
2024-10-04 00:59:15 +02:00
|
|
|
TPE_Unit sides[6];
|
|
|
|
TPE_Unit rampShift = block[0] != LCR_BLOCK_RAMP_CURVED ?
|
|
|
|
LCR_PHYSICS_UNIT / 6 : 0;
|
2024-10-03 00:24:28 +02:00
|
|
|
|
2024-10-04 00:59:15 +02:00
|
|
|
sides[0] = LCR_PHYSICS_UNIT / 8 - rampShift;
|
2024-10-03 00:24:28 +02:00
|
|
|
sides[1] = -1 * LCR_PHYSICS_UNIT / 4;
|
2024-10-04 00:59:15 +02:00
|
|
|
sides[2] = LCR_PHYSICS_UNIT / 2 - rampShift;
|
2024-10-03 00:24:28 +02:00
|
|
|
sides[3] = -1 * LCR_PHYSICS_UNIT / 4;
|
2024-10-04 00:59:15 +02:00
|
|
|
sides[4] = LCR_PHYSICS_UNIT / 2 - rampShift;
|
2024-10-03 00:24:28 +02:00
|
|
|
sides[5] = LCR_PHYSICS_UNIT / 4;
|
|
|
|
|
2024-10-04 00:59:15 +02:00
|
|
|
_CHECK_NEXT(TPE_envAATriPrism(point,
|
|
|
|
TPE_vec3(0,0,0),sides,LCR_PHYSICS_UNIT,2))
|
2024-10-03 00:24:28 +02:00
|
|
|
|
2024-10-04 00:59:15 +02:00
|
|
|
sides[0] = -1 * LCR_PHYSICS_UNIT / 2;
|
|
|
|
sides[1] = -1 * LCR_PHYSICS_UNIT / 4;
|
|
|
|
sides[2] = LCR_PHYSICS_UNIT / 2 - rampShift;
|
|
|
|
sides[3] = -1 * LCR_PHYSICS_UNIT / 4;
|
|
|
|
sides[4] = LCR_PHYSICS_UNIT / 2 - rampShift;
|
|
|
|
sides[5] = 0;
|
|
|
|
|
|
|
|
_CHECK_NEXT(TPE_envAATriPrism(point,TPE_vec3(0,0,0),sides,LCR_PHYSICS_UNIT
|
|
|
|
,2));
|
2024-10-03 00:24:28 +02:00
|
|
|
|
|
|
|
point = vBest;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-10-04 00:59:15 +02:00
|
|
|
#undef _CHECK_NEXT
|
|
|
|
|
2024-09-24 14:48:45 +02:00
|
|
|
case LCR_BLOCK_RAMP:
|
|
|
|
case LCR_BLOCK_RAMP_34:
|
|
|
|
case LCR_BLOCK_RAMP_12:
|
|
|
|
case LCR_BLOCK_RAMP_14:
|
2024-10-01 22:08:03 +02:00
|
|
|
case LCR_BLOCK_RAMP_STEEP:
|
2024-09-24 14:48:45 +02:00
|
|
|
{
|
2024-10-01 22:08:03 +02:00
|
|
|
uint8_t front, top;
|
|
|
|
LCR_rampGetDimensions(block[0],&top,&front);
|
|
|
|
front = 6 - front;
|
|
|
|
|
2024-09-24 14:48:45 +02:00
|
|
|
TPE_Unit sides[6];
|
2024-10-01 22:08:03 +02:00
|
|
|
sides[0] =
|
|
|
|
-1 * LCR_PHYSICS_UNIT / 2 + (LCR_PHYSICS_UNIT / 6) * ((int) front);
|
|
|
|
|
2024-09-26 14:56:39 +02:00
|
|
|
sides[1] = -1 * LCR_PHYSICS_UNIT / 4;
|
|
|
|
|
|
|
|
sides[2] = LCR_PHYSICS_UNIT / 2;
|
|
|
|
sides[3] = -1 * LCR_PHYSICS_UNIT / 4;
|
|
|
|
|
|
|
|
sides[4] = LCR_PHYSICS_UNIT / 2;
|
|
|
|
sides[5] = -1 * LCR_PHYSICS_UNIT / 4 +
|
2024-10-01 22:08:03 +02:00
|
|
|
((int) top) * (LCR_PHYSICS_UNIT / 8);
|
2024-09-24 14:48:45 +02:00
|
|
|
|
|
|
|
point = TPE_envAATriPrism(point,TPE_vec3(0,0,0),sides,LCR_PHYSICS_UNIT,2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-11-29 00:51:43 +01:00
|
|
|
case LCR_BLOCK_HILL:
|
|
|
|
{
|
|
|
|
point =
|
|
|
|
(point.y > -1 * LCR_PHYSICS_UNIT / 4 && point.z < LCR_PHYSICS_UNIT / 4) ?
|
|
|
|
TPE_envCylinder(point,
|
|
|
|
TPE_vec3(0,-1 * LCR_PHYSICS_UNIT / 2,LCR_PHYSICS_UNIT / 4),
|
|
|
|
TPE_vec3(LCR_PHYSICS_UNIT / 2,0,0),
|
|
|
|
LCR_PHYSICS_UNIT / 2 + LCR_PHYSICS_UNIT / 4) :
|
|
|
|
TPE_envAABox(point,TPE_vec3(0,0,0),
|
|
|
|
TPE_vec3(LCR_PHYSICS_UNIT / 2,LCR_PHYSICS_UNIT / 4,
|
|
|
|
LCR_PHYSICS_UNIT / 2));
|
|
|
|
|
|
|
|
if (point.y < -1 * LCR_PHYSICS_UNIT / 4) // for some reason happens somet.
|
|
|
|
point.y = -1 * LCR_PHYSICS_UNIT / 4;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-12-02 23:05:41 +01:00
|
|
|
case LCR_BLOCK_BUMP:
|
|
|
|
point = TPE_envCone(point,
|
|
|
|
TPE_vec3(0,-1 * LCR_PHYSICS_UNIT / 4 ,0),
|
|
|
|
TPE_vec3(0,LCR_PHYSICS_UNIT / 6,0),(5 * LCR_PHYSICS_UNIT) / 12);
|
|
|
|
break;
|
|
|
|
|
2024-10-01 22:38:49 +02:00
|
|
|
case LCR_BLOCK_CORNER:
|
|
|
|
case LCR_BLOCK_CORNER_12:
|
|
|
|
{
|
|
|
|
TPE_Unit sides[6];
|
|
|
|
sides[0] = -1 * LCR_PHYSICS_UNIT / 2;
|
|
|
|
sides[1] = LCR_PHYSICS_UNIT / 2;
|
|
|
|
sides[2] = -1 * LCR_PHYSICS_UNIT / 2;
|
|
|
|
sides[3] = -1 * LCR_PHYSICS_UNIT / 2;
|
|
|
|
sides[4] = block[0] == LCR_BLOCK_CORNER ? LCR_PHYSICS_UNIT / 2 : 0;
|
|
|
|
sides[5] = LCR_PHYSICS_UNIT / 2;
|
|
|
|
|
|
|
|
point = TPE_envAATriPrism(point,TPE_vec3(0,0,0),sides,
|
|
|
|
LCR_PHYSICS_UNIT / 2,1);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-09-24 14:48:45 +02:00
|
|
|
default:
|
|
|
|
point = TPE_vec3(0,0,LCR_MAP_SIZE_BLOCKS * LCR_PHYSICS_UNIT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-09-26 14:56:39 +02:00
|
|
|
point = TPE_vec3Plus(point,
|
|
|
|
TPE_vec3(LCR_PHYSICS_UNIT / 2,LCR_PHYSICS_UNIT / 4,LCR_PHYSICS_UNIT / 2));
|
2024-09-24 14:48:45 +02:00
|
|
|
|
2024-09-26 14:56:39 +02:00
|
|
|
transform = LCR_mapBlockOppositeTransform(transform);
|
2024-09-24 14:48:45 +02:00
|
|
|
|
2024-09-26 14:56:39 +02:00
|
|
|
LCR_TRANSFORM_COORDS(transform,point.x,point.y,point.z,LCR_PHYSICS_UNIT,
|
|
|
|
(LCR_PHYSICS_UNIT / 2))
|
2024-09-24 14:48:45 +02:00
|
|
|
|
2024-09-26 14:56:39 +02:00
|
|
|
point = TPE_vec3Plus(point,blockOffset); // shift back
|
|
|
|
|
|
|
|
return point;
|
2024-09-23 20:21:08 +02:00
|
|
|
}
|
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist)
|
2024-09-04 23:26:05 +02:00
|
|
|
{
|
2024-09-23 20:21:08 +02:00
|
|
|
// start with the map outside walls:
|
|
|
|
TPE_ENV_START(TPE_envAABoxInside(point,TPE_vec3(0,0,0),TPE_vec3(
|
2024-09-23 23:31:30 +02:00
|
|
|
LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS,
|
|
|
|
(LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS) / 2,
|
|
|
|
LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS)),point)
|
2024-09-23 20:21:08 +02:00
|
|
|
|
2024-09-26 14:56:39 +02:00
|
|
|
// without this check we might try to get block outside the map
|
|
|
|
if (_pBest.x == point.x && _pBest.y == point.y && _pBest.z == point.z)
|
|
|
|
return _pBest;
|
|
|
|
|
2024-09-23 20:21:08 +02:00
|
|
|
if (maxDist <= LCR_PHYSICS_UNIT / 4) // considering half of square height
|
|
|
|
{
|
|
|
|
/* Here we only check the 8 closest blocks => relatively fast. */
|
|
|
|
|
|
|
|
TPE_Vec3 pointShifted = TPE_vec3Plus(point,TPE_vec3(
|
|
|
|
(LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT,
|
|
|
|
(LCR_MAP_SIZE_BLOCKS / 4) * LCR_PHYSICS_UNIT,
|
|
|
|
(LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT));
|
|
|
|
|
|
|
|
uint8_t coords[6]; // x_low, x_high, y_low, y_high, z_low, z_high
|
|
|
|
|
|
|
|
coords[0] = (pointShifted.x / LCR_PHYSICS_UNIT);
|
|
|
|
coords[1] = (pointShifted.x % LCR_PHYSICS_UNIT < LCR_PHYSICS_UNIT / 2);
|
|
|
|
|
|
|
|
coords[2] = (pointShifted.y / (LCR_PHYSICS_UNIT / 2));
|
|
|
|
coords[3] =
|
|
|
|
(pointShifted.y % (LCR_PHYSICS_UNIT / 2) < LCR_PHYSICS_UNIT / 4);
|
|
|
|
|
|
|
|
coords[4] = (pointShifted.z / LCR_PHYSICS_UNIT);
|
|
|
|
coords[5] = (pointShifted.z % LCR_PHYSICS_UNIT < LCR_PHYSICS_UNIT / 2);
|
|
|
|
|
|
|
|
for (int i = 0; i < 6; i += 2)
|
|
|
|
if (coords[i + 1])
|
|
|
|
{
|
|
|
|
coords[i + 1] = coords[i];
|
|
|
|
coords[i] = coords[i] > 0 ? coords[i] - 1 : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
coords[i + 1] = coords[i] < 63 ? coords[i] + 1 : 63;
|
|
|
|
|
|
|
|
int start = 0, end = LCR_currentMap.blockCount - 1;
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
/* Black magic: here we make it so that we the lowest coord numbers
|
|
|
|
(0,0,0), then the highest (1,1,1), then second lowest (1,0,0), then
|
|
|
|
second highest (0,1,1) etc. This way we are narrowing the range (start,
|
|
|
|
end) for the binary search. */
|
|
|
|
|
|
|
|
int blockNum = LCR_mapGetBlockAtFast(
|
|
|
|
coords[0] + ((i ^ (i >> 1)) & 0x01),
|
|
|
|
coords[2] + ((i ^ (i >> 2)) & 0x01),
|
|
|
|
coords[4] + (i & 0x01),start,end);
|
|
|
|
|
|
|
|
if (blockNum >= 0) // is there a block at the coords?
|
|
|
|
{
|
|
|
|
TPE_ENV_NEXT(_LCR_racingBlockEnvFunc(point, // check it
|
|
|
|
LCR_currentMap.blocks + blockNum * LCR_BLOCK_SIZE),point)
|
|
|
|
|
|
|
|
// narrow the search range:
|
|
|
|
if (i % 2 == 0 && blockNum > start)
|
|
|
|
start = blockNum;
|
|
|
|
|
|
|
|
if (i % 2 && blockNum < end)
|
|
|
|
end = blockNum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-09-27 00:08:52 +02:00
|
|
|
LCR_LOG1("collision checking all blocks (shouldn't happen often!)");
|
|
|
|
|
2024-09-23 23:31:30 +02:00
|
|
|
const uint8_t *block = LCR_currentMap.blocks;
|
|
|
|
|
2024-09-23 20:21:08 +02:00
|
|
|
/* Full check of all map blocks, slow, shouldn't happen often! */
|
|
|
|
for (int j = 0; j < LCR_currentMap.blockCount; ++j)
|
|
|
|
{
|
|
|
|
TPE_ENV_NEXT(_LCR_racingBlockEnvFunc(point,block),point)
|
|
|
|
block += LCR_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TPE_ENV_END
|
2024-09-04 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2024-12-03 22:59:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
LCR_GameUnit LCR_racingGetCarSpeedUnsigned(void)
|
|
|
|
{
|
|
|
|
return LCR_racing.carSpeed >= 0 ? LCR_racing.carSpeed :
|
|
|
|
(-1 * LCR_racing.carSpeed);
|
|
|
|
}
|
|
|
|
|
|
|
|
LCR_GameUnit LCR_racingGetCarSpeedSigned(void)
|
2024-09-10 15:30:07 +02:00
|
|
|
{
|
2024-12-03 00:05:15 +01:00
|
|
|
return LCR_racing.carSpeed;
|
2024-09-10 15:30:07 +02:00
|
|
|
}
|
|
|
|
|
2024-12-03 22:59:07 +01:00
|
|
|
|
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
uint8_t _LCR_racingCollisionHandler(uint16_t b1, uint16_t j1, uint16_t b2,
|
|
|
|
uint16_t j2, TPE_Vec3 p)
|
|
|
|
{
|
|
|
|
// check which wheels are touching the ground.
|
|
|
|
|
|
|
|
if (j1 < 4) // wheel joint?
|
|
|
|
LCR_racing.wheelCollisions |= 0x01 << j1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-09-22 20:19:43 +02:00
|
|
|
LCR_GameUnit _LCR_racingSmoothRot(LCR_GameUnit angleNew, LCR_GameUnit angleOld)
|
|
|
|
{
|
|
|
|
/* We'll smooth small rotations by averaging the last two angles; bigger
|
|
|
|
rotations won't be smoothed -- firstly this removes lag for fast rotations
|
|
|
|
and also deals with the issue of averaging e.g. 1 and 359 degrees. */
|
|
|
|
|
|
|
|
LCR_GameUnit diff = angleNew - angleOld;
|
|
|
|
|
2024-09-23 23:31:30 +02:00
|
|
|
if (diff > LCR_GAME_UNIT / 8 || diff < -1 * LCR_GAME_UNIT / 8)
|
2024-09-22 20:19:43 +02:00
|
|
|
return angleNew;
|
|
|
|
|
|
|
|
return angleOld + diff / 2;
|
|
|
|
}
|
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
/**
|
|
|
|
Initializes new run.
|
|
|
|
*/
|
|
|
|
void LCR_racingRestart(void)
|
|
|
|
{
|
2024-09-27 00:08:52 +02:00
|
|
|
LCR_LOG0("restarting race");
|
|
|
|
|
2024-09-10 21:49:23 +02:00
|
|
|
LCR_racing.tick = 0;
|
|
|
|
|
2024-09-10 15:30:07 +02:00
|
|
|
TPE_bodyActivate(&(LCR_racing.carBody));
|
2024-09-09 19:16:51 +02:00
|
|
|
LCR_racing.wheelCollisions = 0;
|
2024-09-10 15:30:07 +02:00
|
|
|
|
|
|
|
LCR_racing.wheelRotation = 0;
|
|
|
|
LCR_racing.wheelSteer = 0;
|
2024-12-03 00:05:15 +01:00
|
|
|
LCR_racing.carSpeed = 0;
|
2024-09-09 19:16:51 +02:00
|
|
|
|
2024-09-22 20:19:43 +02:00
|
|
|
LCR_racing.carPositions[0] = TPE_vec3(0,0,0);
|
|
|
|
LCR_racing.carPositions[1] = LCR_racing.carPositions[0];
|
|
|
|
|
|
|
|
LCR_racing.carRotations[0] = TPE_vec3(0,0,0);
|
|
|
|
LCR_racing.carRotations[1] = LCR_racing.carRotations[0];
|
2024-12-03 22:31:19 +01:00
|
|
|
|
|
|
|
// make the car body:
|
|
|
|
TPE_makeCenterRectFull(LCR_racing.carJoints,
|
|
|
|
LCR_racing.carConnections,
|
|
|
|
LCR_PHYSICS_UNIT / 2,
|
|
|
|
(LCR_PHYSICS_UNIT * 3) / 4,
|
|
|
|
LCR_PHYSICS_UNIT / 8);
|
|
|
|
|
|
|
|
LCR_racing.carJoints[4].position.y += LCR_PHYSICS_UNIT / 6;
|
|
|
|
LCR_racing.carJoints[4].sizeDivided *= 3;
|
|
|
|
LCR_racing.carJoints[4].sizeDivided /= 2;
|
|
|
|
|
|
|
|
TPE_bodyInit(&(LCR_racing.carBody),LCR_racing.carJoints,LCR_CAR_JOINTS,
|
|
|
|
LCR_racing.carConnections,LCR_CAR_CONNECTIONS,TPE_F);
|
|
|
|
|
|
|
|
LCR_racing.carBody.friction = LCR_CAR_FORWARD_FRICTION;
|
|
|
|
LCR_racing.carBody.elasticity = LCR_CAR_ELASTICITY;
|
|
|
|
LCR_racing.carBody.flags |= TPE_BODY_FLAG_ALWAYS_ACTIVE;
|
|
|
|
|
|
|
|
/* We disable bounding sphere checks because that would lead to calling env.
|
|
|
|
function with large min. distance which would lead to slow iteration over
|
|
|
|
all map blocks. */
|
|
|
|
LCR_racing.carBody.flags |= TPE_BODY_FLAG_NO_BSPHERE;
|
|
|
|
|
2024-09-29 20:52:52 +02:00
|
|
|
TPE_bodyMoveTo(&(LCR_racing.carBody),
|
|
|
|
TPE_vec3(
|
|
|
|
(((TPE_Unit) LCR_currentMap.startPos[0]) - LCR_MAP_SIZE_BLOCKS / 2)
|
|
|
|
* LCR_PHYSICS_UNIT + LCR_PHYSICS_UNIT / 2,
|
|
|
|
(((TPE_Unit) LCR_currentMap.startPos[1]) - LCR_MAP_SIZE_BLOCKS / 2)
|
|
|
|
* LCR_PHYSICS_UNIT / 2 + LCR_PHYSICS_UNIT / 4,
|
|
|
|
(((TPE_Unit) LCR_currentMap.startPos[2]) - LCR_MAP_SIZE_BLOCKS / 2)
|
|
|
|
* LCR_PHYSICS_UNIT + LCR_PHYSICS_UNIT / 2));
|
|
|
|
|
|
|
|
// TODO: allow also flipping the car upside down on start?
|
|
|
|
if (LCR_currentMap.startPos[3])
|
|
|
|
TPE_bodyRotateByAxis(&(LCR_racing.carBody),
|
|
|
|
TPE_vec3(0,
|
|
|
|
LCR_currentMap.startPos[3] == LCR_BLOCK_TRANSFORM_ROT_90 ? 3 * TPE_F / 4 :
|
|
|
|
(LCR_currentMap.startPos[3] == LCR_BLOCK_TRANSFORM_ROT_180 ? TPE_F / 2 :
|
|
|
|
(TPE_F / 4)),0));
|
|
|
|
|
2024-09-28 01:47:05 +02:00
|
|
|
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
|
|
|
|
LCR_racing.carOKPositions[i] = TPE_vec3(0,0,0);
|
|
|
|
|
|
|
|
LCR_racing.carNotOKCount = 0;
|
|
|
|
|
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Initializes the racing module, only call once.
|
|
|
|
*/
|
2024-09-04 23:26:05 +02:00
|
|
|
void LCR_racingInit(void)
|
|
|
|
{
|
2024-09-27 00:08:52 +02:00
|
|
|
LCR_LOG0("initializing racing engine");
|
2024-09-04 23:26:05 +02:00
|
|
|
|
2024-09-05 22:51:11 +02:00
|
|
|
|
|
|
|
|
2024-12-03 22:31:19 +01:00
|
|
|
|
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
|
2024-09-04 23:26:05 +02:00
|
|
|
TPE_worldInit(&(LCR_racing.physicsWorld),
|
2024-09-09 19:16:51 +02:00
|
|
|
&(LCR_racing.carBody),1,_LCR_racingEnvironmentFunction);
|
2024-09-06 00:58:32 +02:00
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
LCR_racing.physicsWorld.collisionCallback = _LCR_racingCollisionHandler;
|
|
|
|
|
2024-09-04 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2024-09-22 20:19:43 +02:00
|
|
|
/**
|
|
|
|
Gets current car transformation intended for rendering, i.e. potentially with
|
|
|
|
smoothing and interpolation applied to the underlying internal state in the
|
|
|
|
physics engine.
|
|
|
|
*/
|
2024-09-05 22:51:11 +02:00
|
|
|
void LCR_racingGetCarTransform(LCR_GameUnit position[3],
|
2024-09-09 19:16:51 +02:00
|
|
|
LCR_GameUnit rotation[3], LCR_GameUnit interpolationParam)
|
2024-09-05 22:51:11 +02:00
|
|
|
{
|
2024-09-27 00:08:52 +02:00
|
|
|
LCR_LOG2("getting car transform");
|
|
|
|
|
2024-09-20 15:22:18 +02:00
|
|
|
TPE_Vec3 v;
|
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
#if LCR_SETTING_SMOOTH_ANIMATIONS
|
2024-09-23 23:31:30 +02:00
|
|
|
v = TPE_vec3Plus(LCR_racing.carPositions[1], // LERP previous and current pos
|
|
|
|
_LCR_TPE_vec3DividePlain(
|
|
|
|
TPE_vec3TimesPlain(TPE_vec3Minus(
|
|
|
|
LCR_racing.carPositions[0],LCR_racing.carPositions[1]),
|
|
|
|
interpolationParam),LCR_GAME_UNIT));
|
2024-09-09 19:16:51 +02:00
|
|
|
|
|
|
|
position[0] = v.x;
|
|
|
|
position[1] = v.y;
|
|
|
|
position[2] = v.z;
|
2024-09-22 20:19:43 +02:00
|
|
|
|
|
|
|
rotation[0] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].x,
|
|
|
|
LCR_racing.carRotations[1].x);
|
|
|
|
rotation[1] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].y,
|
|
|
|
LCR_racing.carRotations[1].y);
|
|
|
|
rotation[2] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].z,
|
|
|
|
LCR_racing.carRotations[1].z);
|
2024-09-09 19:16:51 +02:00
|
|
|
#else
|
|
|
|
position[0] = LCR_racing.carPositions[0].x;
|
|
|
|
position[1] = LCR_racing.carPositions[0].y;
|
|
|
|
position[2] = LCR_racing.carPositions[0].z;
|
|
|
|
|
2024-09-22 20:19:43 +02:00
|
|
|
rotation[0] = LCR_racing.carRotations[0].x;
|
|
|
|
rotation[1] = LCR_racing.carRotations[0].y;
|
|
|
|
rotation[2] = LCR_racing.carRotations[0].z;
|
|
|
|
#endif
|
2024-09-05 22:51:11 +02:00
|
|
|
}
|
|
|
|
|
2024-09-04 23:26:05 +02:00
|
|
|
void _LCR_drawPhysicsDebugPixel(uint16_t x, uint16_t y, uint8_t color)
|
|
|
|
{
|
2024-09-05 22:51:11 +02:00
|
|
|
if (x > 1 && x < LCR_EFFECTIVE_RESOLUTION_X - 2 &&
|
|
|
|
y > 1 && y < LCR_EFFECTIVE_RESOLUTION_Y - 2)
|
2024-09-04 23:26:05 +02:00
|
|
|
{
|
2024-09-05 22:51:11 +02:00
|
|
|
uint16_t c = 0x8101 | (0x8f1f << (2 * color));
|
2024-09-04 23:26:05 +02:00
|
|
|
|
2024-09-05 22:51:11 +02:00
|
|
|
for (int j = -1; j <= 2; ++j)
|
|
|
|
for (int i = -1; i <= 2; ++i)
|
|
|
|
LCR_drawPixelXYUnsafe(x + i,y + j,c);
|
2024-09-04 23:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
int LCR_racingCarWheelTouchesGround(int wheel)
|
|
|
|
{
|
|
|
|
return ((LCR_racing.wheelCollisions & (LCR_racing.wheelCollisions >> 4))
|
|
|
|
>> wheel) & 0x01;
|
|
|
|
}
|
2024-09-10 15:30:07 +02:00
|
|
|
|
|
|
|
LCR_GameUnit LCR_racingGetWheelRotation(void)
|
|
|
|
{
|
|
|
|
return LCR_racing.wheelRotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
LCR_GameUnit LCR_racingGetWheelSteer(void)
|
|
|
|
{
|
|
|
|
return LCR_racing.wheelSteer;
|
|
|
|
}
|
|
|
|
|
2024-12-05 00:37:50 +01:00
|
|
|
TPE_Unit _LCR_applyMaterialFactor(TPE_Unit value, uint8_t mat)
|
|
|
|
{
|
|
|
|
switch (mat)
|
|
|
|
{
|
|
|
|
case LCR_BLOCK_MATERIAL_GRASS:
|
|
|
|
value *= LCR_CAR_GRASS_FACTOR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LCR_BLOCK_MATERIAL_DIRT:
|
|
|
|
value *= LCR_CAR_DIRT_FACTOR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LCR_BLOCK_MATERIAL_ICE:
|
|
|
|
value *= LCR_CAR_ICE_FACTOR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: value *= 8; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value / 8;
|
|
|
|
}
|
|
|
|
|
2024-10-06 21:53:11 +02:00
|
|
|
void _LCR_racingWheelAccelerate(unsigned int wheel, TPE_Vec3 dir,
|
|
|
|
uint8_t material)
|
2024-09-10 15:30:07 +02:00
|
|
|
{
|
2024-10-06 21:53:11 +02:00
|
|
|
TPE_Unit acc =
|
2024-12-05 00:37:50 +01:00
|
|
|
_LCR_applyMaterialFactor(LCR_CAR_ACCELERATION,material);
|
2024-12-03 00:05:15 +01:00
|
|
|
|
2024-12-05 00:37:50 +01:00
|
|
|
acc = acc / (1 + (LCR_racingGetCarSpeedUnsigned() / LCR_CAR_AIR_FRICTION));
|
2024-12-03 00:05:15 +01:00
|
|
|
|
2024-09-10 15:30:07 +02:00
|
|
|
LCR_racing.carBody.joints[wheel].velocity[0] +=
|
2024-10-06 21:53:11 +02:00
|
|
|
(dir.x * acc) / TPE_F;
|
2024-09-10 15:30:07 +02:00
|
|
|
LCR_racing.carBody.joints[wheel].velocity[1] +=
|
2024-10-06 21:53:11 +02:00
|
|
|
(dir.y * acc) / TPE_F;
|
2024-09-10 15:30:07 +02:00
|
|
|
LCR_racing.carBody.joints[wheel].velocity[2] +=
|
2024-10-06 21:53:11 +02:00
|
|
|
(dir.z * acc) / TPE_F;
|
2024-09-10 15:30:07 +02:00
|
|
|
}
|
2024-09-09 19:16:51 +02:00
|
|
|
|
2024-09-30 01:58:21 +02:00
|
|
|
int _LCR_racingCarShapeOK(void)
|
|
|
|
{
|
|
|
|
int r = 1;
|
|
|
|
|
|
|
|
for (int i = 0; i < LCR_racing.carBody.jointCount; ++i)
|
|
|
|
r &= TPE_connectionTension(TPE_dist(
|
|
|
|
LCR_racing.carBody.joints[
|
|
|
|
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
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2024-09-10 17:29:22 +02:00
|
|
|
/**
|
|
|
|
Updates the racing physics world, call every LCR_RACING_TICK_MS milliseconds.
|
2024-11-21 00:16:00 +01:00
|
|
|
Returns a set of events (logically ORed) that occured during this step.
|
2024-09-10 17:29:22 +02:00
|
|
|
*/
|
2024-11-21 00:16:00 +01:00
|
|
|
uint32_t LCR_racingStep(unsigned int input)
|
2024-09-05 22:51:11 +02:00
|
|
|
{
|
2024-09-27 00:08:52 +02:00
|
|
|
LCR_LOG2("racing step start");
|
|
|
|
|
2024-11-21 00:16:00 +01:00
|
|
|
uint32_t result = 0;
|
2024-12-03 22:59:07 +01:00
|
|
|
TPE_Vec3 carForw, carRight, carUp, carVel;
|
2024-10-06 21:53:11 +02:00
|
|
|
uint8_t groundMat = LCR_BLOCK_MATERIAL_CONCRETE; // material under wheels
|
|
|
|
int groundBlockIndex = -1;
|
2024-09-10 15:30:07 +02:00
|
|
|
|
|
|
|
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);
|
2024-09-05 22:51:11 +02:00
|
|
|
|
2024-12-03 22:59:07 +01:00
|
|
|
carVel = TPE_vec3(
|
|
|
|
LCR_racing.carBody.joints[4].velocity[0],
|
|
|
|
LCR_racing.carBody.joints[4].velocity[1],
|
|
|
|
LCR_racing.carBody.joints[4].velocity[2]);
|
|
|
|
|
2024-10-06 21:53:11 +02:00
|
|
|
if ((LCR_racing.wheelCollisions & 0x0f) != 0x0f) // EXPERIMENTAL: don't apply gravity with all wheels on ground
|
|
|
|
TPE_bodyApplyGravity(&(LCR_racing.carBody),LCR_GRAVITY);
|
|
|
|
|
|
|
|
if (LCR_racing.wheelCollisions) // at least one wheel on ground?
|
|
|
|
{
|
|
|
|
TPE_Unit upDot = TPE_vec3Dot(carUp,TPE_vec3(0,TPE_F,0));
|
|
|
|
|
|
|
|
if (upDot > TPE_F / 8 || upDot < -1 * TPE_F / 8) // TODO: consts
|
|
|
|
{
|
|
|
|
uint8_t
|
|
|
|
gx = (LCR_racing.carPositions[0].x + (LCR_MAP_SIZE_BLOCKS / 2) *
|
|
|
|
LCR_GAME_UNIT) / LCR_GAME_UNIT,
|
|
|
|
gy = (LCR_racing.carPositions[0].y + (LCR_MAP_SIZE_BLOCKS / 2) *
|
|
|
|
(LCR_GAME_UNIT / 2)) / (LCR_GAME_UNIT / 2),
|
|
|
|
gz = (LCR_racing.carPositions[0].z + (LCR_MAP_SIZE_BLOCKS / 2) *
|
|
|
|
LCR_GAME_UNIT) / LCR_GAME_UNIT;
|
|
|
|
|
|
|
|
TPE_Unit yMod = (LCR_racing.carPositions[0].y + LCR_MAP_SIZE_BLOCKS *
|
|
|
|
LCR_GAME_UNIT / 2) % (LCR_GAME_UNIT / 2);
|
|
|
|
|
|
|
|
if (upDot > 0 && yMod < LCR_GAME_UNIT / 2) // TODO: const
|
|
|
|
groundBlockIndex = LCR_mapGetBlockAt(gx,gy - 1,gz);
|
|
|
|
else if (upDot < 0 && yMod > LCR_GAME_UNIT / 2)
|
|
|
|
groundBlockIndex = LCR_mapGetBlockAt(gx,gy + 1,gz);
|
|
|
|
|
|
|
|
if (groundBlockIndex == -1)
|
|
|
|
groundBlockIndex = LCR_mapGetBlockAt(gx,gy,gz);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groundBlockIndex != -1)
|
|
|
|
groundMat = LCR_mapBlockGetMaterial(
|
|
|
|
LCR_currentMap.blocks + groundBlockIndex * LCR_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
LCR_racing.carBody.friction =
|
2024-12-05 00:37:50 +01:00
|
|
|
_LCR_applyMaterialFactor(LCR_CAR_FORWARD_FRICTION,groundMat);
|
2024-10-06 21:53:11 +02:00
|
|
|
|
2024-12-03 22:59:07 +01:00
|
|
|
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;
|
2024-12-03 00:05:15 +01:00
|
|
|
|
2024-09-05 22:51:11 +02:00
|
|
|
if (input)
|
|
|
|
{
|
2024-09-10 17:29:22 +02:00
|
|
|
unsigned char steering = 0;
|
2024-12-03 22:59:07 +01:00
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
2024-09-10 17:29:22 +02:00
|
|
|
|
2024-09-10 15:30:07 +02:00
|
|
|
// TODO: magic constants
|
|
|
|
|
2024-09-10 17:29:22 +02:00
|
|
|
if (input & (LCR_RACING_INPUT_FORW | LCR_RACING_INPUT_BACK))
|
|
|
|
{
|
2024-09-10 21:49:23 +02:00
|
|
|
LCR_GameUnit rotateBy =
|
|
|
|
(LCR_racing.wheelCollisions & 0x0f) ? // on ground slow down wheel rot.
|
2024-12-03 22:59:07 +01:00
|
|
|
(LCR_racingGetCarSpeedUnsigned() / 16) : LCR_GAME_UNIT / 32;
|
2024-09-10 17:29:22 +02:00
|
|
|
|
2024-09-10 21:49:23 +02:00
|
|
|
if (!(input & LCR_RACING_INPUT_BACK))
|
|
|
|
rotateBy *= -1;
|
|
|
|
|
|
|
|
LCR_racing.wheelRotation =
|
|
|
|
(LCR_racing.wheelRotation + rotateBy) % LCR_GAME_UNIT;
|
2024-09-10 15:30:07 +02:00
|
|
|
|
2024-09-10 17:29:22 +02:00
|
|
|
if (LCR_racing.wheelRotation < 0)
|
2024-09-10 15:30:07 +02:00
|
|
|
LCR_racing.wheelRotation += LCR_GAME_UNIT;
|
2024-09-10 17:29:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 22:51:11 +02:00
|
|
|
if (input & LCR_RACING_INPUT_RIGHT)
|
2024-09-10 17:29:22 +02:00
|
|
|
{
|
|
|
|
steering = 2;
|
2024-09-10 21:49:23 +02:00
|
|
|
|
2024-09-10 15:30:07 +02:00
|
|
|
LCR_racing.wheelSteer = TPE_min(
|
2024-12-03 00:05:15 +01:00
|
|
|
LCR_racing.wheelSteer + LCR_CAR_STEER_SPEED,
|
|
|
|
LCR_CAR_STEER_MAX);
|
2024-09-10 17:29:22 +02:00
|
|
|
}
|
|
|
|
else if (input & LCR_RACING_INPUT_LEFT)
|
|
|
|
{
|
|
|
|
steering = 1;
|
2024-09-10 21:49:23 +02:00
|
|
|
|
2024-09-10 15:30:07 +02:00
|
|
|
LCR_racing.wheelSteer = TPE_max(
|
2024-12-03 00:05:15 +01:00
|
|
|
LCR_racing.wheelSteer - LCR_CAR_STEER_SPEED,
|
|
|
|
-1 * LCR_CAR_STEER_MAX);
|
2024-09-10 17:29:22 +02:00
|
|
|
}
|
2024-09-10 15:30:07 +02:00
|
|
|
|
2024-09-10 17:29:22 +02:00
|
|
|
if ((LCR_racing.wheelCollisions & 0x0c)) // back wheel on ground?
|
2024-09-10 15:30:07 +02:00
|
|
|
{
|
|
|
|
if (input & LCR_RACING_INPUT_FORW)
|
|
|
|
{
|
2024-10-06 21:53:11 +02:00
|
|
|
_LCR_racingWheelAccelerate(0,carForw,groundMat);
|
|
|
|
_LCR_racingWheelAccelerate(1,carForw,groundMat);
|
2024-09-10 15:30:07 +02:00
|
|
|
}
|
|
|
|
else if (input & LCR_RACING_INPUT_BACK)
|
|
|
|
{
|
2024-10-06 21:53:11 +02:00
|
|
|
_LCR_racingWheelAccelerate(0,TPE_vec3TimesPlain(carForw,-1),groundMat);
|
|
|
|
_LCR_racingWheelAccelerate(1,TPE_vec3TimesPlain(carForw,-1),groundMat);
|
2024-09-10 15:30:07 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-10 17:29:22 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
if (LCR_racing.wheelCollisions & (0x01 << i)) // wheel on ground?
|
|
|
|
{
|
|
|
|
TPE_Vec3 jv = TPE_vec3( // joint velocity
|
|
|
|
LCR_racing.carBody.joints[i].velocity[0],
|
|
|
|
LCR_racing.carBody.joints[i].velocity[1],
|
|
|
|
LCR_racing.carBody.joints[i].velocity[2]);
|
|
|
|
|
|
|
|
TPE_Vec3 ja = carRight; // wheel axis of rotation
|
|
|
|
|
|
|
|
if (i >= 2 && steering)
|
|
|
|
{
|
|
|
|
// for front wheels with turning we tilt the wheel axis 45 degrees
|
|
|
|
|
|
|
|
TPE_Unit steer =
|
|
|
|
(LCR_racing.wheelSteer * TPE_F) / LCR_GAME_UNIT;
|
2024-09-20 15:22:18 +02:00
|
|
|
|
2024-09-10 17:29:22 +02:00
|
|
|
ja = TPE_vec3Normalized(
|
|
|
|
TPE_vec3Plus(TPE_vec3Times(carForw,steer),carRight));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* friction is in the direction if the axis and its magnitude is
|
|
|
|
determined by the dot product (angle) of the axis and velocity */
|
2024-12-03 22:31:19 +01:00
|
|
|
|
|
|
|
|
2024-12-05 00:37:50 +01:00
|
|
|
|
2024-09-10 17:29:22 +02:00
|
|
|
TPE_Vec3 fric = TPE_vec3Times(ja,(TPE_vec3Dot(ja,jv) *
|
2024-12-05 00:37:50 +01:00
|
|
|
_LCR_applyMaterialFactor(LCR_CAR_STEER_FRICTION,groundMat)) / TPE_F);
|
2024-09-10 17:29:22 +02:00
|
|
|
|
|
|
|
jv = TPE_vec3Minus(jv,fric); // subtract the friction
|
|
|
|
|
|
|
|
LCR_racing.carBody.joints[i].velocity[0] = jv.x;
|
|
|
|
LCR_racing.carBody.joints[i].velocity[1] = jv.y;
|
|
|
|
LCR_racing.carBody.joints[i].velocity[2] = jv.z;
|
|
|
|
}
|
2024-09-05 22:51:11 +02:00
|
|
|
}
|
|
|
|
|
2024-09-10 15:30:07 +02:00
|
|
|
if ((!(input & LCR_RACING_INPUT_LEFT)) &&
|
|
|
|
(!(input & LCR_RACING_INPUT_RIGHT)))
|
|
|
|
LCR_racing.wheelSteer /= 2;
|
2024-09-09 19:16:51 +02:00
|
|
|
|
2024-09-10 15:30:07 +02:00
|
|
|
LCR_racing.wheelCollisions <<= 4;
|
2024-09-27 00:08:52 +02:00
|
|
|
|
|
|
|
LCR_LOG2("gonna step physics engine");
|
2024-09-10 15:30:07 +02:00
|
|
|
TPE_worldStep(&(LCR_racing.physicsWorld));
|
2024-09-27 00:08:52 +02:00
|
|
|
LCR_LOG2("stepping physics engine done");
|
2024-09-09 19:16:51 +02:00
|
|
|
|
2024-12-03 22:59:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
LCR_racing.carSpeed = (TPE_vec3Len(carVel) * LCR_GAME_UNIT)
|
2024-12-03 00:05:15 +01:00
|
|
|
/ LCR_PHYSICS_UNIT;
|
|
|
|
|
2024-12-03 22:59:07 +01:00
|
|
|
|
|
|
|
if (TPE_vec3Dot(carVel,carForw) < 0)
|
|
|
|
LCR_racing.carSpeed *= -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-09 21:25:22 +02:00
|
|
|
TPE_Vec3 tmpVec = LCR_racing.carPositions[0];
|
|
|
|
|
2024-10-06 21:53:11 +02:00
|
|
|
TPE_Vec3 wheelAverage = _LCR_TPE_vec3DividePlain(
|
2024-09-28 01:47:05 +02:00
|
|
|
TPE_vec3Plus(
|
2024-10-06 21:53:11 +02:00
|
|
|
TPE_vec3Plus(
|
|
|
|
LCR_racing.carBody.joints[0].position,
|
|
|
|
LCR_racing.carBody.joints[1].position),
|
|
|
|
TPE_vec3Plus(
|
|
|
|
LCR_racing.carBody.joints[2].position,
|
|
|
|
LCR_racing.carBody.joints[3].position)),4);
|
|
|
|
|
|
|
|
LCR_racing.carPositions[0] = _LCR_TPE_vec3DividePlain(
|
|
|
|
TPE_vec3TimesPlain(wheelAverage,LCR_GAME_UNIT),LCR_PHYSICS_UNIT);
|
2024-09-23 23:31:30 +02:00
|
|
|
|
|
|
|
LCR_racing.carPositions[0] = // smooth the position
|
|
|
|
TPE_vec3KeepWithinBox(LCR_racing.carPositions[1],LCR_racing.carPositions[0],
|
|
|
|
TPE_vec3(
|
|
|
|
LCR_PHYSICS_UNIT / 64, // TODO: constant
|
|
|
|
LCR_PHYSICS_UNIT / 64,
|
|
|
|
LCR_PHYSICS_UNIT / 64));
|
2024-09-09 21:25:22 +02:00
|
|
|
|
|
|
|
LCR_racing.carPositions[1] = tmpVec;
|
2024-09-10 21:49:23 +02:00
|
|
|
|
2024-09-23 23:31:30 +02:00
|
|
|
tmpVec = _LCR_TPE_vec3DividePlain(TPE_vec3TimesPlain(TPE_bodyGetRotation(
|
|
|
|
&(LCR_racing.carBody),0,2,1),LCR_GAME_UNIT),TPE_F);
|
2024-09-22 20:19:43 +02:00
|
|
|
|
|
|
|
LCR_racing.carRotations[1] = LCR_racing.carRotations[0];
|
|
|
|
LCR_racing.carRotations[0] = tmpVec;
|
|
|
|
|
2024-10-06 21:53:11 +02:00
|
|
|
TPE_Unit angle = TPE_vec3Dot(carUp,TPE_vec3Normalized(TPE_vec3Minus(
|
|
|
|
LCR_racing.carBody.joints[4].position,
|
|
|
|
LCR_racing.carBody.joints[0].position)));
|
2024-09-28 01:47:05 +02:00
|
|
|
|
2024-12-04 23:50:50 +01:00
|
|
|
if (angle < TPE_F / 64) // TODO: magic constant
|
2024-09-28 01:47:05 +02:00
|
|
|
{
|
2024-09-29 21:41:06 +02:00
|
|
|
LCR_LOG2("roof squeezed, applying anti force")
|
2024-09-28 01:47:05 +02:00
|
|
|
|
|
|
|
tmpVec = TPE_vec3Times(carUp,LCR_PHYSICS_UNIT / 16); // TODO: 16 magic con.
|
|
|
|
|
2024-12-04 23:50:50 +01:00
|
|
|
angle = TPE_F - 4 * angle; // 4 comes from above TPE_F / 4
|
|
|
|
|
|
|
|
tmpVec = TPE_vec3Times(tmpVec,angle);
|
|
|
|
|
2024-09-29 20:52:52 +02:00
|
|
|
if (angle <= 0)
|
2024-09-28 01:47:05 +02:00
|
|
|
{
|
2024-09-29 21:41:06 +02:00
|
|
|
LCR_LOG1("roof flipped over, fixing")
|
2024-09-28 01:47:05 +02:00
|
|
|
LCR_racing.carBody.joints[4].position = wheelAverage;
|
2024-09-29 20:52:52 +02:00
|
|
|
angle = 0;
|
2024-09-28 01:47:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// accelerate roof and wheels away from each other
|
|
|
|
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
|
|
|
|
{
|
|
|
|
LCR_racing.carBody.joints[i].velocity[0] += (i == 4 ? 1 : -1) * tmpVec.x;
|
|
|
|
LCR_racing.carBody.joints[i].velocity[1] += (i == 4 ? 1 : -1) * tmpVec.y;
|
|
|
|
LCR_racing.carBody.joints[i].velocity[2] += (i == 4 ? 1 : -1) * tmpVec.z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 01:32:30 +02:00
|
|
|
// now make a special test for a "pinch" front collision
|
2024-10-06 21:53:11 +02:00
|
|
|
|
2024-10-07 01:32:30 +02:00
|
|
|
TPE_Vec3 frontWheelMiddle = TPE_vec3Plus(
|
|
|
|
LCR_racing.carBody.joints[0].position,
|
|
|
|
LCR_racing.carBody.joints[1].position);
|
2024-10-06 21:53:11 +02:00
|
|
|
|
2024-10-07 01:32:30 +02:00
|
|
|
frontWheelMiddle.x /= 2;
|
|
|
|
frontWheelMiddle.y /= 2;
|
|
|
|
frontWheelMiddle.z /= 2;
|
2024-10-06 21:53:11 +02:00
|
|
|
|
2024-10-07 01:32:30 +02:00
|
|
|
frontWheelMiddle = TPE_vec3Minus(frontWheelMiddle,
|
|
|
|
_LCR_racingEnvironmentFunction(frontWheelMiddle,LCR_PHYSICS_UNIT / 4));
|
2024-10-06 21:53:11 +02:00
|
|
|
|
2024-10-07 01:32:30 +02:00
|
|
|
uint8_t frontCollision = frontWheelMiddle.x == 0 && frontWheelMiddle.y == 0 &&
|
|
|
|
frontWheelMiddle.z == 0;
|
2024-10-06 21:53:11 +02:00
|
|
|
|
2024-10-07 01:32:30 +02:00
|
|
|
if (frontCollision)
|
|
|
|
{
|
|
|
|
// stop the car immediately
|
|
|
|
LCR_LOG1("car front pierced");
|
|
|
|
LCR_racing.carNotOKCount += (LCR_racing.carNotOKCount < 15 ? 32 : 0); // TODO: consts
|
|
|
|
}
|
2024-09-28 01:47:05 +02:00
|
|
|
|
2024-10-07 01:32:30 +02:00
|
|
|
if ((LCR_racing.carBody.flags & TPE_BODY_FLAG_UNRESOLVED) || frontCollision ||
|
|
|
|
!_LCR_racingCarShapeOK())
|
2024-09-28 01:47:05 +02:00
|
|
|
{
|
2024-10-07 01:32:30 +02:00
|
|
|
// car not OK
|
2024-09-28 01:47:05 +02:00
|
|
|
|
2024-12-05 00:37:50 +01:00
|
|
|
if (LCR_racing.carNotOKCount > 10) // TODO: constant
|
2024-09-29 20:52:52 +02:00
|
|
|
{
|
2024-10-07 01:32:30 +02:00
|
|
|
LCR_LOG1("car not OK (short), fixing");
|
|
|
|
|
|
|
|
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
|
2024-09-30 02:28:27 +02:00
|
|
|
{
|
2024-12-05 00:37:50 +01:00
|
|
|
if (LCR_racing.carNotOKCount < 20) // TODO: const
|
2024-10-07 01:32:30 +02:00
|
|
|
{
|
|
|
|
// for a while try to smoothly iterate towards previous OK position
|
|
|
|
LCR_racing.carBody.joints[i].position =
|
|
|
|
TPE_vec3Plus(LCR_racing.carBody.joints[i].position,
|
|
|
|
LCR_racing.carOKPositions[i]);
|
|
|
|
|
|
|
|
LCR_racing.carBody.joints[i].position.x /= 2;
|
|
|
|
LCR_racing.carBody.joints[i].position.y /= 2;
|
|
|
|
LCR_racing.carBody.joints[i].position.z /= 2;
|
|
|
|
|
|
|
|
for (int j = 0; j < 3; ++j) // lower speed a bit
|
|
|
|
LCR_racing.carBody.joints[i].velocity[j] =
|
|
|
|
(7 * ((int) LCR_racing.carBody.joints[i].velocity[j])) / 8;
|
|
|
|
}
|
|
|
|
else // hard set the pos (iteration may be infinite due to sim.)
|
|
|
|
{
|
|
|
|
LCR_LOG1("car not OK (long), teleporting");
|
|
|
|
LCR_racing.carBody.joints[i].position = LCR_racing.carOKPositions[i];
|
|
|
|
}
|
2024-09-30 02:28:27 +02:00
|
|
|
}
|
2024-09-29 20:52:52 +02:00
|
|
|
}
|
2024-09-30 02:28:27 +02:00
|
|
|
|
2024-10-07 01:32:30 +02:00
|
|
|
LCR_racing.carNotOKCount += LCR_racing.carNotOKCount < 255 ? 1 : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// car OK
|
|
|
|
LCR_racing.carNotOKCount = 0;
|
2024-09-29 20:52:52 +02:00
|
|
|
|
2024-10-07 01:32:30 +02:00
|
|
|
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
|
|
|
|
LCR_racing.carOKPositions[i] = LCR_racing.carBody.joints[i].position;
|
|
|
|
}
|
2024-09-28 01:47:05 +02:00
|
|
|
|
2024-09-10 21:49:23 +02:00
|
|
|
LCR_racing.tick++;
|
2024-09-27 00:08:52 +02:00
|
|
|
|
|
|
|
LCR_LOG2("racing step end");
|
2024-11-21 00:16:00 +01:00
|
|
|
|
|
|
|
return result;
|
2024-09-05 22:51:11 +02:00
|
|
|
}
|
|
|
|
|
2024-09-04 23:26:05 +02:00
|
|
|
void LCR_physicsDebugDraw(LCR_GameUnit camPos[3], LCR_GameUnit camRot[2],
|
|
|
|
LCR_GameUnit camFov)
|
|
|
|
{
|
2024-09-27 00:08:52 +02:00
|
|
|
LCR_LOG2("drawing physics debug overlay");
|
|
|
|
|
2024-09-04 23:26:05 +02:00
|
|
|
TPE_Vec3 cPos, cRot, cView;
|
|
|
|
|
|
|
|
cPos.x = (camPos[0] * LCR_PHYSICS_UNIT) / LCR_GAME_UNIT;
|
|
|
|
cPos.y = (camPos[1] * LCR_PHYSICS_UNIT) / LCR_GAME_UNIT;
|
|
|
|
cPos.z = (camPos[2] * LCR_PHYSICS_UNIT) / LCR_GAME_UNIT;
|
|
|
|
|
2024-09-09 19:16:51 +02:00
|
|
|
cRot.x = (camRot[0] * TPE_F) / LCR_GAME_UNIT;
|
|
|
|
cRot.y = (camRot[1] * TPE_F) / LCR_GAME_UNIT;
|
2024-09-04 23:26:05 +02:00
|
|
|
cRot.z = 0;
|
|
|
|
|
|
|
|
cView.x = LCR_EFFECTIVE_RESOLUTION_X;
|
|
|
|
cView.y = LCR_EFFECTIVE_RESOLUTION_Y;
|
2024-09-09 19:16:51 +02:00
|
|
|
cView.z = (camFov * TPE_F) / LCR_GAME_UNIT;
|
2024-09-04 23:26:05 +02:00
|
|
|
|
|
|
|
TPE_worldDebugDraw(&(LCR_racing.physicsWorld),_LCR_drawPhysicsDebugPixel,
|
2024-09-24 14:48:45 +02:00
|
|
|
cPos,cRot,cView,16,LCR_PHYSICS_UNIT / 4,LCR_racing.tick * 4);
|
2024-09-04 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2023-09-16 22:52:03 +02:00
|
|
|
#endif // guard
|