Reputation: 103
I have a box which consists of 8 vertices and may be scaled or rotated in any direction so I apply the transformation matrix for vertices each transformation, I have center, size and directions on X,Y and Z from center, how can I check if any 3D point is inside the box ?
Upvotes: 0
Views: 3083
Reputation: 96176
If you have:
vec3 center; // Center of the box.
vec3 dx, dy, dz; // X,Y, and Z directions, normalized.
vec3 half; // Box size in each dimension, divided by 2.
vec3 point; // Point to test.
Then the test is as simple as:
vec3 d = point - center;
bool inside = abs(dot(d, dx)) <= half.x &&
abs(dot(d, dy)) <= half.y &&
abs(dot(d, dz)) <= half.z;
The primary property of dot product is that fact that
X⋅Y == |X|⋅|Y|⋅cos(angle_formed_by_X_and_Y)
.
If Y
is normalized, then X⋅Y
essentially gives you the length of the projection of X
onto Y
(which is negative iff the angle formed by X
and Y
is obtuse). Once you have the projection lengths, you just need to compare their absolute values with corresponding half-extents of your box.
Upvotes: 5