8 ball/ Snooker game

Hello, this is my first post but im a new dev making a new game about 8ball and snooker and ive made a simple start where you can hit the ball, ive figured out the physics and the pool table but im stuck on how i would make a cue work,

i want to make it so the cue has collitions of annd would circle around the cue ball when i circle round it with my camera and when i press the shoot button it plays a animation and that all i need but i cant seem to fint any tutorials on how to start so im a little stuck, this is what i have so far in the game:

and here is the code for the circling

local targetObject = game.Workspace.Balls:FindFirstChild("CueBall") -- Replace "TargetObject" with the actual name of the object you want the camera to follow
local camera = game.Workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")

local db = true

local cameraDistance = 10
local cameraAngleX = 0
local cameraAngleY = 45

local isFollowingCueBall = true

local sensitivity = 0.2
local baseSensitivity = 0.03
local rotationSpeedX = 0
local rotationSpeedY = 0

-- Calculate the initial position only once outside the functions
local initialPosition = targetObject and targetObject.Position

local function updateCamera()
	if targetObject and initialPosition then
		rotationSpeedX = rotationSpeedX - (rotationSpeedX * 0.15)
		rotationSpeedY = rotationSpeedY - (rotationSpeedY * 0.15)

		cameraAngleX = math.clamp(cameraAngleX - rotationSpeedX * sensitivity, -45, 0)
		cameraAngleY = cameraAngleY - rotationSpeedY * sensitivity

		local rotatedCFrame = CFrame.Angles(0, math.rad(cameraAngleY), 0) * CFrame.Angles(math.rad(cameraAngleX), 0, 0)
		local cameraPosition = initialPosition + (rotatedCFrame.LookVector * -cameraDistance)
		local lookAtPosition = initialPosition

		camera.CFrame = CFrame.new(cameraPosition, lookAtPosition)
	end
end

local function rotateCamera()
	local mouseDelta = UserInputService:GetMouseDelta()
	rotationSpeedX = rotationSpeedX + mouseDelta.Y * sensitivity
	rotationSpeedY = rotationSpeedY + mouseDelta.X * sensitivity

	updateCamera()
end

local function onShiftPressed()
	sensitivity = baseSensitivity
	game.Players.LocalPlayer:FindFirstChild("PlayerGui"):FindFirstChild("PowerBarGUI"):FindFirstChild("aim").Visible = true
end

local function onShiftReleased()
	sensitivity = 0.2
	game.Players.LocalPlayer:FindFirstChild("PlayerGui"):FindFirstChild("PowerBarGUI"):FindFirstChild("aim").Visible = false
end

game:GetService("RunService").RenderStepped:Connect(rotateCamera)

-- Listen for Shift key press and release events
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
		onShiftPressed()
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
		onShiftReleased()
	end
end)

local RemoteEvent = game.ReplicatedStorage.hi
RemoteEvent.OnClientEvent:Connect(function()
	if targetObject then
		initialPosition = targetObject.Position
		updateCamera()
	end
end)

1 Like

You will want to render the cue position frame by frame by using RunService.

First, choose an initial position for the tip of the cue. Then, cast a ray at a certain length according to the chosen speed of the cue and then position the cue tip at the end of the ray:


Repeat this every frame until a raycast intersects with the cue ball:

Position the cue tip at the impact point on the ball. The impact of the cue with the ball will apply a force to the ball at that point:

You will have to determine the magnitude of the impact force depending on the speed of the cue.

thanks for the info! ive actually already got the ball movement sorted, all i need is to find out how to make the cue move around when my camera moves around, do you think if i just make it so the cue is always a certain distance from my camera and make it follow the camera then when i press shoot it plays the ball shooting animation? do you know how i would approach that.

and thanks for the reply!

You should be familiar with how ball physics works, there’s a good video I’ve seen recently that explains it from a pro:

It’s not scripting, but it explains how the physics for this game works.

thanks, im actually pretty familiar with how the physics of this game works and just wondering how i code a cue to rotate round a ball, my current code hits the ball in the direction im facing by applying force, i dont want the cue to push the ball because Roblox physics is funny

Well, you can do some basic math. Assuming the cue ball has the same mass and diameter as all the other (which it doesn’t), you can calculate the velocities of the ball that’s colliding after it touches.

Here’s the formula, where u is the previous velocity, and v is the resulting velocity. m is the mass. This formula should help you calculate everything.