turt2live

Untitled

Mar 6th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using GDIDrawer;
  6. using System.Threading;
  7. using System.Drawing;
  8.  
  9. namespace CNT1300TravisRalstonAssignment12
  10. {
  11.     class ProjectileMotion
  12.     {
  13.  
  14.         static CDrawer screen = new CDrawer();
  15.         static Random rand = new Random();
  16.         static double gravity = 9.81;
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             bool stopped = false;
  21.             double velocity = 75.0;
  22.             int radius = 5;
  23.             double degrees = 60;
  24.             double radians = ToRadians(degrees);
  25.             double touchGroundLoss = 0.05;
  26.             double bouncePercRetain = 0.9;
  27.             int tickTime = 10; // Milliseconds
  28.             int timePerTick = 50; // Milliseconds
  29.  
  30.             int time = 1;
  31.             double Vx = 0, Vy = 0, Vo = velocity;
  32.            
  33.             Color color = RandomColor();
  34.             int iterations = 0;
  35.  
  36.             double tan = Math.Tan(radians);
  37.             double g = gravity/(2*Vo*Vo*CosSquared(radians));
  38.             int x = 0;
  39.             int displayX = 0;
  40.             int xShift = 0;
  41.             int lastX = 0;
  42.             bool reverse = false;
  43.             while (!stopped)
  44.             {
  45.                 // Anti-OHGODWHY
  46.                 iterations++;
  47.                 if (iterations > 1100)
  48.                 {
  49.                     break;
  50.                 }
  51.  
  52.                 // Math
  53.                 displayX = reverse ? --lastX:++lastX;
  54.                 x++;
  55.                 int xCalc = x - xShift;
  56.                 Vx = Vo * Math.Cos(radians);
  57.                 Vy = (Vo * Math.Sin(radians)) - (gravity * time);
  58.  
  59.                 // New Math
  60.                 int y = (int)((tan * xCalc) - (g * xCalc * xCalc));
  61.  
  62.                 Console.WriteLine("({0}, {1}) Vx={2}  Vy={3}  Vo={4}  Xcalc={5}", x, y, Vx, Vy, Vo, xCalc);
  63.  
  64.                 // Corrections
  65.                 y = screen.m_ciHeight - y;
  66.  
  67.                 // Physics
  68.                 bool bounced = false;
  69.                 if (y >= screen.m_ciHeight)
  70.                 {
  71.                     bounced = true;
  72.                     Vy = Vy * bouncePercRetain;
  73.                     xShift = x;
  74.                 }
  75.                 if (y == screen.m_ciHeight)
  76.                 {
  77.                     bounced = true;
  78.                     Vx -= touchGroundLoss;
  79.                 }
  80.                 if (displayX > screen.m_ciWidth || displayX < 0)
  81.                 {
  82.                     reverse = !reverse;
  83.                     color = RandomColor();
  84.                 }
  85.  
  86.                 // Recalc Vo
  87.                 if (bounced)
  88.                 {
  89.                     Vo = Math.Sqrt((Vy * Vy) + (Vx * Vx));
  90.                     g = gravity / (2 * Vo * Vo * CosSquared(radians));
  91.                 }
  92.  
  93.                 // Draw
  94.                 screen.AddCenteredEllipse(displayX, y, radius * 2, radius * 2, color);
  95.  
  96.                 // Misc
  97.                 stopped = IsStopped(Vx, Vy, y, time);
  98.                 time += timePerTick;
  99.                 Thread.Sleep(tickTime);
  100.             }
  101.             Console.ReadKey(); // TODO: Remove if needed
  102.         }
  103.  
  104.         static double CosSquared(double radians)
  105.         {
  106.             double cosine = Math.Cos(radians);
  107.             return cosine * cosine;
  108.         }
  109.  
  110.         static double SinSquared(double radians)
  111.         {
  112.             double sine = Math.Sin(radians);
  113.             return sine * sine;
  114.         }
  115.  
  116.         static double ToRadians(double degrees)
  117.         {
  118.             return Math.PI * degrees / 180.0;
  119.         }
  120.  
  121.         static bool IsStopped(double horizontalVelocity, double verticalVelocity, int yPosition, int time)
  122.         {
  123.             if (yPosition >= screen.m_ciHeight - 1 && time != 0)
  124.             {
  125.                 double roundedHV = Math.Round(horizontalVelocity, 1);
  126.                 double roundedVV = Math.Round(verticalVelocity, 0);
  127.                 if (roundedHV == 0.0 && roundedVV == 0.0)
  128.                 {
  129.                     return true;
  130.                 }
  131.             }
  132.             return false;
  133.         }
  134.  
  135.         static Color RandomColor()
  136.         {
  137.             int r = rand.Next(0, 256);
  138.             int g = rand.Next(0, 256);
  139.             int b = rand.Next(0, 256);
  140.             return Color.FromArgb(r, g, b);
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment