Change antibug (nonrotating phys.)

This commit is contained in:
Miloslav Ciz 2025-04-14 00:47:12 +02:00
parent 27d2e89133
commit 00519e0116
3 changed files with 92 additions and 65 deletions

100
racing.h
View file

@ -125,10 +125,6 @@ struct
LCR_GameUnit carSpeeds[2]; /**< Signed speed in game units per tick (negative
if backwards) and its previous value. */
// for fixing physics bugs:
TPE_Vec3 carOKPositions[LCR_CAR_JOINTS];
uint8_t carNotOKCount;
uint16_t crashState;
uint8_t playingReplay;
@ -993,11 +989,6 @@ void LCR_racingRestart(uint8_t replay)
TPE_F / 2 : (TPE_F / 4),0));
}
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
LCR_racing.carOKPositions[i] = TPE_vec3(0,0,0);
LCR_racing.carNotOKCount = 0;
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);
@ -1386,7 +1377,7 @@ uint32_t LCR_racingStep(unsigned int input)
to unstick itself. */
TPE_bodySpin(&LCR_racing.carBody,_LCR_TPE_vec3DividePlain(carUp,
(steering == (((input & LCR_RACING_INPUT_LEFT) != 0)) ==
((steering == (((input & LCR_RACING_INPUT_LEFT) != 0))) ==
((input & (LCR_RACING_INPUT_FORW)) != 0)) ? 100 : -100));
}
}
@ -1412,7 +1403,49 @@ uint32_t LCR_racingStep(unsigned int input)
#endif
LCR_LOG2("stepping physics (start)");
TPE_worldStep(&(LCR_racing.physicsWorld));
{
/* To prevent many physics glitches we simulate the physics steps twice:
once with normal physics and once with non-rotating (completely stiff)
body. If the normal step works out wrong (car shape ends up wrong etc.),
we fall back to the simple non-rotating step. */
TPE_Joint joints[LCR_CAR_JOINTS];
LCR_racing.carBody.flags |= TPE_BODY_FLAG_NONROTATING;
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
joints[i] = LCR_racing.carBody.joints[i];
TPE_worldStep(&(LCR_racing.physicsWorld)); // non-rotating step
for (int i = 0; i < LCR_CAR_JOINTS; ++i) // remember the position and reset
{
TPE_Joint tmpJoint = LCR_racing.carBody.joints[i];
LCR_racing.carBody.joints[i] = joints[i];
joints[i] = tmpJoint;
}
LCR_racing.carBody.flags &= ~TPE_BODY_FLAG_NONROTATING;
TPE_worldStep(&(LCR_racing.physicsWorld)); // normal step
if ((LCR_racing.carBody.flags & TPE_BODY_FLAG_UNRESOLVED) ||
!_LCR_racingCarShapeOK()) // bad?
{
LCR_LOG1("car bad, simplifying physics");
for (int i = 0; i < LCR_CAR_JOINTS; ++i) // use the first step positions
LCR_racing.carBody.joints[i] = joints[i];
// If still not OK (slim chance), just stop the body (prevent uberbugs):
if ((LCR_racing.carBody.flags & TPE_BODY_FLAG_UNRESOLVED) ||
!_LCR_racingCarShapeOK())
TPE_bodyStop(&LCR_racing.carBody);
}
}
LCR_LOG2("stepping physics (end)");
LCR_racing.carSpeeds[1] = LCR_racing.carSpeeds[0];
@ -1493,51 +1526,6 @@ uint32_t LCR_racingStep(unsigned int input)
}
}
if ((LCR_racing.carBody.flags & TPE_BODY_FLAG_UNRESOLVED) || frontCollision ||
!_LCR_racingCarShapeOK())
{
// car not OK
if (LCR_racing.carNotOKCount > 4) // TODO: constant
{
LCR_LOG2("car not OK (short), fixing");
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
{
if (LCR_racing.carNotOKCount < 20) // TODO: const
{
// 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];
}
}
}
LCR_racing.carNotOKCount += LCR_racing.carNotOKCount < 255 ? 1 : 0;
}
else
{
// car OK
LCR_racing.carNotOKCount = 0;
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
LCR_racing.carOKPositions[i] = LCR_racing.carBody.joints[i].position;
}
int carBlock[3];
LCR_racingGetCarBlockCoords(carBlock);