Periodic boundary conditions and 3d objects

I want to have objects (let’s say spheres) moving around in a box with periodic boundary conditions. Meaning, when the objects are about to go outside the box on one side, they enter from the opposite side again.

Right now I’m just treating them as point like objects and as such they “teleport” to the opposite side, once their center exits the box.

However, it would look way better if the mesh of the objects smoothly disappeared on one side and again smoothly appeared on the opposite side.

For the smoothly disappearing part, I know, I can use clipping planes. But I have no idea how I can make the mesh appear on the other side again.

Any ideas or help would be appreciated.

You can use two spheres and two planes like this:

1 Like

I guess that is the only way of doing it. However one would need 26 times as many objects to consider every boundary of the box.

On an other note, is it possible to make the clipped mesh look closed? To make it look like the sphere here is solid.

Why 26 times?

You need to add 6 boundaries to each sphere material, since a box has 6 sides, so one sphere is allowed only inside the box and the other - only outside. Boundaries are the same, just with flipped normals.

So for every sphere flying inside the box, there will be its duplicate flying outside, which doubles the number of spheres.

Since the clipping planes match the box walls, you don’t need to close the spheres, they will be closed by the box walls.

Hmm maybe you are right. I just thought about what happens when a sphere would go right through a corner and got a bit confused.

Edit: the way I’m seeing it, a sphere going through a corner should show up 8 times in the cube (in each corner).

But what if i want to look inside the box from outside? Sorry I didn’t clarify that. The box should just be a virtual concept, so basically invisible/transparent.

If the sphere goes diagonally thru the box, you’ll need to calculate the proper distance/angle of its counterpart, so when one pokes outside the box, the second one pops in.

I don’t know how to auto-close the sphere, my wild guess is that pixels are discarded in the fragment shader after comparing their position relative to all clipping planes. You could make a “lid” out of circle geometry and close the hole but it’d take a fair amount of math.

Not sure what you mean by sphere appearing 8 times. Without clipping, if a sphere center is outside the box, you move it to the other side, you don’t make 8 spheres, even in the corner. Clipping just provides a smooth version of that.

But that way a sphere is not always fully visible inside the box. I basically want a 3 torus geometry for the boundary conditions.