I have been trying to do this for many hours now, but I can't get the right result :(
I have two vectors: forward (fx, fy, fz) and up (ux, uy, uz). They are perpendicular unit vectors represented with 3 fixed variables each. Together, they represent a view direction for the camera. What I want is to set the camera to view down the forward vector with up being the upwards direction for the camera.
To do that, I need to convert from those two vectors to the corresponding camera yaw, pitch and roll.
Anyone here who have had success with something like that?
6 hours and lots of tests later, I got something that seems like it works.
In case anyone else could use it..
voidGetYawPitchRoll(pointforward,pointup,outfixedyaw,outfixedpitch,outfixedroll){pitch=ASin(-forward.z);fixedcosPitch=SquareRoot(1-forward.z*forward.z);//Check if we are looking straight up or downif(cosPitch==0||AbsF(forward.z)>=1){if(pitch>0){yaw=0;roll=ATan2(-up.y,-up.x)+180;}else{yaw=0;roll=-ATan2(up.y,up.x)+180;}}else{fixedcosYaw=forward.x/cosPitch;fixedsinYaw=forward.y/cosPitch;yaw=ATan2(sinYaw,cosYaw);fixedcosRoll=up.z/cosPitch;fixedsinRoll;if(AbsF(cosYaw)<AbsF(sinYaw)){sinRoll=-(up.x+forward.z*cosRoll*cosYaw)/sinYaw;}else{sinRoll=(up.y+forward.z*cosRoll*sinYaw)/cosYaw;}roll=ATan2(sinRoll,cosRoll);}//Keep all angles in [0, 360]if(yaw<0)yaw+=360;elseif(yaw>=360)yaw-=360;if(pitch<0)pitch+=360;elseif(pitch>=360)pitch-=360;if(roll<0)roll+=360;elseif(roll>=360)roll-=360;}
Edit:
There still seems to be some issues with calculating sinRoll, when AbsF(cosYaw) < AbsF(sinYaw)..
Edit2:
I recalculated it all, and fixed an error in the G++ compiler. Now it works.
I have been trying to do this for many hours now, but I can't get the right result :(
I have two vectors: forward (fx, fy, fz) and up (ux, uy, uz). They are perpendicular unit vectors represented with 3 fixed variables each. Together, they represent a view direction for the camera. What I want is to set the camera to view down the forward vector with up being the upwards direction for the camera.
To do that, I need to convert from those two vectors to the corresponding camera yaw, pitch and roll.
Anyone here who have had success with something like that?
6 hours and lots of tests later, I got something that seems like it works.
In case anyone else could use it..
Edit:
There still seems to be some issues with calculating sinRoll, when AbsF(cosYaw) < AbsF(sinYaw)..
Edit2:
I recalculated it all, and fixed an error in the G
++
compiler. Now it works.