Calculate Velocity of a part, + bullet time to reach and aim there, to intercept part (Easy solution 4 u)

Basically,

Currently ,the object aims at the part’s position, and not at where its gonna be. how would i calculate that? The turret uses a yaw and pitch hinge.

1 Like

Get the part’s Position offset from where it was last time. Do this every .25 seconds or so. Then instead of having the turret look at the part, have it look at where it will be. For the bullet, you would get the distance to the part, and the bullet speed. You would get the offset magnitude, (5) multiply it by 4, because we are doing this every .25 seconds, (20), and convert the bullet speed to 1 second (250) studs per second.) You would then divide (20)/250. And multiply that number by the magnitude from the cannon to the parts offset. So if you had an offset of (20,0,35)*.08, you would then add that onto the current part position, and that would be where to aim.

However, I was going to say it wouldn’t work when targeting fast moving vehicles, but I forgot why.

This is . STRANGELY and very confusing to me, could you, MAGICALLY write a basic script or function for this?

Yeah basically im saying, i have no idea how to piece or thread together what you said.
Could you do it?
I’m serious, i read it thoroughlly it is VERY hard to understand.

First, you take the time the bullet will take to intercept it. You can approximate this with the distance from the turret to the part divided by the bullet speed:

local BULLET_SPEED = -- Your turret's bullet speed
local distance = (otherPart.Position - turretPart.Position).Magnitude
local travelTime = distance / BULLET_SPEED

Assuming you’re just using velocity, and that the part isn’t anchored (for example, a player) you can use . AssemblyLinearVelocity to get the speed and direction of the part. You can then calculate the position that the part will be with

-- Velocity * time
local changeInPosition = otherPart.AssemblyLinearVelocity * travelTime

-- The predicted position `travelTime` seconds in the future
local futurePosition = otherPart.Position + changeInPosition

If you absolutely need to use anchored parts, do the same thing but calculate the velocity every frame using Heartbeat.

If you want to use acceleration, do the same thing for anchored part velocity but with the part’s velocity to find the change in velocity (aka the acceleration). With the acceleration, use the following kinematic equation to calculate the future position:

local changeInPosition = velocity * travelTime + 1/2 * acceleration * travelTime^2
local futurePosition = otherPart.Position + changeInPosition
2 Likes

bro had me checking my pings on discord

and ill do ita gain buddy… dont forget…

This… works but it’s odd. I unanchored the part and let it fall. and it’s just… inaccurate, until it reaches the void and destroys itself, it slowly becomes accurate, is that normal?

(The bullet explodes when the travelTime has passed)

1 Like

Since a part that’s falling has a big acceleration factor, make sure to use the last equation that takes into account the acceleration.

The travel time I gave is also just an approximation (I was thinking this was for something that could afford to be slightly off, like AIs enemies for close range stuff).

Currently, it gets the time the bullet would take to get to the part’s current position, not it’s future position, which is where it’s actually traveling to. If you use the future position, the equations become recursive (though I bet they’re solvable):

-- The travel time is ((futurePosition-turrentPosition).Magnitude/Speed)
futurePosition = velocity * ((futurePosition-turrentPosition).Magnitude/Speed) + 1/2 * acceleration * ((futurePosition-turrentPosition).Magnitude/Speed)^2

See how the future position is defined in terms of future position? That makes it a little difficult to solve.

However, you can do something similar to a Euler approximation pretty simply though, which should give you almost exactly the right answer:

-- Uses the position, velocity, and acceleration of the target
-- If you want to test acceleration, for a falling part it would be Vector3.new(0, -1, 0) * workspace.Gravity
-- Otherwise use a Heartbeat loop to calculate it using the current and last velocity
local function calculate(position, velocity, acceleration, travelTime, turretPosition)
    local changeInPosition = velocity * travelTime + 1/2 * acceleration * travelTime^2
    return position + changeInPosition
end

local BULLET_SPEED = -- Your turret's bullet speed

-- How many times the approximation gets improved, should exponentially decay to correct answer
local REFINEMENT_ITERATIONS = 10

local futurePosition = position
local travelTime -- Will get set inside the loop
for i = 1, REFINEMENT_ITERATIONS do
    -- Each iteration calculates a more correct travel time using the last approximation of the future position
    travelTime = (futurePosition-turrentPosition).Magnitude / BULLET_SPEED
    futurePosition = calculate(position, velocity, acceleration, travelTime, turretPosition)
end

-- Now the future position has been refined 10 times and should basically be the real answer
-- The travel time should also be very accurate

That should get an answer super close to the real answer, and doesn’t require solving the recursive equation. In addition, doing some multiplication ten times is entirely negligible for performance, so that’s not a problem.

LMK if you need help with getting the acceleration with a heartbeat connection, I can show you how to do that. Very cool project BTW, it looks super cool!

2 Likes

Thank you so much, you solved my thread, if you could, could you help me try to make a anti-air missile?

Link

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.