So I am trying to get the amount of particles in a part.
GetTouchingParts will not work because the particles are not objects but tables that has a position and a velocity.
I tried looping through all the particles and checking if they are inside the cell or not, but that was super laggy.
Here is the functions I am using.
function module:ParticleIn(Cell:BasePart, Particle)
local Position:Vector3 = Particle.Position
local CellSize = Cell.Size
local Center = Cell.Position
local MinX = Center.X - CellSize.X / 2
local MaxX = Center.X + CellSize.X / 2
local MinY = Center.Y - CellSize.Y / 2
local MaxY = Center.Y + CellSize.Y / 2
local MinZ = Center.Z - CellSize.Z / 2
local MaxZ = Center.Z + CellSize.Z / 2
local Inside = Position.X >= MinX and Position.X <= MaxX and
Position.Y >= MinY and Position.Y <= MaxY
return Inside
end
function module:CellParticles(Cell:BasePart, Particles)
local ParticlesCount = 0
for I, Particle in pairs(Particles) do
if module:ParticleIn(Cell, Particle) then
ParticlesCount += 1
end
end
return ParticlesCount
end
I need to run the CellParticles function about 1000 times every frame.
I tried to make the ParticleIn function just return false without calculating, And the lag was almost gone. So I think the ParticleIn function is causing the problem.
Theres no real way to make that function less laggy since its basically as efficient as a collision check could be, i just think running a collision calculation 1000 times a frame may not be the greatest idea, what are you using it for?
I could make it run slower and save the result in a table (I think this is called baking)
But I think there is a way to do it without a lot of lag, Because the ParticleIn is the only function that is lagging, everything else seems to be fine.