Finding points of 3D non-axis aligned box from min/max and angle

676 Views Asked by At

From the graphic depicted in this question: Check if a point is inside a rectangular shaped area (3D)?

Points $P_1$ and $P_7$ are known. They are opposite corners of the box. I can obtain Min/Max with $Min(P_1, P_7)$ and $Max(P_1, P_7)$. Although I'm not sure I need the actual min/max as long as both points are opposite.

I have a point $P_A$ between $P_1$ and $P_2$ on a vector $\lvert \vec V_2\rvert$ headed towards $P_2$.

How do I determine points $P_2$, $P_4$, and $P_5$?

What I have done so far:

  • Got the center point $P_C$ doing $(P_1$ + $P_7) / 2$.
  • Got and X (pitch) and Y (yaw) angles between $P_1$ and $P_A$.
  • Translated points $P_1$ and $P_7$ using $P_C$ to be center with origin.
  • Rotated the translated points $P_1$ and $P_7$.
  • Assuming the box is now axis aligned, computed $P_2$, $P_4$, and $P_5$ locally.
  • Rotate and then translate $P_2$, $P_4$, and $P_5$ back.

This seems to work. But it seems a bit cumbersome.

Is there an easier way?

My Goal is to "draw" a Box in 3D space that encompasses a room that is already 3D rendered. I'm working with a scripting language that is layered on top, and it gives very limited access. I can add to the environment and I have access to a Vector object, trig functions (sin/cos/tan/asin/acos/atan), cross product, and dot product. I don't have a Matrix, or other tools often found in graphics frameworks.

The room is sometimes axis aligned, and other times it is rotated from origin 45 degrees. Or a different angle. In this case I'm only dealing with Yaw rotation. In another case I may need to also apply Pitch, for going up or down a staircase. I will never need to apply Roll.

1

There are 1 best solutions below

2
On

If your box is axis-aligned, and $P_1 = (x_1, y_1, z_1)$, $P_7 = (x_7, y_7, z_7)$ in world space coordinates.

If $(P_1 P_4)$ is the oriented $x$-axis, $(P_1 P_2)$ is the oriented $y$-axis, and $(P_1 P_5)$ is the oriented $z$-axis.

Then:

$$P_4 = (x_7, y_1, z_1)$$ $$P_2 = (x_1, y_7, z_1)$$ $$P_5 = (x_1, y_1, z_7)$$

Simple right ? That's the interest of axis-aligned bounding boxes in a nutshell: you can define all vertices of your cuboid with just 2 vectors, and they give minimal computations (not just to find the vertices of the cuboid; they simplify intersections with the cuboid faces/planes as well, intersection becomes a simple bounding of a value between two others).

In any other case (not axis-aligned), you're going to need to handle at least rotations.

You're asking if there's a simpler way to do things... In the language of graphics pipelines, if your AABB is aligned in object space rather than world space, you'll generally use the "homogeneous model" (an embedding of $\mathbb{R}^3$ into a special version of $\mathbb{R}^4$) in order to convert a combination of rotation+scaling+translation of your object into a linear operation (since translations are not linear maps, as they move the origin of your vector space) to transform your data from world space to object space, and do your AABB intersections in object space.

This scheme (using a 4*4 matrix to translate a non-axis aligned box into an AABB) can be generalized to any two spaces. However, if your pipeline structure is not well thought-out, that might actually make you have extra computation, and oblique plane intersections in world space (or "unaligned space") might be faster to render than object space (or "aligned space").

AABBs are used in general to avoid having to do extra computations: if my ray doesn't even hit the surrounding AABB, it has no chance of hitting the curved object it contains (say a sphere, a 3D triangle-based model or a 3D fractal, for which intersections are much more costly to compute). This small extra check can often remove lots of costly computation.