I’m having trouble finding the correct formula to calculate the velocity needed to propel an actor with a Projectile Movement Component from its start position to a known target.
The start angle is predetermined, and so is known.
The Target Coordinates are known.
The direction and distance can be established.
What I need to find is the Velocity.
I really need the projectile to travel in an arc towards the target.
I’ve made several attempts, and my most successful approach is below, which was adapted from a coding example in another forum.
AShell* Proj = World->SpawnActor<AShell>(ProjectileBP, startPos, FRotator(newrot.Pitch, newrot.Yaw, newrot.Roll), SpawnParams); float distance = (startPos - TargetPosition).Size(); float angleToPoint = atan2(TargetPosition.Z - startPos.Z, (TargetPosition - startPos).Size()); float distanceFactor = 1 / 10000; float angleCorrection = (PI*0.18) * (distance * distanceFactor); float velocityX = cos(angleToPoint + angleCorrection) * 3000; float velocityZ = sin(angleToPoint + angleCorrection) * -1 * 3000 + 500; FVector Vel = FVector(velocityX, 0, velocityZ); Proj->ProjectileComponent->SetVelocityInLocalSpace(Vel);
This gives a semi satisfactory result, but the problem is that this does not take the launch angle into account and almost always either falls short of the target or goes too long for distant and near targets respectively. It is not at all accurate.
I have a few cheat approaches that I may have to employ (such as a UCurve and distance to dictate the height based on the distance to the target), but I know there is a relatively simple formulaic solution.
Unfortunately, mathematics is not my strong point, so I turn to you for help.