// Move the moving disk to this location. private void AnimateMovement(int end_x, int end_y) { int start_x = MovingDiskRect.X; int start_y = MovingDiskRect.Y;
const int pixels_per_second = 400; float dx = end_x - MovingDiskRect.X; float dy = end_y - MovingDiskRect.Y; float dist = (float)Math.Sqrt(dx * dx + dy * dy);
// Calculate distance moved per second. dx = pixels_per_second * dx / dist; dy = pixels_per_second * dy / dist;
// See how long the total move will take. float seconds = dist / pixels_per_second; DateTime start_time = DateTime.Now;
The code calculates the number of pixels per second it should add to the moving rectangle's X and Y coordinates. It then enters a loop where it adds the needed number of pixels, depending on how much time has elapsed.
Download the example for additional details.
Comments