Hi, im trying to create a system that really requires checking if vectors are close to each other (currently with .magnitude), the problem with this that i encounter is that the amount of calculations required to determine the distance is exponential to the amount of vectors there are, which will lag the game at only 300~ different vectors.

its pretty integral to this system that i can get upwards of 2000 vectors to calculate in realtime but im at a loss coming up with an effective solution…

Have you benchmarked your code? We also need more context - for example, how often are you running this code and are you able to reduce the number you’re checking over? The calculations you’re trying to make should be fairly negligible on performance but this response doesn’t seem applicable to your case under the assumption that you’re performing massive amounts of checks in short periods of time (e.g. every frame).

the calculation is binded to renderstepped so i want it to be as resource limited as possible, i would intend to limit the fps of the calculations when its too intensive but thats not fun.

You could try dotting the (difference) vector instead of magnitude, as the dot of a vector onto itself is the magnitude squared.

when asking optimization questions its best to provide as much about the underlying problem at and as you can because an optimization is always a case of considering priori and using that to remove redundancies in the calculation work. so telling us about any possible invariants in the calculations being done can go a long way, otherwise you’re stuck with O(n^2) complexity here.

without any prior knowledge about the things that need to be done here, keep a graph of position vectors as vertices and distance vectors are edges while only updating edges connected to a vertex that has changed it’s position would let give you a lazier evaluation step, speeding up the general case. this wouldn’t effect the pathological case when all of the vertices update however.

I often wonder this myself, using too much magnitude, calculating thousands of parts and their distances away from my character is too much, I have thought about Raycasting and GetTouchingParts on a part thats double the players size and touches other parts, I have also thought about using Dot products then getting the Magnitude of those parts… Those are pretty useful too.

that sounds pretty good, for more context, its a particle based fun physics thing where particles will interact with particles they can collide with. maybe categorize particles in a table for each particle based on their distance… though that would be shifting the problem from exponental computational time to memory usage from all the tables

so this is similar in structure to a n-body problem?

pretty much exactly, trying to make nice water or sand simulations

if you insist on making it in roblox theres some tricks you can do by stapling a integrator to a particle system although it wont be physically significant just convincing. other than that i’d say pick up a tool that is more conductive for that sort of work.

thanks, theres plenty of dynamic fluid sims in unity and others so thought this would be an interesting project if anything