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!
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