home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Include / Camera.js < prev    next >
Encoding:
Text File  |  2002-05-13  |  4.7 KB  |  139 lines

  1. /***************************************************************
  2. ADOBE SYSTEMS INCORPORATED 
  3. Copyright 2002 Adobe Systems Incorporated 
  4. All Rights Reserved 
  5.  
  6. NOTICE:  Adobe permits you to use, modify, and distribute this 
  7. file in accordance with the terms of the Adobe license agreement 
  8. accompanying it.  If you have received this file from a source 
  9. other than Adobe, then your use, modification, or distribution
  10. of it requires the prior written permission of Adobe. 
  11. ***************************************************************/
  12. /***************************************************************
  13. Author: Mary Obelnicki
  14. ***************************************************************/
  15.  
  16. /***************************************************************
  17.  
  18. This function creates the illusion of a simple camera with no 
  19. concept of clipping.  It sets up a pseudo-3d perception of 2-d
  20. objects. Object disapear behind the eye because the math works 
  21. out to set their scales to negative numbers, which actually sets 
  22. the scale to 0. The view plan is always at z=0;
  23.  
  24. Functions:
  25.  
  26. Camera.perspectiveSetup(object, forAnimation, oriZ)
  27.     This function sets up an object to have perspective transforms 
  28.     applied to it.  Basicly saving the scale of the object when z=0; 
  29.     this function does not perform the perspective transformation 
  30.     on the object. 
  31.     
  32.     Arguments:
  33.         <object> the object to setup. 
  34.         <forAnimation> boolean. 
  35.             true: perspective for animation, turn on relavant 
  36.                 stopwatches
  37.             false: no stopwatch changes. You can still
  38.                 set stop watches outside this function.
  39.         <oriZ> the z coordinate for the objects current location, 
  40.             so that all objects do not have to start out at z=0. 
  41.             This is an optional argument.  The assumed oriZ is 0.
  42.  
  43. Camera.perspectiveTransform(object, pos3d)
  44.     This changes the position and scale of <object> to reflect 
  45.     a perspective transform of <object> from <position3d> to the 
  46.     view plane of the camera. 
  47.     
  48.     Arguments:
  49.         <object> the object to be transformed
  50.         <pos3d> a vector for the new 3d position of the object
  51.  
  52. ***************************************************************/
  53.  
  54. /***************************************************************
  55. DO NOT EDIT BELOW THIS LINE
  56. ***************************************************************/
  57.  
  58.  
  59. #include "Vector.js"
  60.  
  61. function Camera(inEye)
  62. //inEye: a vector to the position of the eye
  63. {
  64.     this.eye = inEye.copy(); 
  65.     this.zs = 0; 
  66. }
  67.  
  68. new Camera(new Vector(0, 0, 0));
  69.  
  70.  
  71. Camera.prototype.perspectiveSetup  = 
  72.     function(object, forAnimation, oriZ)
  73.     //a function that sets up an object to have perspective transforms 
  74.     //applied to it.  Basicly saving the scale of the object when z = 0; 
  75.     //this function does not do the perspective transformation on the 
  76.     //object. 
  77.     
  78.     //object: the object to setup. 
  79.     //forAnimation: a bool, true: perspective for animation, turn on relavant stopwatches
  80.     //              false: no stopwatch changes. A convenience, you can set stop watches 
  81.     //              out side this function.
  82.     //oriZ: the z coordinate for the objects current location, so that 
  83.     //      all objects do not have to start out at z = 0. If unprovied, 
  84.     //      oriZ = 0; 
  85.     {
  86.     if(forAnimation)
  87.     {
  88.         object.stopwatch.position = true; 
  89.         object.stopwatch.scale = true; 
  90.     }
  91.     
  92.     // test for the optional arguement
  93.     if(arguments.length == 3)
  94.     {
  95.         var pScale = (oriZ-this.eye.z)/(this.zs-this.eye.z);
  96.     }
  97.     else
  98.     {
  99.         var pScale = 1; 
  100.     }
  101.     object.oriScale = new Object(); 
  102.     object.oriScale.x = pScale*(object.scale.x); 
  103.     object.oriScale.y = pScale*(object.scale.y); 
  104.     }
  105.  
  106.  
  107. Camera.prototype.perspectiveTransform = 
  108.     function(object, pos3d)
  109.     //changes the position and scale of <object> to reflect a perspective 
  110.     //transform of <object> from <position3d> to the view plane of the 
  111.     //camera. 
  112.     //object: the object to be transformed
  113.     //pos3d: a vector for the new 3d position of the object
  114.     {
  115.     if(pos3d.z > this.eye.z) // the object is in front of the eye
  116.     {
  117.         var xs = this.eye.x + ((pos3d.x-this.eye.x)*(this.zs-this.eye.z)/(pos3d.z-this.eye.z));  
  118.         var ys = this.eye.y + ((pos3d.y-this.eye.y)*(this.zs-this.eye.z)/(pos3d.z-this.eye.z)); 
  119.         
  120.         var pScale = (this.zs-this.eye.z)/(pos3d.z-this.eye.z); 
  121.         
  122.         //into screen coordinates
  123.         object.scale.x = object.oriScale.x * pScale; 
  124.         object.scale.y = object.oriScale.y * pScale; 
  125.         object.position.x = xs; 
  126.         object.position.y = ys;
  127.     }
  128.     else // the object is behind the eye... this should do something more than avoid the question.
  129.     {
  130.  
  131.     }
  132.  
  133.     
  134.     } 
  135.  
  136.  
  137.  
  138.  
  139.