Hi there. I have an actor in my project, which is supposed to imitate the ballistic movement without overcomplicating it. So it has InitVelocity variable, which represents velocity in meters per second at the start of motion, StartLocation and StartRotation, and some other variables, that are shown here:
FVector ATarget3D::BallisticMovement()
{
/*FVector drag = -CurrentVelocity.GetSafeNormal() * K * CurrentVelocity.SizeSquared() * p;
FVector gravity = FVector(0.0, 0.0, -G * M);
FVector force = drag + gravity;
FVector acc = force / M;*/
return (-CurrentVelocity.GetSafeNormal() * K * CurrentVelocity.SizeSquared() * p + FVector(0.0, 0.0, -G * M)) / M;
}
where
float M = 100.0;
float G = 9.8;
float K = 5; //change this later
float p = 1.2255;
FVector CurrentVelocity;
So basically what I do is I implement this formula
in code. Then, in BeginPlay, I run this code:
SetActorLocation(StartLocation);
SetActorRotation(StartRotation);
CurrentVelocity = StartRotation.Vector() * Velocity;
And to make this whole code work properly here’s what my Tick function has in it:
FVector old_pos = GetActorLocation();
CurrentVelocity += BallisticMovement() * DeltaTime;
SetActorLocation(old_pos + CurrentVelocity);
SetActorRotation(UKismetMathLibrary::FindLookAtRotation(old_pos, GetActorLocation()));
So here’s the problem. This code does actually send my actor by ballistical(parabolical) trajectory, but every time I start the game over and over again, the result trajectory faces different direction, has different height and lands on different location. So I wanted to ask how to fix that problem. However, it would be even better if somebody could provide me with different formula, which can be counted by time since launch, as I need to implement some sort of algorithm, which counts when and in which exact direction should missile be sent with set InitVelocity, so that the formula of ballistic trajectory also has to be very accurate. I’d be very thankful to see any possible suggestions or questions under this questions as I really need to implement this feature as soon as possible.