hey,
i am back with my well known problem! so this is how far i am right now: i created my own processes to make it more structured. one is called "switch" and one is called "pythagoras". in the "switch" process i want the two circles to exchange velocities so it looks like they are bouncing off of each other.
i created a double loop for so that no circle is left out, but it isnt working.. my program is starting without any errors but the circle wont bounce off of eachother.
procedure pythagoras;
var d : real;
var j,i,c,a,b : integer;
begin
for j := 1 to 20 do
for i := 2 to 20 do begin
d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i]));
if d <= r[j]+r[i]+12 then begin
switch
switch
end;
end;
end;
sooo, this is currently my "pythagoras" process. after "switch" i need to add something i will come back to that later. soo that code i wrote up there i got from my teacher, but i already modified it, for example i added the "switch" process and changed the letters "j" and "i".
my questions now:
do i need to define vx, vy, mx, my and r new like this :
for i := 1 to 20 do begin
r[j] := 20;
mx[j] := random(image1.width);
my[j] := random(image1.height);
vx[j] := random(10);
vy[j] := random(10);
end;
and the same with i?
why is there at the part with the two "r`s" summed up a "+12" at the end?
btw: after the two "switch" i want to add (vx[j], vx
) and (vy[j], vx) but when i do that there comes the error Illegal qualifier
and the j and the i is marked.
this is my switch process:
procedure switch (Var a,b:integer);
var zwischenspeicher : integer;
begin
zwischenspeicher := a;
a := b;
b := zwischenspeicher;
end;
questions:
do i need two of those? so switch1 and switch2?
summed up:
I need to create 20 ellipses, spawning randomly, bouncing off the wall and moving around [DONE]
I need the ellipses to bounce off of each other when they touch [not done]
this is my whole code atm:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Image1: TImage;
Timer1: TTimer;
Timer2: TTimer;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShowHint(Sender: TObject; HintInfo: PHintInfo);
procedure Image1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
public
end;
var
Form1: TForm1;
mx : array[1..20] of integer;
my : array[1..20] of integer;
vx : array[1..20] of integer;
vy : array[1..20] of integer;
r : array[1..20] of integer;
zwischenspeicher1, zwischenspeicher2, c, c2,j,i,d : integer;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
timer1.enabled := true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
timer1.enabled := false;
end;
procedure switch (Var a,b:integer);
var zwischenspeicher : integer;
begin
zwischenspeicher := a;
a := b;
b := zwischenspeicher;
end;
procedure pythagoras;
var d : real;
var j,i,vx,vy,c,a,b : integer;
begin
for j := 1 to 20 do
for i := 2 to 20 do begin
d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i]));
if d <= r[j]+r[i]+12 then begin
switch
switch
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
form1.color:=clpurple;
randomize;
doublebuffered := true;
timer1.enabled := false;
for c := 1 to 20 do begin
r[c] := 20;
mx[c] := random(image1.width);
my[c] := random(image1.height);
vx[c] := random(10);
vy[c] := random(10);
end;
end;
procedure TForm1.FormShowHint(Sender: TObject; HintInfo: PHintInfo);
begin
end;
procedure TForm1.Image1Click(Sender: TObject);
begin
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
with image1.canvas do begin
for c := 1 to 20 do begin
pen.color := clblack;
brush.color:=clblack;
ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]);
mx[c]:=mx[c]+vx[c];
my[c]:=my[c]+vy[c];
pen.color := clpurple;
brush.color := clpurple;
ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]);
end;
end;
for c:= 1 to 20 do begin
if my[c]+r[c]>=image1.height
then vy[c]:= -vy[c];
if mx[c]+r[c]>=image1.width
then vx[c]:=-vx[c];
if my[c]-r[c]<0
then vy[c]:=-vy[c];
if mx[c]-r[c]<0
then vx[c]:=-vx[c];
end;
end;
end.
if you have any questions with the formulation of my question then feel free to ask!
regards
brady