Jump to content
  • Advertisement
Sign in to follow this  
ZeRaW

Bounding Box for a cylinder

This topic is 4848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I need to find the bounding box for a rotated cylinder on an arbitrary axis, but i seem to have some trouble doing it. I did a bounding box for a cylinder, but the cylinder is on an x-axis and has no rotation. The bounding box must be bigger as the cylinder rotates on any axis. The box's shape must change at runtime. The cylinder has 4 properties which are: the radius, the height, the center and the normal. The box has the center and its 8 pts or vertices. Any help is very much appreciated. thanks

Share this post


Link to post
Share on other sites
Advertisement
It's probably quicker, and almost certainly easier, to not calculate the exact AABB around the cylinder, but to calculate it around a shape that contains the cylinder. Two candidates are:

1. A capsule aligned along the cylinder
2. oriented box with an axis along the cylinder axis, length equal to the cylinder length and other sides equal to the cylinder diameter.

The first is quicker to generate than the second, but will probably not result in such a tightly fitting AABB.

Share this post


Link to post
Share on other sites
If you're really intent on finding the exact AABB, you could use a support mapping function to project the cylinder onto the three cardinal axes. The support mapping for an axis-aligned cylinder can be found in the SOLID library; for an arbitrarily aligned cylinder you will need to apply the appropriate transformation to the axes of projection (which can be simplified since the axes are the world axes).

This may be more effort than it's worth though, and as Mr. Rowl suggested there may be better (or at least easier) alternatives.

Share this post


Link to post
Share on other sites
Let cylinder have end points A,B and radius r,

Bounds in direction X (same for Y and Z) can be found as:
Let A.X<B.X (otherwise swap points)
Good approximate lowest bound is A.X-r and highest is B.X+r (precise for capsule). At worst, in one direction it can be larger than needed by 2*r.

Accurate bounds for cylinder is
A.X-kx*r, B.X+kx*r
where kx=sqrt(((A.Y-B.Y)^2+(A.Z-B.Z)^2)/((A.X-B.X)^2+(A.Y-B.Y)^2+(A.Z-B.Z)^2))

similar thing for Y and Z
(i.e.
ky=sqrt(((A.X-B.X)^2+(A.Z-B.Z)^2)/((A.X-B.X)^2+(A.Y-B.Y)^2+(A.Z-B.Z)^2))
kz=sqrt(((A.X-B.X)^2+(A.Y-B.Y)^2)/((A.X-B.X)^2+(A.Y-B.Y)^2+(A.Z-B.Z)^2))
)

How derived: geometric reasoning. Bounds of cylinder is same as for 2 circles centered on A and B. This sqrt thingy gives sine of angle between axis and direction, used to find projection of radius.

Share this post


Link to post
Share on other sites
I found that a nice representation for this was to generate a matrix that could take a point and transform it into the space of the OBB of the cylinder such that the OBB was the unit cube. Then you can do trivial rejection by putting the test primitive in this space and testing against the unit-cube, which is usually a pretty simple test (sphere is simpler in some cases though).

You can do this because you know that the OBB for the cylinder is the unit-cube scaled about one axis equal to the half-length of the cylinder, and about the other two axes as the radius. Then it is rotated so that the long-axis is aligned with cylinder axis in world space (you can generate this directly from the axis and a couple cross products). Then it is translated s.t. the origin is the midpt of the cylinder. Multiply these together and invert and there you are. Maybe it sounds complicated in language, but the math is straightforward.

Whoa, I just realized that maybe you want the AABB of the cylinder. Thas is easy also. The 2 points that define the long axis of the cylinder are A and B: then an AABB that contains this segment is:

minBB = min(A,B); // for each dimension x,y,z
maxBB = max(A,B);

In addition to this AABB, you need to add the radius of the cylinder (which will give the AABB of the capped cylinder):

minBB -= vec3(radius, radius, radius);
maxBB += vec3(radius, radius, radius);

done, and done.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajas95
I found that a nice representation for this was to generate a matrix that could take a point and transform it into the space of the OBB of the cylinder such that the OBB was the unit cube. Then you can do trivial rejection by putting the test primitive in this space and testing against the unit-cube, which is usually a pretty simple test (sphere is simpler in some cases though).

You can do this because you know that the OBB for the cylinder is the unit-cube scaled about one axis equal to the half-length of the cylinder, and about the other two axes as the radius. Then it is rotated so that the long-axis is aligned with cylinder axis in world space (you can generate this directly from the axis and a couple cross products). Then it is translated s.t. the origin is the midpt of the cylinder. Multiply these together and invert and there you are. Maybe it sounds complicated in language, but the math is straightforward.

Whoa, I just realized that maybe you want the AABB of the cylinder. Thas is easy also. The 2 points that define the long axis of the cylinder are A and B: then an AABB that contains this segment is:

minBB = min(A,B); // for each dimension x,y,z
maxBB = max(A,B);

In addition to this AABB, you need to add the radius of the cylinder (which will give the AABB of the capped cylinder):

minBB -= vec3(radius, radius, radius);
maxBB += vec3(radius, radius, radius);

done, and done.


Well, as I said above(tho it was sorta hard to read), for precise bounding box one can use same thing but with
minBB -= vec3(kx*radius, ky*radius, kz*radius);
maxBB += vec3(kx*radius, ky*radius, kz*radius);
where
kx=sqrt(((A.Y-B.Y)^2+(A.Z-B.Z)^2)/((A.X-B.X)^2+(A.Y-B.Y)^2+(A.Z-B.Z)^2))
ky=sqrt(((A.X-B.X)^2+(A.Z-B.Z)^2)/((A.X-B.X)^2+(A.Y-B.Y)^2+(A.Z-B.Z)^2))
kz=sqrt(((A.X-B.X)^2+(A.Y-B.Y)^2)/((A.X-B.X)^2+(A.Y-B.Y)^2+(A.Z-B.Z)^2))
for precise bounding box (and all equal to 1 for bounding box of capsule).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!