Clean a bit

This commit is contained in:
Miloslav Ciz 2024-09-29 21:41:06 +02:00
parent beda272f18
commit 3310008a0d
2 changed files with 57 additions and 118 deletions

138
racing.h
View file

@ -16,27 +16,14 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_PHYSICS_UNIT 2048 ///< length of map square for physics engine
#define TPE_RESHAPE_TENSION_LIMIT 10
#define TPE_RESHAPE_ITERATIONS 5
#define TPE_RESHAPE_ITERATIONS 5
#include "map.h"
#include "tinyphysicsengine.h"
// TODO: move some of this to constants?
#if 0
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 100)
#define LCR_CAR_FORWARD_FRICTION (TPE_F / 11)
#define LCR_CAR_TURN_FRICTION (4 * TPE_F / 4)
#define LCR_CAR_ELASTICITY (TPE_F / 100)
#define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 15)
#define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 20)
#define LCR_CAR_TURN_MAX (LCR_GAME_UNIT / 4)
#else
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 140)
#define LCR_CAR_FORWARD_FRICTION (TPE_F / 9)
#define LCR_CAR_TURN_FRICTION (TPE_F)
@ -45,8 +32,6 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 18)
#define LCR_CAR_TURN_MAX ((7 * LCR_GAME_UNIT) / 24)
#endif
#define LCR_CAR_JOINTS 5
#define LCR_CAR_CONNECTIONS 10
@ -339,52 +324,12 @@ if (LCR_currentMap.startPos[3])
(LCR_currentMap.startPos[3] == LCR_BLOCK_TRANSFORM_ROT_180 ? TPE_F / 2 :
(TPE_F / 4)),0));
/*
const uint8_t *b = LCR_currentMap.blocks;
for (int i = 0; i < LCR_currentMap.blockCount; ++i)
{
if (*b == LCR_BLOCK_START)
{
uint8_t x, y, z;
LCR_LOG1("placing car to start position");
LCR_mapBlockGetCoords(b,&x,&y,&z);
TPE_bodyMoveTo(&(LCR_racing.carBody),
TPE_vec3(
(((TPE_Unit) x) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT +
LCR_PHYSICS_UNIT / 2,
(((TPE_Unit) y) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT / 2 +
LCR_PHYSICS_UNIT / 4,
(((TPE_Unit) z) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT +
LCR_PHYSICS_UNIT / 2
));
// resuse x:
x = LCR_mapBlockGetTransform(b) & 0x60;
if (x)
TPE_bodyRotateByAxis(&(LCR_racing.carBody),
TPE_vec3(0,x == LCR_BLOCK_TRANSFORM_ROT_90 ? TPE_F / 4 :
(x == LCR_BLOCK_TRANSFORM_ROT_180 ? TPE_F / 2 : (3 * TPE_F / 4)),0));
}
b += LCR_BLOCK_SIZE;
}
*/
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
LCR_racing.carOKPositions[i] = TPE_vec3(0,0,0);
LCR_racing.carNotOKCount = 0;
// TODO
}
@ -676,13 +621,13 @@ LCR_racing.carPositions[0] =
if (angle < TPE_F / 4) // TODO: magic constant
{
LCR_LOG2("car roof low, applying anti force")
LCR_LOG2("roof squeezed, applying anti force")
tmpVec = TPE_vec3Times(carUp,LCR_PHYSICS_UNIT / 16); // TODO: 16 magic con.
if (angle <= 0)
{
LCR_LOG1("car roof flipped over, fixing")
LCR_LOG1("roof flipped over, fixing")
LCR_racing.carBody.joints[4].position = wheelAverage;
angle = 0;
}
@ -700,75 +645,68 @@ LCR_racing.carPositions[0] =
}
}
// now try to decide if car shape is OK
TPE_Unit v1, v2;
angle =
(TPE_vec3Dot(
TPE_vec3Minus(
v1 = TPE_vec3Dot(
TPE_vec3Minus(
LCR_racing.carBody.joints[1].position,
LCR_racing.carBody.joints[0].position),
TPE_vec3Minus(
LCR_racing.carBody.joints[2].position,
LCR_racing.carBody.joints[0].position)) +
TPE_vec3Dot(
LCR_racing.carBody.joints[3].position,
LCR_racing.carBody.joints[0].position));
v2 = TPE_vec3Dot(
TPE_vec3Minus(
LCR_racing.carBody.joints[1].position,
LCR_racing.carBody.joints[2].position),
TPE_vec3Minus(
LCR_racing.carBody.joints[0].position,
LCR_racing.carBody.joints[3].position),
TPE_vec3Minus(
LCR_racing.carBody.joints[2].position,
LCR_racing.carBody.joints[3].position)));
LCR_racing.carBody.joints[3].position,
LCR_racing.carBody.joints[2].position));
int carOK = 1; // TODO: for checking if car shape is fine, if we're inside a wall etc.
v1 = v1 > v2 ? ((v1 * LCR_GAME_UNIT) / TPE_nonZero(v2)) :
((v2 * LCR_GAME_UNIT) / TPE_nonZero(v1));
if (angle < 1900 || angle > 2350 ||
(LCR_racing.carBody.flags & TPE_BODY_FLAG_UNRESOLVED))
carOK = 0;
#define _TOLERANCE (LCR_GAME_UNIT / 8)
if (carOK)
if ((LCR_racing.carBody.flags & TPE_BODY_FLAG_UNRESOLVED) ||
v1 < (LCR_GAME_UNIT - _TOLERANCE) ||
v1 > (LCR_GAME_UNIT + _TOLERANCE))
{
LCR_racing.carNotOKCount -= LCR_racing.carNotOKCount ? 1 : 0;
// car not OK
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
LCR_racing.carOKPositions[i] = LCR_racing.carBody.joints[i].position;
}
else
{
if (LCR_racing.carNotOKCount > 5)
if (LCR_racing.carNotOKCount > 8) // TODO: constant
{
LCR_LOG1("car not OK, fixing");
LCR_LOG1("car not OK for some time, fixing");
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
{
// iterate towards previous OK position
LCR_racing.carBody.joints[i].position =
TPE_vec3Plus(
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;
//LCR_racing.carBody.joints[i].velocity[0] = 0;
//LCR_racing.carBody.joints[i].velocity[1] = 0;
//LCR_racing.carBody.joints[i].velocity[2] = 0;
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;
}
// TPE_bodyReshape(&(LCR_racing.carBody),
// LCR_racing.physicsWorld.environmentFunction);
// TODO
// LCR_racing.carNotOKCount = 0;
}
else
LCR_racing.carNotOKCount++;
}
else
{
// car OK
LCR_racing.carNotOKCount -= LCR_racing.carNotOKCount ? 1 : 0;
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
LCR_racing.carOKPositions[i] = LCR_racing.carBody.joints[i].position;
}
LCR_racing.tick++;