# How to Get Overlapped Colliders Repulse Each Others ?

—Hi, I get stars spawned(instantiated) at random locations when player collides something. When player collides alot of something then that much stars get spawned of course. And (this is important info: stars reflect to another direction when collide each others)
—Sometimes new star spawns inside one of other moving stars.And they both stuck and move together. That’s called overlapping. This is where i bumped into. I know what most of you will suggest me to do are;

1. Turn off collisions between stars,
2. Assign a collider to spawn points and if they already have stars inside then pick another spawn point to spawn,

—I can’t settle for these options and i don’t want to since they break my game style. So i come here to ask if there are any else solutions which is good for my game.

MY IDEA: I’m thinking about getting them repulse each others. I have some thoughts but can’t put in action.

A) If their contact counts are higher than usual then do something,
B) If the distance between them is lower than < (sum of their scales) / 2, so it means they are overlapping,

—But i can’t put them in action cause i don’t know how to repulse them since they are stuck. I’m looking forward for your replies. Thanks for reading!

when you pick a random location for a new star you could do a check for there would be a collision, and if there is, move the new star away from the random position until there isn’t a collision anymore. Or you could keep picking random positions until there wouldn’t be an initial collision. Either way, you’d want to have some sort of mechanism to avoid checking so many points that you miss frames.

1 Like

You have a really low opinion of the people here if your guess is that we tell you to effectively ignore the problem

I would probably do one of the following:

1. You could prevent this from happening, by utilizing an algorithm like Poisson Disc Sampling (possibly in 3D depending on where you spawn stars) to handle the spawning. It’s basically random but collision free spawning, and if there is no fitting spawn location after (adjustable) X-many tries, you stop trying. Depending on how high X is, this means that there is no spawn location anymore, so spawning a star anywhere, would always cause overlap which you dont want.

2. Going with your repulsion idea, you could loop through all stars that are within the collider. I’m assuming the stars basically have a circle / sphere collider, so that’s really easy to calculate. If not… then probably just use these, as i’m not sure for why you’d need more complex colliders, but maybe i’m missing something. Anyways, when you collide with something you detect is overlapping, simply calculate the directional vector away from the center of the other star, and apply a force to this star (or just moving it) in that direction. This effectively means all stars will “repell” each other. However, when you spawn tons of stars it will eventually slow down, so the above method has the advantage of stopping stars from spawning when there is no space to do so anymore.

2 Likes
``````void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Bird")
{
Destroy(collision.gameObject);

indexE = Random.Range(0, dynamicList.Count);

spawnAt = dynamicList[indexE];

{
starInstance = (GameObject)Instantiate(starPrefab, spawnAt.transform.position,starPrefab.transform.rotation);
}
}
``````

(This script is in Player which collides Star)

Guys!
spawnAt is an empty gameobject which i pick randomly. I added circle collider to spawnAt and if this collider is not Touching (!isTouching) gamobject with tag “Star” then spawn “Star” at this location. (To avoid overlapping stars, if there is already a star object then don’t spawn)

But when Star collides with spawnAt gameobjects then Star gets destroyed. This code is not working the way i thought. I don’t know why

I think i solved this problem by using “Physics2D.OverlapCircle” to check if there are any so-tagged “Enemy” in circleRadius then call SpawnEnemy method over again until it matches requirements.

I couldn’t find any else solution for overlapping, there must be any else solutions for this of course. I tried it works cool. But be careful if there are not any empty SpawnPoint, it probably will fall in endless loop. For may game, it won’t, cause game already will be over before it falls.

I leave my solution here. Just in case, this can help someone else as well.

``````void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Star")
{
SpawnEnemy();
}
}
public void SpawnEnemy()
{
if (dynamicList.Count > 0)
{
indexE = Random.Range(0, dynamicList.Count);
spawnAt = dynamicList[indexE];
if (spawnAt != null)
{