# How to get voxels or octrees near the player?

Okay so i made a script which adds a object to and finds out what cell the object would be in.

example

``````    local X = math.floor(Object.X/Octree.Size) * Object.X
local Y = math.floor(Object.Y/Octree.Size) * Object.Y
local Z = math.floor(Object.Z/Octree.Size) * Object.Z
``````

the i would insert them into a table

When visualizing the cells it works great now im trying to get voxels near a player. How do i get these without looping through all of them? Ive never made an algorithm like this before I got help from creators who made modules using this way. Cells arent a 3d object either they just store the position value.

2 Likes

Boosting post since I want to know how other do this.

3 Likes

Could you explain this part a bit? Like what decides what cell the object is supposed to be in, what you mean by â€śadds a object toâ€ť (is â€śtoâ€ť a location?)

2 Likes

For example i have a base part called â€śRed Blockâ€ť in the world. I add it through this function and returns the cell it is in. I use this calculation to determine it. I floor its position by the size of the voxel (In studs) and multiply it by its position again.

Voxels are grid shaped. Ive seen other developers use this when they want to attach functions to thousands of instances in the game. Since that would cause lag they put them into these cells and only get the cells near the player or in view of the players camera. Example: WindShake etc.

Something like this. And i want to filter through these voxels to determine the ones closest to the player without iterating through all of them since thats horrible performance. I know its possible i just dont know what other devs use

2 Likes

Iâ€™m not sure exactly how to do this, but I would suggest you take a look at how people do 3D simulations.

something like this or this

2 Likes

Thank you im still a little confused i learned what i have to know now though.

2 Likes

Tbh I tried learning those things and couldnâ€™t understand either lol

Although why are you even putting everything in the game into a table? Wouldnâ€™t it be easier to literally just look for the objects nearby using some physical method (like just adding a part and doing getPartsInPart)?*

*Because in that case roblox would do the whole searching for you

1 Like

Instead of doing spatial queries i wanted to do spatial partioning instead which is this. This method is faster i heard and its for a ton of objects. Instead of doing spatial queries and looping through the thousand if objects i plan to have. This method is way faster than a linear search

1 Like

But Iâ€™d assume that roblox would use spatial partitioning anyways for stuff like getPartsInPart

Then again im not 100% sure lol

1 Like

Probsbly they do, also spatial partioning has many use cases than this too i just wanted to lestn how i can make my own

i would still have to iterate through the objects

2 Likes

Boosting post again? I should change the title to something more in depth i guess