ChaseGarrett
ChaseGarrett

Reputation: 11

Finding absolute coordinates from relative coordinates in 3D space

My question is fairly difficult to explain, so please bear with me. I have a random object with Forward, Right, and Up vectors. Now, imagine this particular object is rotated randomly across all three axis randomly. How would I go about finding the REAL coordinates of a point relative to the newly rotated object?

Example: enter image description here

How would I, for instance, find the forward-most corner of the cube given its Forward, Right, and Up vectors (as well as its coordinates, obviously) assuming that the colored axis is the 'real' axis.

The best I could come up with is:

x=cube.x+pointToFind.x*(forward.x+right.x+up.x)
y=cube.y+pointToFind.y*(forward.y+right.y+up.y)
z=cube.z+pointToFind.z*(forward.z+right.z+up.z)

This worked sometimes, but failed when one of the coordinates for the point was 0 for obvious reasons.

In short, I don't know what do to, or really how to accurately describe what I'm trying to do... This is less of a programming questions and more of a general math question.

Upvotes: 1

Views: 1595

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 25992

In general, you would have to project all corners of the object, one after the other, on the target direction (i.e., compute the scalar or dot product of both vectors) and remember the point delivering the maximum value.

Because of the special structure of the cube, several simplifications are possible. You can rotate the target direction vector into the local frame. Then the determination of the maximal projection can be read off the signs of its local coordinates. If the sign of the coordinate is positive the scalar product is maximized by maximizing the cube coordinate to 1. If the sign is negative, then the scalar product is maximized by minimizing the cube coordinate to 0.

Inverse rotation is the same as forming dot products with the columns of the rotation matrix (forward, right, up), so

 result = zero-vector; //zero corner of the cube
 if( dot( target, forward ) > 0 )
      result += forward;

 if( dot( target, up ) > 0 )
      result += up;

 if( dot( target, right ) > 0 )
      result += right;

Upvotes: 1

Related Questions