Blame lazarus/gravity-control/unit1.pas

b59892
unit Unit1;
b59892
b59892
{$mode objfpc}{$H+}
b59892
b59892
interface
b59892
b59892
uses
b59892
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;
b59892
b59892
type
b59892
b59892
  { TForm1 }
b59892
b59892
  TForm1 = class(TForm)
b59892
    Shape1: TShape;
b59892
    Timer1: TTimer;
b59892
    Timer2: TTimer;
b59892
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
b59892
    procedure Timer1Timer(Sender: TObject);
b59892
    procedure Timer2Timer(Sender: TObject);
b59892
  private
b59892
    { private declarations }
b59892
  public
b59892
    { public declarations }
b59892
  end;
b59892
b59892
const
b59892
  G = 1000000;
b59892
b59892
var
b59892
  Form1: TForm1;
b59892
  mx: double = 0;
b59892
  my: double = 0;
b59892
  x: double = 200;
b59892
  y: double = 200;
b59892
  vx: double = 0;
b59892
  vy: double = 0;
b59892
b59892
implementation
b59892
b59892
{$R *.lfm}
b59892
b59892
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
b59892
  Y: Integer);
b59892
begin
b59892
  mx := x;
b59892
  my := y;
b59892
end;
b59892
b59892
procedure TForm1.Timer1Timer(Sender: TObject);
b59892
var
b59892
  dx, dy, d: double;
b59892
  ax, ay: double;
b59892
  t, r: double;
b59892
begin
b59892
  dx := mx - x;
b59892
  dy := my - y;
b59892
  d := sqrt(dx*dx + dy*dy);
b59892
  t := Timer1.Interval/1000;
b59892
  r := Shape1.Width/2;
b59892
b59892
  ax := G*dx/(d*d*d);
b59892
  ay := G*dy/(d*d*d);
b59892
b59892
  if d < 10 then begin ax:=0; ay:=0; end;
b59892
b59892
  vx := vx + ax*t;
b59892
  vy := vy + ay*t;
b59892
b59892
  x := x + vx*t;
b59892
  y := y + vy*t;
b59892
b59892
  if x < r then begin x:=r; vx:=abs(vx); end;
b59892
  if x > ClientWidth-r then begin x:=ClientWidth-r; vx:=-abs(vx); end;
b59892
  if y < r then begin y:=r; vy:=abs(vy); end;
b59892
  if y > ClientHeight-r then begin y:=ClientHeight-r; vy:=-abs(vy); end;
b59892
end;
b59892
b59892
procedure TForm1.Timer2Timer(Sender: TObject);
b59892
begin
b59892
  Shape1.Left := round(x - Shape1.Width/2);
b59892
  Shape1.Top  := round(y - Shape1.Height/2);
b59892
end;
b59892
b59892
end.
b59892