Need help figuring out the exact position a projectile will land at

Hello, developers!

I currently have a baseball game and everything is good, but I’ve run into an issue with something I’m working on. I’d like to get the exact position that the ball will land at when it’s hit. I have the ball’s velocity, distance traveled, launch angle, and all, but I don’t know how to go about predicting it’s final position.

More specifically, I have the velocity and distance traveled for each individual coordinate (X, Y, Z). I have how far the ball travels on the X axis, how far it travels on the Z axis, and how far it travels on the Y axis. I just don’t know how to put these together to find out the landing position.

If you have any questions, I’m happy to answer, but I need help!

3 Likes

I believe you will need to combine projectile motion equation with for collision detection. This function should work:

--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
3 Likes

You may also be interested in this thread, which includes some more details and derivations.

I got busy and forgot about this post, oops.

Anyways, I’ll try this function out and see how things go. Thank you!

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