Delphi-PRAXiS
Seite 3 von 3     123   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi Kollision von 2 Kreisen (https://www.delphipraxis.net/33067-kollision-von-2-kreisen.html)

ReDoX 2. Nov 2004 15:56

Re: Kollision von 2 Kreisen
 
Aber die Falschen :( so ist es richtig :
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Button1: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TMeineShapes = class(TShape)
  public
    Richtung: TPoint;
    MittelPunkt: TPoint;
    constructor Create(Owner: TComponent); override;
    procedure MovingBall(Speed: Integer; AndererKreis: TMeineShapes);
  end;

var
  Kreis: TMeineShapes;
  Kreis1: TMeineShapes;
  Form1: TForm1;

implementation

uses Math;

{$R *.dfm}

constructor TMeineShapes.Create(Owner: TComponent);
begin
  inherited create(Owner);
  Richtung.X := 0;
  Richtung.Y := 0;
  Height := 66;
  Width := Height;
  Randomize;
  Left := 50 + Random(200);
  Top := 50 + Random (100);
  Brush.Color := clMoneyGreen;
  Pen.Width := 3;
  Shape := stCircle;
  MittelPunkt.X := (Left + BoundsRect.Right) div 2;
  MittelPunkt.Y := (Top + BoundsRect.Bottom) div 2;
end;

procedure TMeineShapes.MovingBall(Speed: Integer; AndererKreis: TMeineShapes);
var
  PlusMinus: Integer;
  Winkel: Integer;
  P: TPoint;
begin
  if Richtung.Y = 0 then
    begin
      Randomize;
      PlusMinus := Random (1);
      If Plusminus = 1 then
        Richtung.Y := 1
      else
        Richtung.Y := -1;
    end;

  if Richtung.X = 0 then
    begin
      Randomize;
      PlusMinus := Random (1);
      If Plusminus = 1 then
        Richtung.X := 1
      else
        Richtung.X := -1;
    end;

  MittelPunkt.X := (Left + BoundsRect.Right) div 2;
  MittelPunkt.Y := (Top + BoundsRect.Bottom) div 2;
  with AndererKreis do
    begin
      MittelPunkt.X := (Left + BoundsRect.Right) div 2;
      MittelPunkt.Y := (Top + BoundsRect.Bottom) div 2;
    end;

  if (Sqr(Abs(MittelPunkt.X - AndererKreis.MittelPunkt.X))) +
     (Sqr(Abs(MittelPunkt.Y - AndererKreis.MittelPunkt.Y))) <= Sqr(Width) then
    begin
      P.X := (MittelPunkt.X + AndererKreis.MittelPunkt.X) div 2;
      P.Y := (MittelPunkt.Y + AndererKreis.MittelPunkt.Y) div 2;
      try
        Winkel := StrToInt(FloatToStrF((ArcTan2(P.Y - MittelPunkt.Y,
                           MittelPunkt.X - P.X)/ Pi * 180), ffFixed,3,0));
      except
          Winkel := 0;
      end;
(*.*) Form1.Label1.Caption := IntToStr(P.X - MittelPunkt.X);
      Form1.Label2.Caption := IntToStr(P.Y - MittelPunkt.Y);
      Form1.Label3.Caption := IntToStr(Winkel);
      Case Winkel of
              0 :      Richtung.X := 1;
        1..89   : begin Richtung.X := 1; Richtung.Y := -1; end;
             90 :      Richtung.X := 1;
        91..179 : begin Richtung.X := -1; Richtung.Y := -1; end;
            180 :      Richtung.X := -1;
        -89..-1 : begin Richtung.X := 1; Richtung.Y := 1; end;
            -90 :                        Richtung.Y := -1;
       -179..-91: begin Richtung.X := -1; Richtung.Y := -1; end;
           -180 :                        Richtung.Y := 1;
      end;
end;

  Top := Top + Richtung.Y * Speed;
  Left := Left + Richtung.X * Speed;

  if BoundsRect.Bottom >= Parent.ClientHeight then
    Richtung.Y := -1;
  if Top <= 0 then
    Richtung.Y := 1;
  if BoundsRect.Right >= Parent.ClientWidth then
    Richtung.X := -1;
  if Left <= 0 then
    Richtung.X := 1;


end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Kreis.MovingBall(4, Kreis1);
  Kreis1.MovingBall(7, Kreis);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Kreis := TMeineShapes.Create(Self);
  Kreis.Parent := Self;
  Kreis1 := TMeineShapes.Create(Self);
  Kreis1.Parent := Self;
  Kreis1.Brush.Color := clSkyBlue;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Timer1.Enabled then
    TImer1.Enabled := False
  else
    Timer1.Enabled := True;
end;

end.

freak4fun 2. Nov 2004 16:02

Re: Kollision von 2 Kreisen
 
Was hast du verändert, Felix? :gruebel:

mfg
freak

ReDoX 2. Nov 2004 16:26

Re: Kollision von 2 Kreisen
 
Ich habe einfach nur die Delphi Tags benutzt! :-D
Mfg MrScholz


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:50 Uhr.
Seite 3 von 3     123   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz