perf: PredictedRigidbody MoveTowardsCustom: only calculate distance factor once

This commit is contained in:
mischa 2024-03-15 13:23:44 +08:00 committed by MrGadget
parent 8705a9b0c2
commit 7cf0b884a6

View File

@ -364,15 +364,18 @@ static Vector3 MoveTowardsCustom(
if (_sqrDistance == 0.0 || maxDistanceDelta >= 0.0 && _sqrDistance <= maxDistanceDelta * maxDistanceDelta) if (_sqrDistance == 0.0 || maxDistanceDelta >= 0.0 && _sqrDistance <= maxDistanceDelta * maxDistanceDelta)
return target; return target;
float distFactor = maxDistanceDelta / _distance; // unlike Vector3.MoveTowards, we only calculate this once
return new Vector3( return new Vector3(
current.x + (_delta.x / _distance) * maxDistanceDelta, // current.x + (_delta.x / _distance) * maxDistanceDelta,
current.y + (_delta.y / _distance) * maxDistanceDelta, // current.y + (_delta.y / _distance) * maxDistanceDelta,
current.z + (_delta.z / _distance) * maxDistanceDelta); // current.z + (_delta.z / _distance) * maxDistanceDelta);
current.x + _delta.x * distFactor,
current.y + _delta.y * distFactor,
current.z + _delta.z * distFactor);
} }
Vector3 newPosition = MoveTowardsCustom(currentPosition, physicsPosition, delta, sqrDistance, distance, positionStep * deltaTime); Vector3 newPosition = MoveTowardsCustom(currentPosition, physicsPosition, delta, sqrDistance, distance, positionStep * deltaTime);
// smoothly interpolate to the target rotation. // smoothly interpolate to the target rotation.
// Quaternion.RotateTowards doesn't seem to work at all, so let's use SLerp. // Quaternion.RotateTowards doesn't seem to work at all, so let's use SLerp.
// Quaternions always need to be normalized in order to be a valid rotation after operations // Quaternions always need to be normalized in order to be a valid rotation after operations