Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using GDIDrawer;
- using System.Threading;
- using System.Drawing;
- namespace CNT1300TravisRalstonAssignment12
- {
- class ProjectileMotion
- {
- static CDrawer screen = new CDrawer();
- static Random rand = new Random();
- static double gravity = 9.81;
- static void Main(string[] args)
- {
- bool stopped = false;
- double velocity = 75.0;
- int radius = 5;
- double degrees = 60;
- double radians = ToRadians(degrees);
- double touchGroundLoss = 0.05;
- double bouncePercRetain = 0.9;
- int tickTime = 10; // Milliseconds
- int timePerTick = 50; // Milliseconds
- int time = 1;
- double Vx = 0, Vy = 0, Vo = velocity;
- Color color = RandomColor();
- int iterations = 0;
- double tan = Math.Tan(radians);
- double g = gravity/(2*Vo*Vo*CosSquared(radians));
- int x = 0;
- int displayX = 0;
- int xShift = 0;
- int lastX = 0;
- bool reverse = false;
- while (!stopped)
- {
- // Anti-OHGODWHY
- iterations++;
- if (iterations > 1100)
- {
- break;
- }
- // Math
- displayX = reverse ? --lastX:++lastX;
- x++;
- int xCalc = x - xShift;
- Vx = Vo * Math.Cos(radians);
- Vy = (Vo * Math.Sin(radians)) - (gravity * time);
- // New Math
- int y = (int)((tan * xCalc) - (g * xCalc * xCalc));
- Console.WriteLine("({0}, {1}) Vx={2} Vy={3} Vo={4} Xcalc={5}", x, y, Vx, Vy, Vo, xCalc);
- // Corrections
- y = screen.m_ciHeight - y;
- // Physics
- bool bounced = false;
- if (y >= screen.m_ciHeight)
- {
- bounced = true;
- Vy = Vy * bouncePercRetain;
- xShift = x;
- }
- if (y == screen.m_ciHeight)
- {
- bounced = true;
- Vx -= touchGroundLoss;
- }
- if (displayX > screen.m_ciWidth || displayX < 0)
- {
- reverse = !reverse;
- color = RandomColor();
- }
- // Recalc Vo
- if (bounced)
- {
- Vo = Math.Sqrt((Vy * Vy) + (Vx * Vx));
- g = gravity / (2 * Vo * Vo * CosSquared(radians));
- }
- // Draw
- screen.AddCenteredEllipse(displayX, y, radius * 2, radius * 2, color);
- // Misc
- stopped = IsStopped(Vx, Vy, y, time);
- time += timePerTick;
- Thread.Sleep(tickTime);
- }
- Console.ReadKey(); // TODO: Remove if needed
- }
- static double CosSquared(double radians)
- {
- double cosine = Math.Cos(radians);
- return cosine * cosine;
- }
- static double SinSquared(double radians)
- {
- double sine = Math.Sin(radians);
- return sine * sine;
- }
- static double ToRadians(double degrees)
- {
- return Math.PI * degrees / 180.0;
- }
- static bool IsStopped(double horizontalVelocity, double verticalVelocity, int yPosition, int time)
- {
- if (yPosition >= screen.m_ciHeight - 1 && time != 0)
- {
- double roundedHV = Math.Round(horizontalVelocity, 1);
- double roundedVV = Math.Round(verticalVelocity, 0);
- if (roundedHV == 0.0 && roundedVV == 0.0)
- {
- return true;
- }
- }
- return false;
- }
- static Color RandomColor()
- {
- int r = rand.Next(0, 256);
- int g = rand.Next(0, 256);
- int b = rand.Next(0, 256);
- return Color.FromArgb(r, g, b);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment