Advertisement
mixster

mixster

Oct 4th, 2009
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.92 KB | None | 0 0
  1. const
  2.   init_force = 100; // Initial acceleration - ms^-2 (same as force in newtons assuming 1 kg projectile)
  3.   init_angle = 40; // Initial angle in degrees, with horizontal being 0 and vertical being 90
  4.   grav = 9.81; // Gravity, x ms^-2
  5.   wind = -10; // Wind, x ms^-2 - positive increases distance and vice versa
  6.   res = 10; // Resistance, x ms^-2
  7.   fps = 64.0;
  8.  
  9. var
  10.   m, r_for, x_for, y_for, cur_ang, x_pos, y_pos: Extended; // resultant force; x and y components of resultant force; current angle of projectile
  11.   i: Integer; // Timer
  12.   arc: TPointArray;
  13.   tb: TBox;
  14.  
  15. begin
  16.   r_for := init_force;
  17.   x_pos := 0;
  18.   y_pos := 0;
  19.   cur_ang := Radians(init_angle);
  20.   i := 0;
  21.   repeat
  22.     SetLength(arc, i + 1);
  23.     arc[i] := Point(Round(x_pos), Round(y_pos));
  24.    
  25.     r_for := r_for - (res / fps);
  26.  
  27.     x_for := (r_for * Cos(cur_ang)) - (wind / fps);
  28.     y_for := (r_for * Sin(cur_ang)) - (grav / fps);
  29.     cur_ang := ArcTan(y_for / x_for);
  30.     r_for := x_for / Cos(cur_ang);
  31.  
  32.     x_pos := x_pos + (x_for / fps);
  33.     y_pos := y_pos + (y_for / fps);
  34.    
  35.     i := i + 1;
  36.   until y_pos <= 0;
  37.  
  38.   Writeln('Flight took ~' + IntToStr(Round(i / fps)) + 's');
  39.  
  40.   SetLength(arc, i + 1);
  41.   arc[i] := Point(Round(x_pos), Round(y_pos));
  42.  
  43.   if arc[i].y <> 0 then
  44.   begin
  45.     if arc[i].x = arc[i - 1].x then
  46.       arc[i].y := 0
  47.     else
  48.     begin
  49.       m := (arc[i].y - arc[i - 1].y) / (arc[i].x - arc[i - 1].x);
  50.       arc[i].x := arc[i - 1].x + Round(arc[i - 1].y / -m);
  51.       arc[i].y := 0;
  52.     end;
  53.   end;
  54.  
  55.   tb := GetTPABounds(arc);
  56.  
  57.   tb.y2 := tb.y2 + 5;
  58.   tb.x2 := tb.x2 + 2;
  59.  
  60.   DisplayDebugImgWindow(tb.x2 + 2, tb.y2 + 5);
  61.   Wait(50);
  62.   GetDebugCanvas.Brush.Color := clWhite;
  63.   GetDebugCanvas.Rectangle(0, 0, tb.x2 + 2, tb.y2 + 5);
  64.   GetDebugCanvas.MoveTo(2, tb.y2);
  65.   GetDebugCanvas.Pen.Color := clBlack;
  66.  
  67.   for i := 0 to High(arc) do
  68.     GetDebugCanvas.LineTo(arc[i].x, tb.y2 - arc[i].y);
  69. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement