Player Spawn in Circle

I am making a 2D game that will then spawn all players in a circle. I want there to be 10 different spots to where they can spawn, and I have done some math, but I am unsure how to translate it into code. My calculations will be different because of the nature of my game, but does anyone have any idea of how I can do this? A circle will be divided into 10 equal parts resulting in 36 degree angles between each spawn point and the distance is dependent on the radius. The arc length would be s = 36*r.

1 Like

Time to use the mighty powers of trigonometry!!!

u should use Sine to find the Y coordinate and Cosine to find the X coordinate
the hypothenuse in the calculus is ur radius
i hope ur already on this sort of thing but even if not u could just calculate it like this:

PlayerPos.Y = math.sin(angle)*hypothenuse
PlayerPos.X = math.cos(angle)*hypothenuse

multiply the angle by 1,2 or more to move a step further the circle
bigger angles do bigger steps

i may be wrong though :-/

1 Like

I’ll try this and check it and get back to you when I finish. Thanks for the feedback!

I see that your problem has already been solved, but here’s a solution I just happen to come up with. It will automatically adjust for any cylinder (circle) you give it, and should work well (I tested it):

-- Variables
local spawnCircle = script.Parent

local diameter = (spawnCircle.Size.Y + spawnCircle.Size.Z) / 2
local radius = diameter/2

for spawnAngle = 36, 360, 36 do
	print("spawnAngle = " .. spawnAngle)
	local xDifference = math.cos(math.rad(spawnAngle)) * (radius - radius/5)
	local zDifference = math.sin(math.rad(spawnAngle)) * (radius - radius/5)
	local newPoint = Vector3.new(spawnCircle.Position.X + xDifference, spawnCircle.Position.Y + 1, spawnCircle.Position.Z + zDifference)
	
	local newPart = Instance.new("Part")
	newPart.Parent = game.Workspace
	newPart.Name = "Spawn Angle: " .. spawnAngle
	newPart.Position = newPoint
	newPart.Color = Color3.new(0, 0, 0)
end

How it should look:

Let me know if you have any questions. Have a good weekend!

1 Like

Thank you. I have also came up with the formula:

x = r * cos(θ * i) + x0 + ((1/2) * playerWidth))
y = -r * sin(θ * i) + y0 + ((1/2) * playerHeight))

If mine doesn’t work I’ll try yours out since yours is already tested. :slight_smile:

1 Like

Okay sounds good; and keep in mind you’ll have to adjust the players’ positions on the Y axis to work properly, since I did not account for that.

Yeah since its 2D I have to do the y-coordinate system differently.

Hey, I just made an adjustment to it where if you change the players variable it’ll automatically adjust everything. Here you go:

-- Variables
local spawnCircle = script.Parent

local diameter = (spawnCircle.Size.Y + spawnCircle.Size.Z) / 2
local radius = diameter/2

local players = 25
local angles = 360 / players

for spawnAngle = angles, 360, angles do
	print("spawnAngle = " .. spawnAngle)
	local xDifference = math.cos(math.rad(spawnAngle)) * (radius - radius/5)
	local zDifference = math.sin(math.rad(spawnAngle)) * (radius - radius/5)
	local newPoint = Vector3.new(spawnCircle.Position.X + xDifference, spawnCircle.Position.Y + 1, spawnCircle.Position.Z + zDifference)
	
	local newPart = Instance.new("Part")
	newPart.Parent = game.Workspace
	newPart.Name = "Spawn Angle: " .. spawnAngle
	newPart.Position = newPoint
	newPart.Color = Color3.new(0, 0, 0)
end
1 Like

For anyone wondering what my final code for this problem was here it is:

	for i = 1, #players, 1 do
		local character = clientScreen.getCharacter(players[i])
		local xCharacterCenter = ((1 / 2) * character.Size.X.Scale)
		local yCharacterCenter = ((1 / 2) * character.Size.Y.Scale)
		
		local spawnPoint = clientScreen.getWorld(players[i]).Environment.Assets.Spawn -- Might need to get the client's version due to scale size
		local xSpawnPosition = spawnPoint.Position.X.Scale + ((1 / 2) * spawnPoint.Size.X.Scale)
		local ySpawnPosition = spawnPoint.Position.Y.Scale + ((1 / 2) * spawnPoint.Size.Y.Scale)
		
		-- Coordinates for the player inside the environment
		local xPlayerPosition = (spawnRadius * math.cos(math.rad(angle * i))) + (xSpawnPosition - xCharacterCenter)
		local yPlayerPosition = -(spawnRadius * math.sin(math.rad(angle * i))) + (ySpawnPosition - yCharacterCenter)
		
		-- Move the player
		moveCharacter(players[i], xPlayerPosition, yPlayerPosition)
	end