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