Notifications
Clear all

[Closed] vector rotation?

I’m not sure where I’ve gone wrong in the vector rotation. Could someone help me out. The goal is for the ‘red’ camera to match the rotation which is represented by the yellow camera. The direction for rotation seems off.


-- test scene setup
delete objects
camA = FreeCamera wirecolor:orange transform:(matrix3 [0.268442,0.963296,0] [0.170671,-0.0475607,0.98418] [0.948056,-0.264196,-0.177174] [311.259,-86.7387,-58.1682])
camB = FreeCamera wirecolor:yellow transform:(matrix3 [-0.268442,-0.963296,-1.3411e-007] [0.170671,-0.0475611,0.98418] [-0.948056,0.264195,0.177174] [-311.259,86.7386,58.1684])

tm = camA.transform
origin = [0,0,0]

fn flip_tm_along_vector tm:undefined origin:[0,0,0] = 
(
	dist = distance tm.row4 origin
 	targetPos = (origin + (normalize (origin-tm.row4)) * dist )
 		
 	theDir = normalize (origin - targetPos) --a vector pointing from target pos to original pos
 	theZ = normalize (tm.row2) --the local Y axis
	theCross = cross theDir theZ --a vector perpendicular to both the Z and the direction vector
	theDir2 = normalize (cross theZ theCross) --a vector perpendicular to the Z and the cross product = projection of the direction in local XY
	theNewX = normalize (cross theDir2 theZ) --a vector perpendicular to both the projected direction and Z = the new local X
	newTM = matrix3 theNewX theDir2 theZ targetPos --make a matrix of the new X, Y and existing Z, keep the position
)
newTM = flip_tm_along_vector tm:tm origin:origin
cam = FreeCamera transform:newTM wirecolor:green

4 Replies
-- test scene setup
delete objects
camA = FreeCamera wirecolor:orange transform:(matrix3 [0.268442,0.963296,0] [0.170671,-0.0475607,0.98418] [0.948056,-0.264196,-0.177174] [311.259,-86.7387,-58.1682])
camB = FreeCamera wirecolor:yellow transform:(matrix3 [-0.268442,-0.963296,-1.3411e-007] [0.170671,-0.0475611,0.98418] [-0.948056,0.264195,0.177174] [-311.259,86.7386,58.1684])

tm = camA.transform
origin = [0,0,0]

fn flip_tm_along_vector tm:undefined origin:[0,0,0] = 
(
	dist = distance tm.row4 origin
 	targetPos = (origin + (normalize (origin-tm.row4)) * dist )
 		
 	dirVec = normalize (targetPos - origin) --a vector pointing from target pos to original pos
	upVec = normalize (tm.row2) --the local Y axis
	crossVec = cross dirVec upVec --a vector perpendicular to both the Z and the direction vector
	newTM = matrix3 theNewX upVec dirVec targetPos --make a matrix of the new X, Y and existing Z, keep the position	
)
newTM = flip_tm_along_vector tm:tm origin:origin
cam = FreeCamera transform:newTM wirecolor:green

scratch that…it doesn’t work…

I’m confused. What is it you’re trying to do? You want a camera that is mirrored around the provided origin but has its up axis matching? What about this, then:

fn oppositeCam srcCam tgCam origin:[0,0,0] =
(
	local tm = srcCam.transform
	tm.row4 = origin + (tm.row4 - origin) * -1
	tm.row1 *= -1
	tm.row3 *= -1
	tgCam.transform = tm
)

Or do you want some serious flipping?:

fn flipCam srcCam tgCam origin:[0,0,0] =
(
	local tm = srcCam.transform
	tm.row4 = origin + (tm.row4 - origin) * -1
	tm.row3 *= -1
	tgCam.transform = tm
	
	rotate tgCam (angleaxis 180.0 (normalize (tgCam.transform.pos - origin)))
	rotate tgCam (angleaxis 180.0 tgCam.transform.row3)
)