Handle start position

This commit is contained in:
Miloslav Ciz 2024-09-29 20:52:52 +02:00
parent 34293981b0
commit beda272f18
4 changed files with 146 additions and 162 deletions

172
racing.h
View file

@ -62,9 +62,8 @@ struct
Lower bits record current collisions, higher
bits the previous state (for averaging). */
TPE_Vec3 carPositions[2]; ///* Current and previous position.
TPE_Vec3 carRotations[2]; ///* Current and previous rotation.
TPE_Vec3 carPositions[2]; ///* Current and previous position in game units.
TPE_Vec3 carRotations[2]; ///* Current and previous rotation in game units.
TPE_Vec3 carOKPositions[LCR_CAR_JOINTS];
@ -322,6 +321,61 @@ void LCR_racingRestart(void)
LCR_racing.carRotations[0] = TPE_vec3(0,0,0);
LCR_racing.carRotations[1] = LCR_racing.carRotations[0];
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));
/*
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)
@ -373,9 +427,6 @@ void LCR_racingInit(void)
all map blocks. */
LCR_racing.carBody.flags |= TPE_BODY_FLAG_NO_BSPHERE;
TPE_bodyRotateByAxis(&LCR_racing.carBody,
TPE_vec3(TPE_F / 3,0,0));
LCR_racingRestart();
}
@ -616,29 +667,29 @@ LCR_racing.carPositions[0] =
LCR_racing.carRotations[0] = tmpVec;
int carOK = 1; // TODO: for checking if car shape is fine, if we're inside a wall etc.
TPE_Unit roofAngle =
TPE_vec3Dot(carUp,TPE_vec3Normalized(TPE_vec3Minus(LCR_racing.carBody.joints[4].position,
LCR_racing.carBody.joints[0].position)));
TPE_Unit angle =
TPE_vec3Dot(carUp,TPE_vec3Normalized(TPE_vec3Minus(
LCR_racing.carBody.joints[4].position,
LCR_racing.carBody.joints[0].position)));
if (roofAngle < TPE_F / 4) // TODO: magic constant
if (angle < TPE_F / 4) // TODO: magic constant
{
LCR_LOG2("car roof low, applying anti force")
tmpVec = TPE_vec3Times(carUp,LCR_PHYSICS_UNIT / 16); // TODO: 16 magic con.
if (roofAngle <= 0)
if (angle <= 0)
{
LCR_LOG1("car roof flipped over, fixing")
LCR_racing.carBody.joints[4].position = wheelAverage;
roofAngle = 0;
angle = 0;
}
roofAngle = TPE_F - 4 * roofAngle; // 4 comes from above TPE_F / 4
angle = TPE_F - 4 * angle; // 4 comes from above TPE_F / 4
tmpVec = TPE_vec3Times(tmpVec,roofAngle);
tmpVec = TPE_vec3Times(tmpVec,angle);
// accelerate roof and wheels away from each other
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
@ -650,76 +701,75 @@ int carOK = 1; // TODO: for checking if car shape is fine, if we're inside a wal
}
angle =
(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(
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)));
int carOK = 1; // TODO: for checking if car shape is fine, if we're inside a wall etc.
if (angle < 1900 || angle > 2350 ||
(LCR_racing.carBody.flags & TPE_BODY_FLAG_UNRESOLVED))
carOK = 0;
if (1 /*carOK*/)
if (carOK)
{
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;
}
else
{
LCR_racing.carNotOKCount++;
// if (LCR_racing.carNotOKCount > 4)
if (LCR_racing.carNotOKCount > 5)
{
LCR_LOG1("car not OK, fixing");
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
{
LCR_racing.carBody.joints[i].position = LCR_racing.carOKPositions[i];
{
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;
}
TPE_bodyReshape(&(LCR_racing.carBody),
LCR_racing.physicsWorld.environmentFunction);
}
// TPE_bodyReshape(&(LCR_racing.carBody),
// LCR_racing.physicsWorld.environmentFunction);
// TODO
LCR_racing.carNotOKCount = 0;
// LCR_racing.carNotOKCount = 0;
}
else
LCR_racing.carNotOKCount++;
}
#if 0
if (TPE_vec3Dot(carUp,TPE_vec3Minus(LCR_racing.carBody.joints[4].position,
LCR_racing.carBody.joints[0].position)) < 0)
{
/* if the car falls on its roof the center joint may flip to the other
side, here we fix it */
LCR_LOG1("car roof flipped over, fixing")
// LCR_racing.carBody.joints[4].position = TPE_vec3Plus(TPE_vec3Times(carUp,
// LCR_PHYSICS_UNIT / 8),LCR_racing.carBody.joints[4].position);
//tmpVec = TPE_vec3Times(carUp,LCR_PHYSICS_UNIT / 8);
/*
for (int j = 0; j < 4; ++j)
LCR_racing.carBody.joints[j].position = TPE_vec3Minus(
LCR_racing.carBody.joints[j].position,tmpVec);
*/
/*
LCR_racing.carBody.joints[4].position = TPE_vec3Plus(TPE_vec3Times(carUp,
LCR_GAME_UNIT / 4),LCR_racing.carBody.joints[4].position);
LCR_racing.carBody.joints[4].position =
TPE_vec3Plus(LCR_racing.carBody.joints[4],tmpVec);
*/
// TPE_bodyReshape(&(LCR_racing.carBody),
//LCR_racing.physicsWorld.environmentFunction);
}
#endif
LCR_racing.tick++;
LCR_LOG2("racing step end");