home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Scripting_Resources / Samples / Automation_Scripts / 3Dbounce.js next >
Encoding:
Text File  |  2002-05-13  |  7.4 KB  |  227 lines

  1. /***************************************************************
  2. ADOBE SYSTEMS INCORPORATED 
  3. Copyright 2001 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. /***************************************************************
  14. Author: Mary Obelnicki
  15. ***************************************************************/
  16.  
  17. #include "../../Include/Camera.js"
  18. #include "../../Include/physics.js"
  19.  
  20. function bounce3D(bouncer, bounces, bounceratio, initialPosition, initialVelocity, ground, gravity, keyFrameRate, fps)
  21. /*
  22. bounces an object in 3D.
  23. bouncer: the object to bounce
  24. bounces: how many times to bounce the object
  25. bounceratio: the percentage of the previous bounce's height that the next bounce
  26.      will reach.
  27. initialPosition: a vector of the object's initial position in 3D
  28. initialVelocity: a vecotr of the object's initial velocity in 3D
  29. ground: the y value of the ground (postive y is down); 
  30. gravity: the value of gravity in pixels per second.
  31. keyFrameRate: how often to place keyframes. 
  32. fps: frames per second.  this argument should be removed once the composition 
  33.      can be abtained from the object.
  34.  */
  35. {
  36.  
  37.     var startFrame = bouncer.currentFrame;
  38.     var rebound = Math.sqrt(bounceratio); 
  39.     
  40.     var po = initialPosition.copy(); 
  41.     var vo = initialVelocity.copy(); 
  42.     var ao = new Vector(0, gravity, 0);
  43.     
  44.     var timetoground; 
  45.     var framestoground;
  46.     
  47.     //the perspective camera
  48.     var camera = new Camera(new Vector(application.currentComposition.size.x/2,
  49.                        ground - 50, 
  50.                        -1000)); 
  51.     
  52.     camera.perspectiveSetup(bouncer, true, 0); 
  53.     if(vo.z == 0)
  54.     bouncer.stopwatch.scale = false; //no dz, no need for scale
  55.     camera.perspectiveTransform(bouncer, po);
  56.     
  57.     for (i = 0; i <= bounces; i++) 
  58.     {
  59.     // time to next bounce
  60.     if ( po.y == ground) // quadratic formula is simplified when yo=ground
  61.         timetoground = -2*vo.y/ao.y;
  62.     else
  63.         timetoground = (-vo.y + Math.sqrt(vo.y*vo.y - (2 * ao.y * (po.y - ground))))/ao.y; 
  64.     
  65.     framestoground = Math.round(timetoground * fps); 
  66.     startFrame = bouncer.currentFrame;
  67.     var currentP; 
  68.     var df; 
  69.     for(df = 0; df < framestoground; df+=keyFrameRate)
  70.     {
  71.         bouncer.currentFrame = startFrame + df; 
  72.         dt = df/fps; 
  73.         currentP = newPosition(po, vo, ao, dt);
  74.         camera.perspectiveTransform(bouncer, currentP);
  75.     }
  76.         
  77.     //bounce frame
  78.     
  79.     bouncer.currentFrame = startFrame + framestoground; 
  80.     currentP = newPosition(po, vo, ao, timetoground); 
  81.     camera.perspectiveTransform(bouncer, currentP); 
  82.     
  83.     // calculate new initial conditions
  84.     
  85.     po = currentP; 
  86.     po.y = ground; // to make it exact
  87.     vo = newVelocity(vo, ao, timetoground); 
  88.     vo.y = vo.y*(-rebound); // neg for reflection off ground
  89.     }
  90. }
  91.  
  92. function threshbounce(bouncer, bounceratio, initialPosition, initialVelocity, ground, gravity, diffy, keyFrameRate, fps) 
  93. // estimates the number of bounces until there is less than diffy change in the 
  94. // y dimention, calls bounce with that number of bounces.
  95. // ydiff: the difference in y at which to stop bouncing, 
  96. {
  97.     var rebound = Math.sqrt(bounceratio); 
  98.     var yo = initialPosition.y; 
  99.     var bounces = Math.log(diffy/(ground-yo)) / Math.log(rebound); 
  100.     var bounces = Math.ceil(bounces); 
  101.     //function bounce3D(bouncer, bounces, bounceratio, initialPosition, initialVelocity, ground, gravity, keyFrameRate, fps)
  102.     bounce3D(bouncer, bounces, bounceratio, initialPosition, initialVelocity, ground, gravity, keyFrameRate, fps); 
  103. }
  104.  
  105. function reversebounce3D(bouncer, bounces, bounceratio, initialPosition, initialVelocity, ground, gravity, keyFrameRate, fps) 
  106. /*
  107. bounces an object backwards in time, in 3D.  So the end of the bounce will be the initialPosition and initialVelocity given
  108. as arguements.  The bounce will end at the position of the currrentFrame marker when the script is run. 
  109. bouncer: the object to bounce
  110. bounces: how many times to bounce the object
  111. bounceratio: the percentage of the previous bounce's height that the next bounce
  112.      will reach.
  113. initialPosition: a vector of the object's ending position in 3D
  114. initialVelocity: a vecotr of the object's ending velocity in 3D
  115. ground: the y value of the ground (postive y is down); 
  116. gravity: the value of gravity in pixels per second.
  117. keyFrameRate: how often to place keyframes. 
  118. fps: frames per second.  this argument should be removed once the composition 
  119.      can be abtained from the object.
  120.  */
  121. {
  122.     
  123.     var startFrame; 
  124.     var endFrame = bouncer.currentFrame; 
  125.     var rebound = Math.sqrt(bounceratio); 
  126.  
  127.     var pEnd = initialPosition.copy(); 
  128.     var vEnd = initialVelocity.copy(); 
  129.  
  130.     var ao = new Vector(0, gravity, 0);
  131.  
  132.     var refV = Vector.scalerMult(vEnd, -1); 
  133.     
  134.     var po; 
  135.     var vo;
  136.  
  137.     var timetoground; 
  138.     var framestoground;
  139.     
  140.     //the perspective camera
  141.     var camera = new Camera(new Vector(application.currentComposition.size.x/2,
  142.                        ground - 50, 
  143.                        -1000)); 
  144.     
  145.     camera.perspectiveSetup(bouncer, true, 0); 
  146.     if(initialVelocity.z == 0)
  147.     bouncer.stopwatch.scale = false; 
  148.     camera.perspectiveTransform(bouncer, pEnd); 
  149.     for (i = 0; i < bounces; i++) 
  150.     {
  151.     // time to next bounce
  152.     if ( pEnd.y == ground ) // quadratic formula is simplified when yo=ground
  153.         timetoground = -2*refV.y/ao.y;
  154.     else
  155.         timetoground = (-refV.y + Math.sqrt(refV.y*refV.y - (2 * ao.y * (pEnd.y - ground))))/ao.y; 
  156.     
  157.     framestoground = Math.round(timetoground * fps); 
  158.     startFrame = endFrame - framestoground; 
  159.  
  160.     var currentP; 
  161.     var df; 
  162.     var po = previousPosition(pEnd, vEnd, ao, timetoground);
  163.     po.y = ground; 
  164.     var vo = previousVelocity(vEnd, ao, timetoground); 
  165.     for(df = 0; df < framestoground; df+=keyFrameRate)
  166.     {
  167.         bouncer.currentFrame = startFrame + df; 
  168.         dt = df/fps; 
  169.         currentP = newPosition(po, vo, ao, dt);
  170.         camera.perspectiveTransform(bouncer, currentP);
  171.     }
  172.  
  173.     pEnd = po; 
  174.     vEnd = vo; 
  175.     vEnd.y = vEnd.y/(-rebound); 
  176.     refV = Vector.scalerMult(vEnd, -1); 
  177.     endFrame = startFrame; 
  178.  
  179.     }
  180.  
  181.     // the half parabola
  182.     var timetotop
  183.     if ( pEnd.y == ground) // quadratic formula is simplified when yEnd=ground
  184.     timetotop = -refV.y/ao.y;
  185.     else
  186.     timetotop = (-refV.y + Math.sqrt(refV.y*refV.y - 2*ao.y*(pEnd.y - ground)))/ao.y/2; 
  187.     framestotop = Math.round(timetotop * fps); 
  188.     startFrame = endFrame - framestotop; 
  189.     if(bouncer.startFrame > startFrame )
  190.     bouncer.startFrame = startFrame; 
  191.     
  192.     var currentP; 
  193.     var df; 
  194.     var po = previousPosition(pEnd, vEnd, ao, timetotop);
  195.     var vo = previousVelocity(vEnd, ao, timetotop); 
  196.     for(df = 0; df < framestotop; df+=keyFrameRate)
  197.     {
  198.     bouncer.currentFrame = startFrame + df; 
  199.     dt = df/fps; 
  200.     currentP = newPosition(po, vo, ao, dt);
  201.     camera.perspectiveTransform(bouncer, currentP);
  202.     }
  203.     
  204. }
  205.  
  206. comp = application.currentComposition;
  207. objects = comp.selection;
  208.  
  209.  
  210.  
  211. bounce3D(objects[0], 3, .6, new Vector(100, 100, 0), new Vector(50, 0, 0), 350, 500, 3, comp.framesPerSecond);
  212.  
  213. //reversebounce3D(objects[0], 2, .6, new Vector(500, 350, 100), new Vector(50, 400, 0), 350, 1000, 1, comp.framesPerSecond);
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.