Posted By: EvilSOB
Camera rotation around a fixed (adjustable) point - 08/12/08 05:25
I have seen this question many times across different forums,
and this piece of code has taken many of the suggestions into itself.
This has made it fairly robust and very flexible.
I think the built-in documentation should be sufficient.
If anyone has any questions feel free to ask,
and if you have any suggestions for improvements (that dont cripple flexibility)
also PLEASE feel free.
I'll be buck.....
[EDIT] THE NEWLY RELEASED (only slightly tested) C-SCRIPT Version
and this piece of code has taken many of the suggestions into itself.
This has made it fairly robust and very flexible.
I think the built-in documentation should be sufficient.
If anyone has any questions feel free to ask,
and if you have any suggestions for improvements (that dont cripple flexibility)
also PLEASE feel free.
I'll be buck.....
Code:
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Complete Camera Management function // VECTOR* CamTarget = { x=0; y=0; z=0; } //Globally accesable Target of camera VECTOR* CamOffset = { x=0; y=0; z=0; } //Optionally global vector for the X,Y,Z Offset of Focus. ie (0,0,0) = dead-on CamTarget var CamZoomFactor = 50; //Globally accesable ZOOM setting of camera // // function Camera_Driver_startup() { while(1) { //Optionally place limitations on available angles/positions camera.pan = clamp(camera.pan, -175, 175); //Limit possible Pan angle to keep Camera from Panning completely around camera.tilt = clamp(camera.tilt, -90, 0); //Limit possible Tilt angle to keep Camera "above" play-field camera.roll = clamp(camera.roll, 0, 0); //Limit possible Roll angle to disabled // //Position and Focus Camera camera.x = CamTarget.x + CamOffset.x - CamZoomFactor * cos(camera.tilt) * cos(camera.pan); camera.y = CamTarget.y + CamOffset.y - CamZoomFactor * cos(camera.tilt) * sin(camera.pan); camera.z = CamTarget.z + CamOffset.z - CamZoomFactor * sin(camera.tilt); // // proc_mode = PROC_LATE; wait(1); } } // // function Example_Camera_Controls_startup() { VECTOR* Offset = vector(0,0,0); while(1) { //Rotate/Zoom Camera if(key_pressed(280)) camera.pan -= mickey.x/5; // mouse movement left-right changes PAN (when LeftClick held) if(key_pressed(280)) camera.tilt -= mickey.y/5; // mouse movement up-down changes TILT (when LeftClick held) CamZoomFactor += mickey.z / 25; // mouse Scroller up-down changes ZOOM (Anytime) // //Change Positional Offset of Focus away from Target. ie (0,0,0) = dead-on target if(key_pressed(72)) CamOffset.x += time_step * CamZoomFactor / 25; // Up Arrow Adjust 'North' if(key_pressed(80)) CamOffset.x -= time_step * CamZoomFactor / 25; // Down Arrow Adjust 'South' if(key_pressed(75)) CamOffset.y += time_step * CamZoomFactor / 25; // Left Arrow Adjust 'East' if(key_pressed(77)) CamOffset.y -= time_step * CamZoomFactor / 25; // Right Arrow Adjust 'West' if(key_pressed(71)) CamOffset.z += time_step * CamZoomFactor / 25; // Home Key Adjust Camera Height 'Up' if(key_pressed(79)) CamOffset.z -= time_step * CamZoomFactor / 25; // End Key Adjust Camera Height 'Down' // // proc_mode = PROC_EARLY; wait(1); } } // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[EDIT] THE NEWLY RELEASED (only slightly tested) C-SCRIPT Version
Code:
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Complete Camera Management function - C-SCRIPT VERSION !! // var CamTarget[3] = 0,0,0; //Globally accesable Target of camera var CamOffset[3] = 0,0,0; //Optionally global vector for the X,Y,Z Offset of Focus. ie (0,0,0) = dead-on CamTarget var CamZoomFactor = 50; //Globally accesable ZOOM setting of camera // // starter Camera_Driver() { while(1) { //Optionally place limitations on available angles/positions camera.pan = clamp(camera.pan, -175, 175); //Limit possible Pan angle to keep Camera from Panning completely around camera.tilt = clamp(camera.tilt, -90, 0); //Limit possible Tilt angle to keep Camera "above" play-field camera.roll = clamp(camera.roll, 0, 0); //Limit possible Roll angle to disabled // //Position and Focus Camera camera.x = CamTarget.x + CamOffset.x - CamZoomFactor * cos(camera.tilt) * cos(camera.pan); camera.y = CamTarget.y + CamOffset.y - CamZoomFactor * cos(camera.tilt) * sin(camera.pan); camera.z = CamTarget.z + CamOffset.z - CamZoomFactor * sin(camera.tilt); // // proc_late(); wait(1); } } // // starter Example_Camera_Controls() { var Offset[3] = 0,0,0; while(1) { //Rotate/Zoom Camera if(key_pressed(280)) { camera.pan -= mickey.x/5; } // mouse movement left-right changes PAN (when LeftClick held) if(key_pressed(280)) { camera.tilt -= mickey.y/5; } // mouse movement up-down changes TILT (when LeftClick held) CamZoomFactor += mickey.z / 25; // mouse Scroller up-down changes ZOOM (Anytime) // //Change Positional Offset of Focus away from Target. ie (0,0,0) = dead-on target if(key_pressed(72)) { CamOffset.x += time_step * CamZoomFactor / 25; } // Up Arrow Adjust 'North' if(key_pressed(80)) { CamOffset.x -= time_step * CamZoomFactor / 25; } // Down Arrow Adjust 'South' if(key_pressed(75)) { CamOffset.y += time_step * CamZoomFactor / 25; } // Left Arrow Adjust 'East' if(key_pressed(77)) { CamOffset.y -= time_step * CamZoomFactor / 25; } // Right Arrow Adjust 'West' if(key_pressed(71)) { CamOffset.z += time_step * CamZoomFactor / 25; } // Home Key Adjust Camera Height 'Up' if(key_pressed(79)) { CamOffset.z -= time_step * CamZoomFactor / 25; } // End Key Adjust Camera Height 'Down' // // wait(1); } } // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////