How to compute the distance to a rounded box:
The box is of course centered at the origin and aligned to the xyz axes – you will have to pretransform the point to get the distance to an arbitrarily rotated and translated curved cube. |

## 4 thoughts on “distance to a curved box”

### Leave a Reply

You must be logged in to post a comment.

JanYou could use 8 spheres with the same radius as the rounded edge and position them in the corners. Then, find out which of the sphere-centers is nearest to position X, calculate the distance and subtract the radius.

adminHey, how do you think I found that formula? 🙂

The formula above computes the distance to 6 planes, 12 cylinders and 8 spheres but in a much more compact and efficient way that what you propose.

LexNICE!

To compare, I came up with the following kludzy way of just rounding off 4 sides of a pillar while playing with ‘4kgfxmon_pack’, doing some distance-field raymarching. So that’s not even including the rounded spherical corners like in your code.

float roundedPillar(vec3 p, vec4 plr, float radius)

{

float dist = 1000;

float size = plr.w;

vec3 rel = p-plr.xyz;

if (rel.x > size)

{

if(rel.z > -size && rel.z < size)

dist = rel.x – size;

else

{

if (rel.z < -size)

dist = length(rel.xz-vec2(size, -size));

else

dist = length(rel.xz-vec2(size, size));

}

}

else

{

if(rel.x -size && rel.z < size)

dist = -(rel.x+size);

else

{

if(rel.z size)

dist = rel.z – size;

else

dist = -(rel.z+size);

}

}

return dist-radius;

}

Note to self: Must learn to think like iq. 🙂

Thanks for posting!

(website is where I work)

adminthat’s how I kind of found it. You first do it in the obvious and expensive way, with the individual spheres ans cylinders and stuff, then you realize there is some symmetry so you only need one sphere and three cylinders, the you realize that a cylinder is an extruded 2D sphere, and so on. I like to do it directly in code, simplifying step by step until the final short version pops.