Calculate Projectile's Velocity Based on Targets Future Position Give The Targets Current Velocity And Travel Time of Projectile

What I would like to accomplish is to calculate the velocity a projectile should travel to reach it’s targets predicted future position given the velocity of both the Target and the projectile (and time it takes the projectile to travel to that future position.

For example:

Known Variables:

-- Speed is constant and Gravity Does Not Apply
local ProjectileSpeed = 200
local TargetPart = workspace.MovingTargetPart
-- Projectiles Constant Stationary Starting Position:
local StartingPosition = Vector3.new(35.908287, 87.2070313, 136.881424)
local Projectile = game:GetService("ReplicatedStorage").Projectile:Clone()
local BodyVelocity = Projectile:FindFirstChild("BodyVelocity")
BodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)

Projectile.Position = StartingPosition
Projectile.Parent = workspace

BodyVelocity.Velocity = ?

How do I calculate the BodyVelocity.Velocity to set such that the Projectile reaches the TargetPart 's future position assuming the velocity of the TargetPart and Projectile remain constant.

2 Likes

The velocity is your projectile speed. I’m not too clear on what you’re trying to do but you’ll need to determine the future position and with that you can set the Velocity to;
BodyVelocity.Velocity = (FuturePos - StartingPos).Unit * Projectile Speed

Edit; I’d you’re asking how to determine the future position, I’d recommend doing .Magnitude on Velocity to find the overall speed and with the knowledge of when your projectile will be fired you can approximate the future position through; d=s*t

Yea thats easy. The problem is calculating the FuturePositon based on the time it takes for the projectile to travel to that position.

Basically, If I knew the future position, then I could easily get the Velocity.

You are basically trying to find the point of intercept, and to aim the projectile at that point of intercept, I assume.

To accomplish that you need to find that point of intercept. This is done using the formula Ip = Tp + Tv * t, where Ip is the position the intercept would occur, Tp is the Position of the target, Tv is the velocity of the target, and t is the time it would take for the projectile to reach the target.

However, t is unknown. To derive t, you can use the equation d = s * t and re-arrage it into t = d / s so d would be the distance between you and the target* and s would be the speed of the projectile. This would allow you to find the time taken for the projectile to reach a certain position, in this case, the position of intercept.

Now that you’ve found t, you can then plug it back into the initial equation: Ip = Tp + Tv * t where the position of intercept is the targets current position, plus the distance it will move in the time taken to reach the target.

(I am using a simplified equation that is not very reliable, I personally am also trying to solve this.)
Blockquote

2 Likes

Yes, you get exactly what I am saying. How do I calculate (in Robolox/Lua) the point of intercept assuming the velocity of both remain constant and gravity is not involved. I can then use this to set the Velocity of the projectile.

I know there is some big brain Robloxer out there that knows how to code/calculate this.

Yeah. Because through practice, you will notice that if you derive t from the position of the target at the time of launch, by the time the projectile reaches the target, t would have changed. So you would instead need to derive t from where the point of intercept would be, which you can’t find without t.

This is what I’m still stuggling to solve.

Looking online, this is a common mathematical problem. There is even formulas for this. I just have no clue how to translate this into Roblox + Lua code.

I’ve attempted to but without much success. The closest I’ve gotten was having a loop that constantly refines t based on the already-calculated point of intercept.

1 Like

There is another devforum post that goes into much greater detail, quite a lot to bite on but it may come in useful.

The problem, is that I cant update the projectile on the fly. I have to calculate it once and shoot cause I perform a remote event to client to have the projectile played on the client system as closely as what will be calculated server side.

I meant that the projectiles wernt guided, just that the script that dictates the direction the projectiles would travel in did the calculations. So the first few rounds would miss as t was incorrect, but as the loop ran, it would use the old value of t to get a value closer and closer to the correct value.

You can calculate the future position by treating it as a derivative, where h is your targets position and the future position being h.Position + (h.Velocity * seconds). From there we can create a virtual point in a sense from the value we got from h.Position + (h.Velocity * seconds) because velocity just describes, well velocity. It isnt absolute rather constantly updating. However like I said treating it as we did with h.Position + (h.Velocity * seconds) we can find its new position. From there I am sure you already know the rest based on what you said earlier of knowing how to calculate the velocity needed based on the direction and time.

Not the most efficient way, but the one that works best out of the searching I’ve done so far.

why do you say it is inefficient? With this we dont need to simulate it in a closed system we just do a bit of math (remember, computers like math)

But could I clarify, to determine the position of h, wouldnt you need to use the “incorrect” value of t?

Well I figure I can just calculate the trajectory of the TargetPart and just run a calculation for each position on that Trajectory to see if the projectile and and the TargetPart are close enough to each other at the same time.

Just feels like a lot of extra calculations for something that should be able to be calculated once with a formula.

we know the position of h, what we are doing is finding out what h will be after x seconds

It is being calculated once, you arent looping it

Then you’d need to find x. Which, if you are deriving from h, would be incorrect.

Correct me but the velocity of the target is constant, no?