[Closed] Rotating the camera target around the camera
Having a bunch of problems here, and I know its just something simple
I want to take the vector of the camera to the camera target, rotate in the local space of the camera, so the local coord x of the camera for example will continue to rotate in local X regardless of where the camera gets rotated. I’ve tried a bunch of things and just not making much progress.
So this isn’t working, but I’m not sure where its all breaking down. Any suggestions?
camerafacing = MyCamera.node.Target.pos – MyCamera.node.pos
rm = rotateZMatrix 10
movedir = camerafacing * rm
MyCamera.node.Target.pos=movedir
first you need to store the position of the target relative to the camera which can be easily calculated like this for example:
targetOffset = [0, 0, - distance cam.pos cam.target.pos]
the next line will rotate the target around the camera on the Y axis 45 degrees:
cam.target.pos = targetOffset * (preRotateY cam.transform 45)
Hi Nicholas,
here are some code samples to perform camera and target rotations. Code can be made more compact, but I guess it’s easier to understand this way.
To rotate a Target Camera around its own axis, we need to transform its Target. This has meaning for X and Y rotations only.
(
-- get currently selected Target Camera
local myCamera = $
-- get Camera TM
local m3CameraTM = myCamera.transform
-- get Target Position in World CS
local p3TargetWPos = myCamera.target.position
-- get Target Position in Camera CS
local p3TargetCPos = p3TargetWPos * inverse(m3CameraTM)
-- create the Rotation Matrix
local m3Rotation = rotateYMatrix 15 -- or rotateXMatrix <degrees>
-- rotate the Target in Camera CS
local p3TargetCNewPos = p3TargetCPos * m3Rotation
-- get new Target Position in World CS
local p3TargetWNewPos = p3TargetCNewPos * m3CameraTM
-- set new Target Position in World CS
myCamera.target.position = p3TargetWNewPos
)
To rotate a Target Camera around its Z axis, we need to access the Roll_Angle controller value:
(
-- get currently selected Target Camera
local myCamera = $
-- set Camera Roll Angle
myCamera.transform.controller.Roll_Angle.controller.value += 15
-- or the shorter version: myCamera[3][2].value += 15
)
To rotate a Target Camera around its Target, we need to create an Offset Matrix. This has meaning for X and Y rotations only.
(
-- get currently selected Target Camera
local myCamera = $
-- create a TM oriented like Camera TM translated in Target Position
local m3OffsetTM = myCamera.transform
m3OffsetTM.position = myCamera.target.position
-- get Camera Position in Offset CS
local p3CameraOPos = myCamera.position * inverse(m3OffsetTM)
-- create the Rotation Matrix
local m3Rotation = rotateYMatrix 15 -- or rotateXMatrix <degrees>
-- rotate the Camera in Offset CS
local p3CameraONewPos = p3CameraOPos * m3Rotation
-- get Camera new Position in World CS
local p3CameraWNewPos = p3CameraONewPos * m3OffsetTM
-- set new Camera Position in World CS
myCamera.position = p3CameraWNewPos
)
- Enrico
So I implemented the changes suggested for x,y rotations to see if it solved my problem. And in both cases I’m getting the Y rotation working, but if the camera is flat and the target is above or below the camera in Z space, the target slowly works its way down to being on the same Z height as the camera.
When the camera is tilted the target still makes odd movements, but its harder to track down.
tried a few different versions, trying some different approachs. Here is the latest code I’m using.
-- get Camera TM
local m3CameraTM = mCam.transform
-- get Target Position in World CS
local p3TargetWPos = mCam.target.position
-- get Target Position in Camera CS
local p3TargetCPos = p3TargetWPos * inverse(m3CameraTM)
-- create the Rotation Matrix
local m3Rotation = rotateYMatrix 15 -- or rotateXMatrix <degrees>
-- rotate the Target in Camera CS
local p3TargetCNewPos = p3TargetCPos * m3Rotation
-- get new Target Position in World CS
local p3TargetWNewPos = p3TargetCNewPos * m3CameraTM
-- set new Target Position in World CS
mcam.target.position = p3TargetWNewPos
Thanks for everyones help