A rounded cube, the hard way

in #csg8 years ago

When I was almost obsessive in my attempts to create a digital worlds, it often happened I caught myself dissecting things I saw, thinking about how I could make them using primitives and constructive solid geometry. Of course there are objects in the real world that can't be made that way, unless you are happy with unrealistic results. And I usually was…

I started to design objects for a sci-fi world. On paper they were quite good, according to me, but the actual results were unsatisfying. Little by little I began to abandon the idea: simply I hadn't the number, the talent, the time, anything to reach the dreamed results.

Anyway, one of the problems I had to face when realizing my four-engines cargo ship was this: each engine is basicly a cube, but I wanted the edges to be rounded. The solution is to use a superellipsoid, but I met this object for the first time to solve my problem with the engine.

Another possibility is to use cylinders and half spherical wedges for the corners. Let's start with a dull box.

See? You could cut your fingers caressing those sharp edges! So, let's remove a piece, the one we want to replace with a smooth curve. Let's first remove a “step”. The operation is a difference between two “box”, our initial cube and a small parallelepiped (another box).

In the place of that “step”, we would like to put something rounded. What could it be? A cylinder! Though we need only a fourth of it; I hope you can get “why” from the next picture:

Now, you must repeat it for all the edges. But it is not over: you need a trick on the corners! In fact, you will see that if you apply blindly this recipe to all the edges, on the corners you obtain a sort of discontinuity. Not what we want! The recipe must subtract another box from each corner, and replace it with a spherical “wedge”.

Summary: a rounded cube is hard, if you want to do it this way! Luckly, it exists the superellipsoid! And this is what I've used for the “engine” of the cargo ship. I will show you in the next post!

Thanks for reading, and I hope you didn't get bored! … Just to say you good night, the next section talks about few details… There are pieces of POV Ray code, so maybe you prefer to go to sleep or change post… :-D

Few details

The dull box is a primitive of POVRay, as you can imagine:

box {
  <-S/2, -S/2, -S/2>, <S/2, S/2, S/2>
}

You can see the two 3D-vectors that specify the corners.

For the final rendering I've added a texture, which is what makes the surface as it looks, and rotated it because I didn't want to show it right head-on (it would have been harder to see the rounding of the edge). S is a constant, namely 8. To complete the scene you need also a camera (your eye) and a light source, but let's focus on the needed CSG.

We wanted to remove a piece, the one we're going to replace with our rounded edge. Here we need a difference, because we want remove a piece of the original box. We want to remove, in fact, another box, with different width, height, depth.

I did so:

difference {
  box {
    <-S/2, -S/2, -S/2>, <S/2, S/2, S/2>
  }
  box {
    <-S/2-.1, (S-R)/2, -S/2-.1>, <S/2+.1, S/2+.1, (-S+R)/2> 
  }
}

The new constant R is the radius of the rounded edge. I have chosen 1. When we do differences, we don't want planes to be aligned, overlapped, so to speek, because it causes problems. So I've moved a little bit (namely .1) the corners, so that the box we are subtracting protudes. Let's change difference into union and use a different color to have a view of the box we are subtracting.

We don't need to make it protruding so much. Instead of .1, you could use 0.01… It doesn't matter, really. It is enough it goes a little bit off the faces of the box we are subtracting from.

After the difference, we have a step we want to replace with something rounded… as a cylinder! Let's use union this way:

union {
  difference {
    box {
      <-S/2, -S/2, -S/2>, <S/2, S/2, S/2>
    }
    box {
      <-S/2-.1, (S-R)/2, -S/2-.1>, <S/2+.1, S/2+.1, (-S+R)/2> 
    }
  }
  cylinder {
    <-S/2, (S-R)/2, (-S+R)/2>, <S/2, (S-R)/2, (-S+R)/2>, R/2
  }
}

We need to position the axis of the cylinder properly, so that it “sink” properly in the step, and pick the right radius. It's easier than it might seem once you play a little bit with it. We can say that the final object is given by the union between a cylinder and a object resulting from the difference of two boxes. Let's give the cylinder its own red color, so that we can see it:

Here it is our quarter of a cylinder! The rest is melted with the box, so we see only the part that filled the step created by the difference.

Coin Marketplace

STEEM 0.19
TRX 0.18
JST 0.034
BTC 89422.86
ETH 3144.72
USDT 1.00
SBD 2.76