Camera rotation around a fixed (adjustable) point

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. smirk
This has made it fairly robust and very flexible. cool

I think the built-in documentation should be sufficient. grin

If anyone has any questions feel free to ask, blush
and if you have any suggestions for improvements (that dont cripple flexibility)
also PLEASE feel free. smile

I'll be buck..... cool


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);         
   }
}
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Posted By: kwpsp

Re: Camera rotation around a fixed (adjustable) point - 06/12/09 05:59

I test it in C-Script, it doesn't work, is there a C-Script version=?
thx
kwpsp
Posted By: EvilSOB

Re: Camera rotation around a fixed (adjustable) point - 06/12/09 06:17

Just added a compiled but not live tested c-script version, please test for me wink
Posted By: kwpsp

Re: Camera rotation around a fixed (adjustable) point - 06/12/09 06:25

:-(
< vec_fill(Offset[0],0)>
WORLD.wdl 95:0 Bad or missing parameter unknown function

It's great that you're supporting your posts ;-)
__________________________________________________
Besides I have a presentation on Monday about Australia,
maybe you could help me?
Posted By: EvilSOB

Re: Camera rotation around a fixed (adjustable) point - 06/12/09 06:42

You mas be using OLD A7, or even A6. Hard for me to test.
Try changing that whole line to
var Offset[0] = 0,0,0;

Im at work now, so Ive only got a couple of hours left today, but I'll help
with your presentation if I can....
Im no great patriot, and its been 22 years since school...
So nothing TOO hard, please.
© 2024 lite-C Forums