Author Topic: Rotate Camera in world space axis pointing to a direction vector  (Read 1529 times)

Offline EnduroMorhan

  • byte
  • *
  • Posts: 1
    • View Profile
I have a camera moving freely in space where there are no up an down or left or right. But The camera moves up and down or left and right over it's own local axises when the user scroll finger over the screen, and it can move forward in The camera's direction no matter in which way the user scrolls it.
What I want now is that if The camera is closer than a certain distance to a sphere it should stop moving and turn to look at the sphere's center, but still keep a logic orientation according to what the camera had before looking at the center of sphere.

I have tried to use lookAt but in some cases it flips around leaving the user in lost of control over where he is anymore.
I have also tried to add a dummy to the center of the sphere and a Child object to that and translated it to the distance that I wanted, but to be able to place the camera on that dymmy position I first need to rotate the dummy rig so that the child dummy is where the camera is. But then I dont know how to rotate the parent dummy so that the child dymmy will be located at the point where the camera is located.

Hope someone understands my jibberish explanation because it's hard for me to explain it better.

But if you do, how can I rotate the dummy and place the camera at the child's position and orient the camera in a logic way without flipping and turning so that The user loses sense of where he is anynmore.

Thanks in advance.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Rotate Camera in world space axis pointing to a direction vector
« Reply #1 on: October 29, 2021, 01:49:13 pm »
The lookAt()-method is prone to this flipping issue, because of the way in which the algorithm works. There's another method in Matrix that might help:

https://www.jpct.net/jpct-ae/doc/com/threed/jpct/Matrix.html#setOrientation-com.threed.jpct.SimpleVector-com.threed.jpct.SimpleVector-

Assuming that you have the direction vector and also have/can create the matching up vector, it returns a rotation matrix, that aligns an object with these vectors. If you invert this matrix, you get something that you can feed into the camera as a view matrix using setBack(<Matrix>)...I guess... ;)