Jenna Huntsman Posted February 4 Share Posted February 4 (edited) Hey all - been trying to bash my head at getting this to work but I fear I'm a little rotationally challenged. I've got an object, which should always point (+Y axis) at the user's camera while keeping the object's +Z axis aligned with the camera's UP axis, but I'm having issues. The code I've got so far is this: orientToCameraMaintainUp(rotation camRot, vector camPos) { //Rotate the object to the camera, while also keeping the object's +Z axis relative to camera. WARNING: SUFFERS FROM GIMBAL LOCK rotation inv = <0.00000, -0.00000, -0.70711, 0.70711>; //-90deg on Z axis. vector target = camPos - llGetPos(); camRot = Vec2RotTrue(target); //Rotate X+ towards camera. camRot = inv * camRot; //Rotate Y+ towards camera. list changes = [PRIM_ROTATION,camRot]; llSetLinkPrimitiveParamsFast(LINK_ROOT,changes); } rotation Vec2RotTrue( vector V ) { //Rotate to look at V while maintaining up axis. Credit: Dora Gustafson V = llVecNorm( V ); vector UP = < 0.0, 0.0, 1.0 >; vector LEFT = llVecNorm(UP%V); UP = llVecNorm(V%LEFT); // you want to keep the direction of V return llAxes2Rot(V, LEFT, UP); } //https://wiki.secondlife.com/wiki/User:Dora_Gustafson/llAxes2Rot_right_and_wrong Which works pretty well... but it suffers from gimbal lock if the camera is pointing straight up or straight down (or close to either). I haven't got any good ideas about how to solve the problem, and my understanding of quaternion math isn't great. Hopefully someone with more knowledge than I can point me in the right direction - all tips appreciated! Edited February 4 by Jenna Huntsman Link to comment Share on other sites More sharing options...
Jenna Huntsman Posted February 4 Author Share Posted February 4 Ended up managing to solve this by a dumber method than I thought was needed. I modified Dora's function to have it's UP vector multiplied by the rotation of the camera, so the UP direction is kept relative to the camera rather than the world. orientToCameraMaintainUp(rotation camRot, vector camPos) { //Rotate the object to the camera, while also keeping the object's +Z axis relative to camera. WARNING: SUFFERS FROM GIMBAL LOCK rotation inv = <0.00000, -0.00000, -0.70711, 0.70711>; //-90deg on Z axis. vector target = camPos - llGetPos(); //camRot = Vec2RotTrue(target); //Rotate X+ towards camera. Suffers gimbal lock. camRot = Vec2RotTrueRelative(target, camRot); //Rotate X+ towards camera. No gimbal lock. camRot = inv * camRot; //Rotate Y+ towards camera. list changes = [PRIM_ROTATION,camRot]; llSetLinkPrimitiveParamsFast(LINK_ROOT,changes); } rotation Vec2RotTrueRelative( vector V, quaternion R ) { //Rotate to look at V while maintaining RELATIVE up axis. Credit: Dora Gustafson, Jenna Huntsman V = llVecNorm( V ); vector UP = < 0.0, 0.0, 1.0 > * R; //Keep UP axis relative to input rotation R vector LEFT = llVecNorm(UP%V); UP = llVecNorm(V%LEFT); // you want to keep the direction of V return llAxes2Rot(V, LEFT, UP); } //https://wiki.secondlife.com/wiki/User:Dora_Gustafson/llAxes2Rot_right_and_wrong 1 Link to comment Share on other sites More sharing options...
Quistess Alpha Posted February 4 Share Posted February 4 11 hours ago, Jenna Huntsman said: Which should always point (+Y axis) at the user's camera while keeping the object's +Z axis aligned with the camera's UP axis couldn't you just take the camera's rotation and left-multiply by a 90 degree turn on the z-axis? rotation point_y_to_camera = <0, 0, 0.70711, 0.70711>*camera_rotation; Link to comment Share on other sites More sharing options...
Jenna Huntsman Posted February 4 Author Share Posted February 4 2 minutes ago, Quistess Alpha said: couldn't you just take the camera's rotation and left-multiply by a 90 degree turn on the z-axis? rotation point_y_to_camera = <0, 0, 0.70711, 0.70711>*camera_rotation; Not quite - I did try this at first, but because the object could be anywhere on screen, if it's at the edge of the screen it looks skewed due to perspective, so I implemented the above function to have the object always orient itself to look into the "lens", which resolves the skew issue. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now