Help with an Agar.io like spawn and generation system

Hello everyone!

  1. What do you want to achieve?
    I would like to make a system that generates parts randomly, but with some constraints, across a uniform space. This is similar to how the game Agar .io generates small pellets all over their map. I would also like to spawn in players (as they control a part) into the game. This is also just like Agar .io

  2. What is the issue?
    I do not know how to check if a position already contains a part. If I do not check, it ends with players and the “pellets” being spawned/generated within another player (remember all players are parts) and being eaten instantly.

  3. What solutions have you tried so far?
    Currently, my system is completely random and doesn’t check if the things it generates are in unacceptable positions.

How can I check if the position randomly generated is unacceptable?

Thanks in advance for all the help! The dev forum has been a great resource as I progress with my game!

Edit: If anyone thinks this would be better named as “Check if a position contains a part” say so below. I used the “Agar .io” name to help people understand what I am going for.

Someperson576

5 Likes

It looks like you have two questions:

Q: How can I spawn pellets randomly over the level?

A: You can set up a 2D “region” by creating two corners to reference. A top left, and bottom right corner. They can be represented by Vector3’s.

Now these two points can be represented by two parts in the workspace, manually set vectors, or just a singular part. You will just have to do some simple math to figure out the corners for a single part.

Here is a neat little function that returns a “random” number between a provided min and max value.
Use this to get the random position values for the X and Z.

local TopLeftCorner        = Vector3.new(0,0,0)
local BottomRightCorner    = Vector3.new(100,0,50)

--Random number function. 

local SingletonRandom = Random.new(tick())

local function GetRandom(Min,Max)
	return SingletonRandom:NextNumber(Min,Max)
end

--Set pellet position.

	Pellet.Position = Vector3.new(
			
		GetRandom(TopLeftCorner.X , BottomRightCorner.X ), --Random X value
		PlayingFieldHeight,  --Whatever the height of the playing field is
		GetRandom(TopLeftCorner.Z , BottomRightCorner.Z ) --Random Z value
			
	)

Here is what that will do for you. ( I have different corner positions. Same code. )


Q 2: How can I prevent players from spawning near other players?

A: You can use the same idea above to do this (except just one time), with some checks. First, randomly pick a position in the playing field. You can then check for the positions of all players in the game, and compare their distances from your chosen position to the maximum possible mass size. That way, if their distance is greater than the maximum possible size, you will spawn outside the largest possible eating radius of all players, therefore avoiding any unwanted early deaths.

You could also compare distances AND sizes of the players, so that the player can spawn closer to orbs with less mass, and farther from orbs with more mass.

10 Likes

If I am correct, food pellets in agar.io CAN spawn inside players, You just don’t really notice it because it happens instantly. As long as you make the grid large enough, the parts small enough, and the grid points incremental enough, probability will be greatly in your favor for food not spawning inside of other objects.

3 Likes