SHOW:
|
|
- or go back to the newest paste.
| 1 | - | function tocell(vec) |
| 1 | + | local center = cellsize / 2 |
| 2 | - | return Vector3.new(math.ciel(vec.x/cellsize),math.ciel(vec.y/cellsize),math.ciel(vec.z/cellsize)) |
| 2 | + | |
| 3 | function align(x) | |
| 4 | return math.floor(x + 0.5) + center | |
| 5 | - | function toworld(vec) |
| 5 | + | |
| 6 | - | return (vec-Vector3.new(.5,.5,.5))*cellsize |
| 6 | + | |
| 7 | function getCellsWithinRadius(Point, Radius) | |
| 8 | local Point = Point * cellsize | |
| 9 | - | function cellsinrange(pos,radius) |
| 9 | + | local HalfRadius = (Radius * cellsize) / 2 |
| 10 | - | local diameter=math.ciel((radius*2)/cellsize) |
| 10 | + | local near = {}
|
| 11 | - | local hdiameter=diameter*.5 |
| 11 | + | |
| 12 | - | local cells={}
|
| 12 | + | for x = Point.X - HalfRadius, Point.X + HalfRadius do |
| 13 | - | for xcell=1,diameter do |
| 13 | + | local fx = align(x) |
| 14 | - | for ycell=1,diameter do |
| 14 | + | for y = Point.Y - HalfRadius, Point.Y + HalfRadius do |
| 15 | - | for zcell=1,diameter do |
| 15 | + | local fy = align(y) |
| 16 | - | local cellpos=tocell(pos+toworld(Vector3.new(xcell-diameter,ycell-diameter,zcell-diameter))) |
| 16 | + | for z = Point.Z - HalfRadius, Point.Z + HalfRadius do |
| 17 | - | if (toworld(cellpos)-pos).magnitude<=radius then |
| 17 | + | |
| 18 | - | table.insert(cells,Vector3.new(cellpos.x,cellpos.y,cellpos.z)) |
| 18 | + | local v3p = Vector3.new(fx, fy, align(z)) |
| 19 | if ((Point - v3p).magnitude < Radius) then | |
| 20 | table.insert(near, v3p) | |
| 21 | end | |
| 22 | end | |
| 23 | - | return cells |
| 23 | + | |
| 24 | end | |
| 25 | ||
| 26 | return near | |
| 27 | end |