-1
\$\begingroup\$

I have a camera that rotates around a target point on mouse move. How can I move the camera's position upwards/downwards related to the target position? So when camera is moved upwards the object is seen to go downwards and opposite.

I was trying to change target vector y, camera position vector y but none of it really worked:

void Camera::move(float dir)
{
    m_target.y += dir;
}

and

void Camera::move(float dir)
{
    m_pos.y += dir;
}

I got to confess I'm not strong at linear algebra and most likely that's the reason why I don't understand it well. I was trying to google it however all results are about forward/backward camera movement which is as easy as to change m_radius class member.

My camera's position is described by radius, theta and phi angles (spherical coordinates it's called, I guess).

This is how I set position m_posand then call XMMatrixLookAtLH

setPosition(m_target.x + m_radius * cosf(m_theta) * cosf(m_phi), m_target.y + m_radius * sinf(m_theta), m_target.z + m_radius * cosf(m_theta) * sinf(m_phi));
setLookAt(XMLoadFloat3(&m_pos), XMVectorZero(), XMLoadFloat3(&m_up));

Target is always set in origin (0, 0, 0) because all my model data is loaded related to the origin. I added target position effect to the setPosition() method just in case I will ever need to change it.

\$\endgroup\$
4
  • \$\begingroup\$ Spherical coordinates are used to calculate the camera's lookat, up and right vectors, not position. \$\endgroup\$
    – Hermetix
    Jan 9, 2018 at 10:19
  • \$\begingroup\$ @Hermetix so there is no way I can achieve the desired result with such coordinate system? \$\endgroup\$
    – Mr Jox
    Jan 9, 2018 at 10:33
  • \$\begingroup\$ You will have to show us your code that calculates the camera's orientation/position. Basically we don't know exactly what "m_target" and "m_pos" represent. \$\endgroup\$
    – Hermetix
    Jan 9, 2018 at 11:03
  • \$\begingroup\$ @Hermetix added \$\endgroup\$
    – Mr Jox
    Jan 9, 2018 at 11:18

1 Answer 1

0
\$\begingroup\$

If I understood what you wanted to do correctly, you will have to do some vector addition. Create a vertical vector B of length equal to the distance you want to move up (say (0,2,0) for a vertical distance of 2), and add it to the pos vector. Set your new pos vector to the result of the addition.

If you want to visualize it:

enter image description here

Here A would be your pos vector and B your upward movement. Note that B here is not vertical, it's just an example.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks that's the right answer and turns out I was doing the same thing and yet none of it worked and my guesses were right why it didn't work but I could find out why it's still bugged. It turns out when I set my XMMatrixLookAt I feed XMVectorZero instead of target and so when camera moves upwards/downwards it looked like it moves by on orbit. \$\endgroup\$
    – Mr Jox
    Jan 10, 2018 at 8:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .