How to calculate which side of a cube is closest to a point?
What''s the best way to calculate which side of a cube is closest to a given point? Thanks.
Well, that was a waste of 2 minutes of my life. Now I have to code faster to get ''em back...
Calculate the Angle between the center of the cube and the specified point, then compare all of the face normals of the cube to this angle, whichever angle is closest, then that''s the side that''s facing the cube. This should work in theory.
Horny Farmer (Jolly Rancher)
Horny Farmer (Jolly Rancher)
Is this point inside the cube, or can it be anywhere? If the point is inside the cube, all you need to do is get the dot product between all the face normals and the point, and the face with the smallest result is closest to the point (let''s assume all face normals face inwards then). Otherwise it''s a bit more complicated.
Easy. As Zipster said, evaluate the of dot product of "Point - CubeCenter" against the three 'axes' of your cube. The largest absolute value corresponds to the side that is the closest.
At least, that's what I'm trying to picture in my head.
Of course, you have to deal with two faces on each axis, but that's easy.
Cédric
REEDIT: I think this works if the point is inside or outside.
[edited by - cedricl on July 23, 2002 5:09:55 PM]
[edited by - cedricl on July 23, 2002 5:12:47 PM]
At least, that's what I'm trying to picture in my head.
Of course, you have to deal with two faces on each axis, but that's easy.
Cédric
REEDIT: I think this works if the point is inside or outside.
[edited by - cedricl on July 23, 2002 5:09:55 PM]
[edited by - cedricl on July 23, 2002 5:12:47 PM]
Well, if the point is outside the cube, you have to realize that the dot product checks the distance to the plane, not just the surface, and the plane extends infinitely in all directions. If you have a point in the wildest location, yet it is coplanar with one of the surfaces, you get 0, and it thinks it is cloest to that plane. Yet in reality, it may be "closest" to another side. Ooo ooo ASCII picture
Above, while the point may seem closest to side B, the absolute dot product for side A is smaller.
How to solve? I guess you can say negative dot products take precedence over positive ones...
[edited by - Zipster on July 23, 2002 6:22:15 PM]
A----------| | .| | B| || |----------
Above, while the point may seem closest to side B, the absolute dot product for side A is smaller.
How to solve? I guess you can say negative dot products take precedence over positive ones...
[edited by - Zipster on July 23, 2002 6:22:15 PM]
the dotproduct with b is the biggest one. so thats the face..
"take a look around" - limp bizkit
www.google.com
"take a look around" - limp bizkit
www.google.com
Yup, you shouldn''t take the dot product against the planes, but rather, the ''axes''. In this case, the x axis will yield a much larger value than the y axis, so we should take the face on the x axis. Since the dot product is positive, we should take the right face. That''s it.
Think about it this way: with this technique, where is the limit? Where do we stop saying it''s closest to the right face? In your example, along the line y = x, which is correct.
Cédric
Think about it this way: with this technique, where is the limit? Where do we stop saying it''s closest to the right face? In your example, along the line y = x, which is correct.
Cédric
There could be 4 unique solutions.
Why ?
Rotate the cube so that a vertex is pointing toward the point.
All sides are equally close.
Bugle4d
Why ?
Rotate the cube so that a vertex is pointing toward the point.
All sides are equally close.
Bugle4d
i don't see why u need all the complicated dot product thingies, because if it's perfect square, you can just divide it in thsese four zones, and find out in which one the zones the point lies.
here's how the four zones look like.
the above method also works in 3d. it's just easier to explain it in 2d.
---
shurcool
wwdev
[edited by - shurcool on July 24, 2002 10:09:39 AM]
here's how the four zones look like.
\ a / \___/ |\ /|d | X | b |/ \| /---\ . / c \
if the point lies on a boundary, then it's equally distant from the corrisponding zones.the above method also works in 3d. it's just easier to explain it in 2d.
---
shurcool
wwdev
[edited by - shurcool on July 24, 2002 10:09:39 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement