I want to make an RTS and there have buildings that have a radius where they notice if an other building is placed.
The buildings do of course not move. But since I will have a lot of buildings and collisions performance is important.
I’m not quite sure what the best performing solution to this is.
I have two ideas:
Give every building that needs to detect other buildings a spherecollider that ignores all but overlaps with given object types. But I’m not sure how setting almost everything to ignore is helping performance wise.
Only let the building that is placed check all collisions, either via spherecollider or via sphereoverlapactors
Do you have a better solution, or can you tell me which solution is better?
Hi, if I understand you correctly, then when you place a building you want to notify/call an event on all buildings that are within a certain radius.
Then I would just loop through all buildings you have on the map and check the distance and if it is closer than the certain radius you want, call an event on them. Or if you want to do this via event dispatchers, then every time you place a building call an event dispatcher that all buildings are listening to (e. g. some building manager) and then check the distance and if it is closer than a certain radius do something.
From my experience, collisions are fairly expensive (although if you’re not placing hundreds of buildings
on tick, it shouldn’t matter much whether you use distance checks or use a sphere collision test). So taking an array of 10.000.000 random vectors (random between -1000.0f and 1000.0f in each coordinate and created on begin play) and on tick creating another random vector A and looping through those 10.000.000 vectors and computing the distance to the vector A results in a performance hit of around 5ms.
Vectors.Reserve(10000000);
for (int32 i = 0; i < 10000000; i++)
{
Vectors.Add(FVector(FMath::RandRange(-1000.0f, 1000.0f), FMath::RandRange(-1000.0f, 1000.0f), FMath::RandRange(-1000.0f, 1000.0f)));
}
FVector A = FVector(FMath::RandRange(-1000.0f, 1000.0f), FMath::RandRange(-1000.0f, 1000.0f), FMath::RandRange(-1000.0f, 1000.0f));
int32 NumVectors = Vectors.Num();
for (int32 i = 0; i < NumVectors; i++)
{
FVector::Dist(Vectors[i], A);
}