/* * Virtual Playhead - Timed Item * http://subpixels.com * Version 0.2, 2009-01-29 */ // ----------------------------------------------------------------------------- // All of the timed events have a start time and an end time // We can work out if an item is active based on these and the current time // Knowing the duration is also handy for determining the rate of change for // parameters (such as size of colour) from start to end. // ----------------------------------------------------------------------------- class TimedItem { public float t1; // Start time public float t2; // End time public float duration; // Length of apearance // Position float x1; float x2; float y1; float y2; // Rotation float rot1; float rot2; // Estimate for the rate of movement (in pixels per second). // Used to optimise the number of redraws needed for effective motion blur. public float motionEstimate; // --------------------------------------------------------------------------- // Constructors // --------------------------------------------------------------------------- TimedItem(float t1, float t2) { this(t1, t2, 0, 0, 0, 0, 0, 0); } TimedItem(float t1, float t2, float x1, float x2, float y1, float y2, float rot1, float rot2) { this.t1 = t1; this.t2 = t2; duration = t2 - t1; this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; this.rot1 = rot1; this.rot2 = rot2; motionEstimate = 0; // This should be upgraded by subclasses that move! } // Test to see if the item is active at a moment public boolean active(float time) { return t1 <= time && time <= t2; } // Test to see if the item is active during some period // (from time to time + delta). public boolean active(float time, float delta) { return (t1 <= time + delta) && (time <= t2); } // Calculate the motion estimate, using some item "radius", supposing the // item rotates about a centre with that maximum radius. public float calcMotionEstimate(float itemRadius) { // Make an estimate about the rate of movement of the item text // Take into account size and rotation // // Speed due to linear translation is the total distance divided by duration // transSpeed = dist(x1, y1, x2, y2) / duration; // // The angular velocity, in radians per second // rotSpeed = (rot2 - rot1) / duration; // As a portion of a full circle revolution? // circleFrac = rotSpeed / TWO_PI // And one revolution is distance // circumference = TWO_PI * r // The number of rotations: // rotations = abs(rot2 - rot1) / TWO_PI; // The length of one rotation: // circumference = TWO_PI * r // Total length of rotations // totalRotation = (TWO_PI * r) * ( abs(rot2 - rot1) / TWO_PI ) // = abs(rot2 - rot1) * r // Divide by the duration to get the speed // rotSpeed = abs(rot2 - rot1) * r / duration // // Add the linear and rotational motion together for a gross estimate // motionEstimate = transSpeed + rotSpeed; return (dist(x1, y1, x2, y2) + abs(rot2 - rot1) * itemRadius) / duration; } }