vector - How to calculate azimut & elevation relative to a camera direction of view in 3D ...? -
i'm rusty bit here.
i have vector (camdirectionx, camdirectiony, camdirectionz) represents camera direction of view. have (camx, camy, camz) camera position.
then, have object placed @ (objectx, objecty, objectz)
how can calculate, camera point of view, azimut & elevation of object ??
the first thing do, simplify problem, transform coordinate space camera @ (0, 0, 0) , pointing straight down 1 of axes (so direction (0, 0, 1)). translating camera @ (0, 0, 0) pretty trivial, won't go that. rotating camera direction (0, 0, 1) little trickier...
one way of doing construct full orthonormal basis of camera, stick in rotation matrix , apply it. "orthonormal basis" of camera fancy way of saying 3 vectors point forward, up, , right camera. should @ 90 degrees each other (which ortho bit means), , should of length 1 (which normal bit means).
you can these vectors bit of cross-product trickery: cross product of 2 vectors perpendicular (at 90 degrees) both.
to right-facing vector, can cross-product camera direction vector (0, 1, 0) (a vector pointing straight up). you'll need normalise vector out of cross-product.
to vector of camera, can cross product camera direction vector right-facing vector calculated. assuming both input vectors normalised, shouldn't need normalising.
we have orthonormal basis of camera. if stick these vectors rows of 3x3 matrix, rotation matrix transform our coordinate space camera pointing straight down 1 of axes (which 1 depends on order stick vectors in).
it's easy calculate azimuth , elevation of object.
to azimuth, atan2
on x/z coordinates of object.
to elevation, project object coordinates onto x/z plane (just set y coordinate 0), do:
acos(dot(normalise(object coordinates), normalise(projected coordinates)))
this give positive angle -- want negate if object's y coordinate less 0.
the code of like:
fwd = vec3(camdirectionx, camdirectiony, camdirectionz) cam = vec3(camx, camy, camz) obj = vec3(objectx, objecty, objectz) # if fwd normalised can skip fwd = normalise(fwd) # translate camera @ (0, 0, 0) obj -= cam # calculate orthonormal basis of camera right = normalise(cross(fwd, (0, 1, 0))) = cross(right, fwd) # rotate camera pointing straight down z axis # (this matrix multiplication) obj = vec3(dot(obj, right), dot(obj, up), dot(obj, fwd)) azimuth = atan2(obj.x, obj.z) proj = vec3(obj.x, 0, obj.z) elevation = acos(dot(normalise(obj), normalise(proj))) if obj.y < 0: elevation = -elevation
one thing watch out cross-product of original camera vector (0, 1, 0) return zero-length vector when camera facing straight or straight down. define orientation of camera, i've assumed it's "straight", doesn't mean when it's facing straight or down -- need rule.
Comments
Post a Comment