home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************
- ADOBE SYSTEMS INCORPORATED
- Copyright 2001 Adobe Systems Incorporated
- All Rights Reserved
-
- NOTICE: Adobe permits you to use, modify, and distribute this
- file in accordance with the terms of the Adobe license agreement
- accompanying it. If you have received this file from a source
- other than Adobe, then your use, modification, or distribution
- of it requires the prior written permission of Adobe.
- ***************************************************************/
-
- /***************************************************************
- Author: Mary Obelnicki
- ***************************************************************/
-
- #include "../../Include/Camera.js"
- #include "../../Include/physics.js"
-
- function bounce3D(bouncer, bounces, bounceratio, initialPosition, initialVelocity, ground, gravity, keyFrameRate, fps)
- /*
- bounces an object in 3D.
- bouncer: the object to bounce
- bounces: how many times to bounce the object
- bounceratio: the percentage of the previous bounce's height that the next bounce
- will reach.
- initialPosition: a vector of the object's initial position in 3D
- initialVelocity: a vecotr of the object's initial velocity in 3D
- ground: the y value of the ground (postive y is down);
- gravity: the value of gravity in pixels per second.
- keyFrameRate: how often to place keyframes.
- fps: frames per second. this argument should be removed once the composition
- can be abtained from the object.
- */
- {
-
- var startFrame = bouncer.currentFrame;
- var rebound = Math.sqrt(bounceratio);
-
- var po = initialPosition.copy();
- var vo = initialVelocity.copy();
- var ao = new Vector(0, gravity, 0);
-
- var timetoground;
- var framestoground;
-
- //the perspective camera
- var camera = new Camera(new Vector(application.currentComposition.size.x/2,
- ground - 50,
- -1000));
-
- camera.perspectiveSetup(bouncer, true, 0);
- if(vo.z == 0)
- bouncer.stopwatch.scale = false; //no dz, no need for scale
- camera.perspectiveTransform(bouncer, po);
-
- for (i = 0; i <= bounces; i++)
- {
- // time to next bounce
- if ( po.y == ground) // quadratic formula is simplified when yo=ground
- timetoground = -2*vo.y/ao.y;
- else
- timetoground = (-vo.y + Math.sqrt(vo.y*vo.y - (2 * ao.y * (po.y - ground))))/ao.y;
-
- framestoground = Math.round(timetoground * fps);
- startFrame = bouncer.currentFrame;
- var currentP;
- var df;
- for(df = 0; df < framestoground; df+=keyFrameRate)
- {
- bouncer.currentFrame = startFrame + df;
- dt = df/fps;
- currentP = newPosition(po, vo, ao, dt);
- camera.perspectiveTransform(bouncer, currentP);
- }
-
- //bounce frame
-
- bouncer.currentFrame = startFrame + framestoground;
- currentP = newPosition(po, vo, ao, timetoground);
- camera.perspectiveTransform(bouncer, currentP);
-
- // calculate new initial conditions
-
- po = currentP;
- po.y = ground; // to make it exact
- vo = newVelocity(vo, ao, timetoground);
- vo.y = vo.y*(-rebound); // neg for reflection off ground
- }
- }
-
- function threshbounce(bouncer, bounceratio, initialPosition, initialVelocity, ground, gravity, diffy, keyFrameRate, fps)
- // estimates the number of bounces until there is less than diffy change in the
- // y dimention, calls bounce with that number of bounces.
- // ydiff: the difference in y at which to stop bouncing,
- {
- var rebound = Math.sqrt(bounceratio);
- var yo = initialPosition.y;
- var bounces = Math.log(diffy/(ground-yo)) / Math.log(rebound);
- var bounces = Math.ceil(bounces);
- //function bounce3D(bouncer, bounces, bounceratio, initialPosition, initialVelocity, ground, gravity, keyFrameRate, fps)
- bounce3D(bouncer, bounces, bounceratio, initialPosition, initialVelocity, ground, gravity, keyFrameRate, fps);
- }
-
- function reversebounce3D(bouncer, bounces, bounceratio, initialPosition, initialVelocity, ground, gravity, keyFrameRate, fps)
- /*
- bounces an object backwards in time, in 3D. So the end of the bounce will be the initialPosition and initialVelocity given
- as arguements. The bounce will end at the position of the currrentFrame marker when the script is run.
- bouncer: the object to bounce
- bounces: how many times to bounce the object
- bounceratio: the percentage of the previous bounce's height that the next bounce
- will reach.
- initialPosition: a vector of the object's ending position in 3D
- initialVelocity: a vecotr of the object's ending velocity in 3D
- ground: the y value of the ground (postive y is down);
- gravity: the value of gravity in pixels per second.
- keyFrameRate: how often to place keyframes.
- fps: frames per second. this argument should be removed once the composition
- can be abtained from the object.
- */
- {
-
- var startFrame;
- var endFrame = bouncer.currentFrame;
- var rebound = Math.sqrt(bounceratio);
-
- var pEnd = initialPosition.copy();
- var vEnd = initialVelocity.copy();
-
- var ao = new Vector(0, gravity, 0);
-
- var refV = Vector.scalerMult(vEnd, -1);
-
- var po;
- var vo;
-
- var timetoground;
- var framestoground;
-
- //the perspective camera
- var camera = new Camera(new Vector(application.currentComposition.size.x/2,
- ground - 50,
- -1000));
-
- camera.perspectiveSetup(bouncer, true, 0);
- if(initialVelocity.z == 0)
- bouncer.stopwatch.scale = false;
- camera.perspectiveTransform(bouncer, pEnd);
- for (i = 0; i < bounces; i++)
- {
- // time to next bounce
- if ( pEnd.y == ground ) // quadratic formula is simplified when yo=ground
- timetoground = -2*refV.y/ao.y;
- else
- timetoground = (-refV.y + Math.sqrt(refV.y*refV.y - (2 * ao.y * (pEnd.y - ground))))/ao.y;
-
- framestoground = Math.round(timetoground * fps);
- startFrame = endFrame - framestoground;
-
- var currentP;
- var df;
- var po = previousPosition(pEnd, vEnd, ao, timetoground);
- po.y = ground;
- var vo = previousVelocity(vEnd, ao, timetoground);
- for(df = 0; df < framestoground; df+=keyFrameRate)
- {
- bouncer.currentFrame = startFrame + df;
- dt = df/fps;
- currentP = newPosition(po, vo, ao, dt);
- camera.perspectiveTransform(bouncer, currentP);
- }
-
- pEnd = po;
- vEnd = vo;
- vEnd.y = vEnd.y/(-rebound);
- refV = Vector.scalerMult(vEnd, -1);
- endFrame = startFrame;
-
- }
-
- // the half parabola
- var timetotop
- if ( pEnd.y == ground) // quadratic formula is simplified when yEnd=ground
- timetotop = -refV.y/ao.y;
- else
- timetotop = (-refV.y + Math.sqrt(refV.y*refV.y - 2*ao.y*(pEnd.y - ground)))/ao.y/2;
- framestotop = Math.round(timetotop * fps);
- startFrame = endFrame - framestotop;
- if(bouncer.startFrame > startFrame )
- bouncer.startFrame = startFrame;
-
- var currentP;
- var df;
- var po = previousPosition(pEnd, vEnd, ao, timetotop);
- var vo = previousVelocity(vEnd, ao, timetotop);
- for(df = 0; df < framestotop; df+=keyFrameRate)
- {
- bouncer.currentFrame = startFrame + df;
- dt = df/fps;
- currentP = newPosition(po, vo, ao, dt);
- camera.perspectiveTransform(bouncer, currentP);
- }
-
- }
-
- comp = application.currentComposition;
- objects = comp.selection;
-
-
-
- bounce3D(objects[0], 3, .6, new Vector(100, 100, 0), new Vector(50, 0, 0), 350, 500, 3, comp.framesPerSecond);
-
- //reversebounce3D(objects[0], 2, .6, new Vector(500, 350, 100), new Vector(50, 400, 0), 350, 1000, 1, comp.framesPerSecond);
-
-
-
-
-
-
-
-
-
-
-
-
-
-