Fastcast projectile prediction?

Hey, so I’ve been using fastcast a lot for my opensource gun system. And i really need a way to predict where will a projectile land without actually firing it. I’ve tried looking through fastcast code but couldn’t figure much out, im terrible at math. Maybe someone has done it before?

2 Likes

I don’t know fastcast too well as I usually make my own projectile class so I’m not sure what the exact code to do this would look like. However it just comes down to simulating like 5 seconds worth of the projectile in a single moment.
For example: have a for loop complete several iterations of the projectile physics step.

This handy Wikipedia article gives you a formula for calculating the range of a projectile given velocity, angle, and vertical height:

Once you find the distance, just offset it relative to the LookVector of the projectile’s velocity.

I’ve considered this a few times but i was always eager to find a method that doesn’t involve finding an angle. I thought that if fastcast only requires the origin part then I can probably get the same result without an angle

What’s stopping you from just calculating the angle

--ToOrientation returns 3 values but we only need the X axis rotation (the gun elevation)
--The X axis happens to be the first value
local xRot: number = CFrame.lookAt(Vector3.zero, gun.CFrame.LookVector):ToOrientation()

Otherwise, the only other way is to actually simulate the projectile step by step to see where it would land via a loop and raycasting, which at this point you’ve basically made your own fastcast module.

Fastcast is mostly using projectile motion, so if you use the same equations it should follow the same path:

--g gravity, v velocity, t time, p initial position
local function projectileMotionEquation(g, v, initialPosition, t)
--See Ego Mooses tutorial on integrating projectile motion equation
	return 0.5 * g * t ^ 2 + v * t + initialPosition
end

local function raycastAlongQuadraticPath(g, v, initialPosition, endTime, raycastParams)
	local segments = 10*math.round(endTime*3) -- based on default beam adjust if needed
	local timeGapIncrements = endTime/segments

	local currentTime = 0
	for i=1, segments do
		local point1 = projectileMotionEquation(g,v,initialPosition,currentTime)
		local point2 = projectileMotionEquation(g,v,initialPosition,currentTime+timeGapIncrements)
		local direction = point2-point1
		local rayResult = workspace:Raycast(point1,direction,raycastParams)
		if rayResult then
			return rayResult
		end
		currentTime += timeGapIncrements
	end
end

I havent tries this yet, but I hope it helps
Just make another function that predicts the position and set that as the target position
You can use the direction variable for the raycast info

local distance = (originPosition - targetPosition)
local travelTime = distance / studsPerSecond
local offset = -(velocity * travelTime)
local direction = targetPosition + offset

Is there a way to do this for a Caster that has drop? This looks like it’s only for the straight casters

Sorry I don’t know, maybe I can tell you when I’m done with my module and have figured it out

this is the solution. and unfortunately we have to approximate the curve with the individual straight line pieces.