Make ball not appear inside part with raycasting

I’m making a pool table and I want the preview ball appear against the other ball and not inside
robloxapp-20241006-2130097.wmv (575.4 KB)

local function createLine(ball)
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {ball, aimPart}
	rayParams.FilterType = Enum.RaycastFilterType.Exclude
	local mouse = mouseRaycast()
	local startPos = ball.CFrame.Position
	local targetPos = mouse.Position
	local lookAt = targetPos
	local distance = (startPos - lookAt).magnitude
	local direction = (lookAt-startPos) * distance
	local result = workspace:Raycast(startPos, direction, rayParams)
	if result then
		return result
	end
end

RS.RenderStepped:Connect(function()
	local result = createLine(whiteBall)
	aimPart.Position = result.Position
end)

if you have the position of where the ball will hit against another ball w/ a raycast, you can get the direction of it in accordance to the other ball with vector math

local ballDir = (result.Position - result.Instance.Position).Unit

once you have the direction you just have to offset it by the radius of the ball so its against it

aimPart.Position = result.Instance.Position + ballDir * (result.Instance.Size.X / 2)

Thank you, It works! Now I added a beam from the aimball and to the ball that the raycasting is selecting, how would i make the beam appear after the ball to show the path?
Screenshot 2024-10-07 014304

local ballDir = (result.Position - result.Instance.Position).Unit
aimPart.Position = result.Instance.Position + ballDir * (result.Instance.Size.X / 1.005)
local attachment = Instance.new('Attachment', result.Instance)
aimpartBeam.Attachment1 = attachment

you’d have to do the inverse of the ballDir you have already

the vector function is basically

(endGoal - startGoal).Unit -- returns a velocity aiming from the startGoal to the endGoal

so you’d want to get the direction of the bank which would be

local ballBounce = (result.Instance.Position - result.Position).Unit

and you’d make the attachment go from the ball’s current position and the end point of the beam would be something like

local beamLength = 1 -- number i just picked, you can alter this to whatever you wish the length of the beam to be)
local ballBounce = (result.Instance.Position - result.Position).Unit
attachment.WorldPosition = result.Instance.Position + ballBounce * beamLength

U are a genius bro, thank you so much!

Would you also know how i can make ball follow the guide line everytime? Cause it’s really inconsistent.

you’d have to program your own velocity system which wouldn’t necessarily be too hard since its basically spherical collisions on a 2d plane, you already have the direction bc of the ball bounce variable, you’d just have to interpolate the ball to move by a set amount of speed. however if you wish to use roblox physics, it may be inconsistent because the engine isn’t the best at handling sphere movement :sweat_smile:

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