heres what i initially used:
local function GetPositionAtTime(time: number, origin: Vector3, initialVelocity: Vector3, acceleration: Vector3): Vector3
local force = Vector3.new((acceleration.X * time^2) / 2,(acceleration.Y * time^2) / 2, (acceleration.Z * time^2) / 2)
return origin + (initialVelocity * time) + force
end
-- A variant of the function above that returns the velocity at a given point in time.
local function GetVelocityAtTime(time: number, initialVelocity: Vector3, acceleration: Vector3): Vector3
return initialVelocity + acceleration * time
end
local function RaycastTrajectory(origin, direction, maxDistance, raycastParams)
-- Perform the raycast
local result = workspace:Raycast(origin, direction * maxDistance, raycastParams)
return result
end
function Validation.ValidateTrajectoryWithCollision(startValues, result: RaycastResult, velocity: Vector3, elapsedTime: number)
local currentTime = 0
local currentPosition = startValues.Position
local currentVelocity = startValues.Velocity
local timeStep = 1/60
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {result.Instance}
while currentTime < elapsedTime do
local nextPosition = GetPositionAtTime(timeStep, currentPosition, currentVelocity, startValues.Acceleration)
local direction = (nextPosition - currentPosition).Unit
local distance = (nextPosition - currentPosition).Magnitude
local raycastResult = RaycastTrajectory(currentPosition, direction, distance, params)
if raycastResult then
-- Collision occurred
local collisionTime = currentTime + ((raycastResult.Position - currentPosition).Magnitude / distance) * timeStep
local finalPosition = GetPositionAtTime(collisionTime - currentTime, currentPosition, currentVelocity, startValues.Acceleration)
local finalVelocity = GetVelocityAtTime(collisionTime - currentTime, currentVelocity, startValues.Acceleration)
-- Compare with the provided result
local positionError = (raycastResult.Position - result.Position).Magnitude
local velocityError = (finalVelocity - velocity).Magnitude
if positionError <= POSITION_TOLERANCE then
return true, finalPosition, finalVelocity
else
return false, finalPosition, finalVelocity
end
end
currentTime = currentTime + timeStep
currentPosition = nextPosition
currentVelocity = GetVelocityAtTime(timeStep, currentVelocity, startValues.Acceleration)
end
print("no collision")
end
this basically takes the initial position, velocity, and acceration of the cast, and the time it took to hit, and simulates the cast in one frame using a while loop, if it does hit, it compares the final position and velocity to the actual hit and returning true if the errror is within the tolerance.
my only issue with this is that during the delay between the client and server, an object could get in the way of the trajectory and cause the validation to fail, but it shouldnt be much of an issue if there isnt a lot of flying unanchored parts (for example if your game has destruction).
this should be the closest you got to what you asked.
edit: just realized this thread is solved already, whoops.