Determine projectile impact position

Hello folks!

I highly appreciate anyone who has the time to read my forum post, as I’ve been struggling for a while in this certain topic: Getting the impact position of a projectile with nothing but Initial Position, and Projectile Velocity.

Here’s what I mean:

Now; incase that’s impossible, I just wanted to know if there is an effective way to determine where a projectile will hit. Projectile is affected by gravity, and it fires the projectile towards the mouse.hit.position ( CFrame.new(Position, mouse.hit.position) )

If you have any suggestions or ideas, it’ll be greatly appreciated!

3 Likes

Why would you need to figure that out? I’m guessing you’re making some hitreg system of sorts, but I don’t understand why you’d need to figure out a projectile’s landing position ahead of time.

If you have a lot of moving parts, it can be tricky to determine where they’ll be ahead of time, meaning it can be difficult to predict when a moving object might suddenly get in the way of your projectile.
This is especially the case with players - you can’t predict where a player will move ahead of time.

1 Like

That’s no problem at all! Right now, I wanted to show a part that is positioned to where the projectile impacts… I do understand what you mean, which is why I’d like to mention that the trajectory will constantly update every moment prior to a frame (renderstepped).

If you’re into planes, then you might’ve heard of something called a Constantly Computed Impact Point (CCIP) which shows the impact of your munition on digital terrain surface, as objects such as buildings arent taken account for in the digital map.

image

1 Like

This is the game; I want to visualize a determined impact point for these projectiles with given parameters (inital velocity) etc.

https://cdn.discordapp.com/attachments/1044314527707173055/1218103449527386242/2024-03-15_10-47-09.mp4?ex=6606723d&is=65f3fd3d&hm=48fb40badaaf337bf0201458b6afe389e3dd587c1061cfe1492bca483fae9203&

1 Like

You could use the formula
s = ut + 1/2at²

1 Like

Sure! what’re the variables if you could specify them?

1 Like

The variables for the x are: u (the initial speed), and t (time) so it is s = v₀t
The variables for the y are: u (the initial speed), a (gravity of the place), and t (time)

1 Like

I see; wouldn’t we need Z as well? Or is it not necessary here

1 Like

Well, I forgot about the Z-axis now that you bring that up
so it will just be the same as the X-axis

1 Like

Alrighty then! I’ll try applying that and let you know how it went.

1 Like

Here you go.

local function simulateProjectilePath(firingPosition, firingVelocity)
        local gravity = Vector3.new(0, -196.2, 0) -- Roblox gravity
    local timeStep = 0.05 -- Time step for simulation in seconds, smaller for more accuracy
    local totalTime = 0 -- Total simulation time

    local currentPosition = firingPosition
    local currentVelocity = firingVelocity -- Now a Vector3 value

    local raycastParams = RaycastParams.new()
    raycastParams.IgnoreWater = true -- Customize as needed
    raycastParams.FilterDescendantsInstances = {} -- Objects you want to include here. You can change the filtertype if you want.    
    raycastParams.FilterType = Enum.RaycastFilterType.Include

    while true do
        -- Calculate the new position based on current velocity and gravity
        local newPosition = currentPosition + currentVelocity * timeStep + 0.5 * gravity * timeStep^2
        local direction = newPosition - currentPosition -- The direction and distance for the raycast


        local raycastResult = workspace:Raycast(currentPosition, direction, raycastParams)
        if raycastResult then
            -- Hit detected, place your target indicator here
            --print("Hit at: ", raycastResult.Position)
            -- Place target indicator logic here, e.g., creating a part or marker at raycastResult.Position
            return raycastResult.Position,raycastResult,totalTime -- Returning the hit position
        end

        -- Update for next iteration
        currentPosition = newPosition
        currentVelocity = currentVelocity + gravity * timeStep -- Update velocity with gravity
        totalTime = totalTime + timeStep

        -- Optional: Stop simulation after a certain time to prevent infinite loop
        if totalTime > 10 then -- Adjust as necessary
            --print("No hit detected within simulation time.")
            break
        end
    end
    return nil --
end

return simulateProjectilePath
1 Like

I’ll try that out and see how it goes!

1 Like

What if you tried using this?

2 Likes

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