Relative Content

Tag Archive for pythonmath3d

What is the most efficient way to detect when a moving 3D position entered a new area delimited by a fixed scale?

Context: I have a CPU based raytracing engine written in Pygame, it works by having rays move 1 unit per loop in the direction of their velocity and detecting any voxel at that integer position. I recently added chunks to boost performance, voxels in each area are stored in a container of a fixed size which is read first. For example: If the chunk size is 16 the chunk at position (0, -16, 32) will store all data from it to position (16, 0, 48). Valid chunks are stored in a dictionary indexed by their start corner tuple, the end corner can be obtained by adding the size to it. Here’s an example of the data structure, in this case the chunks are None since their data and how it’s used is irrelevant to my question.

Get distance from a point to the nearest box

I have a 3D space where positions are stored as tuples, eg: (2, 0.5, -4). If I want to know the distance between two points I just do dist = (abs(x1 -x2), abs(y1 - y2), abs(z1 - z2)) and if I want a radius distf = (dist[0] + dist[1] + dist[2]) / 3. Now I have boxes each defined by two min / max positions (eg: (-4 8 -16) to (4, 12, 6)) and I want to know the distance between my point to the closest one: What is the simplest way to know the distance to the closest face in all 3 directions, or 0 in case the position is inside a box? Just looking for the lightest solution that doesn’t require numpy or libraries other than defaults like math since I’m not using those in my project.