I’m sorry if my code is a bit confusing, I’m pretty new.
I’m trying to slow my actor smoothly to a complete stop. To do this I remove a percent of its overall velocity, and also a flat amount. I do this until it changes direction (due to the flat amount) at which time I set the velocity to 0.
This seems to work perfectly fine ingame except when I alt tab while moving it craps the bed and seems to zoom away at lightspeed, outside of what unreal can even render. I feel like I’m missing something simple and was wondering if anyone had tackled this problem themselves?
void UIslandGamePawnMovementComponent::SimulateMove(const FIslandGamePawnMove & Move)
{
//Here I am getting the direction and force from my controller/keyboard and timesing it by my MaxMovingForce.
FVector Force = (Move.XDirection * Move.XValue + Move.YDirection * Move.YValue) * MaxMovingForce;
//Here I add resistance to slow it down, eventually to a stop.
Force += GetAirResistance();
FVector Acceleration = Force / Mass;
Velocity = Velocity + Acceleration * Move.DeltaTime;
//Here I update the pawn using the deltatime
UpdateLocationFromVelocity(Move.DeltaTime);
}
//Here I remove a percent of the velocity, becuase this is will never actually stop it I also remove a set ammount. I check whether removing this amount will change the direction of the vector,
// and if it does I simply return -Velocity which will stop the pawn.
FVector UIslandGamePawnMovementComponent::GetAirResistance()
{
//if the velocity becomes too small to get a safe normal I stop the pawn by returning -velocity.
if (Velocity.GetSafeNormal().Equals(FVector(0.0f, 0.0f, 0.0f))) {
return -Velocity;
}
//Here I compare the SafeNormal/direction of the velocity before and after the removal to see if the direction would be changed, meaning its overshooting.
if ((-Velocity.GetSafeNormal() * Velocity.SizeSquared() * DragCoefficient - Velocity.GetSafeNormal() * 200.0f).GetSafeNormal().Equals(-Velocity.GetSafeNormal(), 0.1f)) {
return -Velocity.GetSafeNormal() * Velocity.SizeSquared() * DragCoefficient - Velocity.GetSafeNormal() * 200.0f;
}
return -Velocity;
}