I'm currently trying to write a LookAt function in C++ with the Source SDK. I did some research and found many links but many of this were about Unity or Glm and were using quaternions and rotation matrix, but I don't need this.
So, here is my problem:
I'm in Portal 2. I have a Target with position coordinates x, y, z and I have the position of my player and his rotation in degrees (yaw, pitch, roll). When I use my function with the target coordinates as parameters, I want my player to look at the target.
I found this : Point look at Point but it doesn't work really well, the final angle was never good.
I think it's because in Portal 2, if I refer to this image:
For rotation, the yaw axis correspond to the y coordinate in the game, the pitch is the z one (from back to front of the player), and the roll is the x one.
For translation :
The z axis of the image correspond to the z axis in the game, the y is the x one, and the x is the y one.
It's pretty hard for me to adapt codes I found on internet to my needs.
Could you help me for this ?
Thank you in advance.
The code I currently have is this one :
float xdistance = pos.x - target.x;
float ydistance = pos.y - target.y;
float zdistance = pos.z - target.z;
float xzdistance = sqrtf(xdistance * xdistance + zdistance * zdistance);
//Final angle:
QAngle a = { RAD2DEG((atan2f(ydistance, zdistance))), RAD2DEG(-(atan2f(xdistance, zdistance))), 0 };