wave simulation lazarus / 70-OpenGL ---------------------------------------------------------------------- Farida Basri · 2015-06-18 02:02 hi guys, i am new member in here i also just start to learn about lazarus. i never learn about programming before could you please, answer my question about wave simulation? ---------------------------------------------------------------------- osddeitf · 2015-06-18 02:39 There are 2 app type in lazarus: GUI or non GUI. First you have to learn programming in lazarus with non GUI app. Create a project by: Project --> New Project... --> Simple Program --> OK. You see the code in Source Editor like this: program Project1; begin end. 'program' is the keyword that define your program name. between 'begin' and 'end' is the code that run while your program are running. Add those code to the between 'begin' and 'end': write('Hello'); readln; then run by press F9 key and see your first app. You can also see in: http://wiki.freepascal.org/Hello,_World ---------------------------------------------------------------------- BitBangerUSA · 2015-06-18 02:41 quite likely someone(s) here can answer. but you should be more informative... ocean waves? sine waves / math functions? human greetings? if you know very little about programming - you probably should spent a good amount of time reading about programming - not specifics about *how* - before you start coding. if you don't have a good idea of how something works, trying to do it will only lead to frustration. you might also want to post about what, if any, technical training you have - regardless of subject. *good* programming requires critical thinking. ---------------------------------------------------------------------- Farida Basri · 2015-06-18 03:01 Hi osddeitf Hi BitBangerUSA thank you guys for your comment and suggestion. actually i already start learn lazarus since first semester (now i am at second semester), but still not expert in programming. now, i try to make program about water wave simulation. firsty, i read jakarta's map from SRTM, and after that make wave simulation. but, when i click wave direction from TRadioGroup, wave only come one times, not repeatly. do you have any suggestion about my problem? ---------------------------------------------------------------------- BitBangerUSA · 2015-06-18 03:06 'still not expert in programming' nor am i - but that doesn't stop me from doing it... can't see your attached PNG while typing this... you can and should post all or the important part of you code here - use the Insert Code (# symbol) - then people can likely see what is causing you grief. ---------------------------------------------------------------------- jc99 · 2015-06-18 04:54 Hi Farida, Looked at the picture, looks impressing. To the problem: Does the wave come every time you change the direction ? Or does it come only once, you click on the direction and never again ? I second BitBangerUSA, post some code, or if it's not too big the compressed project. A good start would be the code of the events regarding the direction. Since that starts the wave once. PS: you've found the right spot. (you know what i mean) ---------------------------------------------------------------------- Farida Basri · 2015-06-18 05:17 Hi jc99 Hi BitBangerUSA jc99 for your question : Does the wave come every time you change the direction ? No, the waves only come one time and not likes the real water wave in ocean, hehehe this is code for that wave procedure TForm1.Button2Click(Sender: TObject); var   Hight : real;   trackbaru : integer; begin     //---- Clear Initial Value ------     Wnew := Nil;     Wnow := Nil;     Wold := Nil;     SetLength(Wnew,MapW+1,MapH+1);     SetLength(Wnow,MapW+1,MapH+1);     SetLength(Wold,MapW+1,MapH+1);     trackbaru := TrackBar2.Position;     Hight := trackbaru/10;     //----- Set Initial Value -------     //for j :=0 to MapH do     //for i := 0 to 0 do     for j :=0 to 0 do     for i := 0 to MapW do     begin       Wnew[i,j] := 0;       Wnow[i,j] := 0;       Wold[i,j] := 0;     end;     Case WaveDirection.ItemIndex of      0:    // --------- Make Wave From West        begin          //----- Set Initial Value --------\\          for j := 0 to MapH do          for i := 0 to 0 do            begin              Wnow[i,j] :=Hight;            end;        end;     1:    // --------- Make Wave From Nort West        begin      //----- Set Initial Value --------          for j := 0 to 0 do          for i := 0 to 5 do            begin              Wnow[i,j] :=Hight;            end;        end;      2:   //----- Make Wave From North         begin      //----- Set Initial Value --------\\           for j := 0 to 10 do           for i := 0 to MapW do             begin               Wnow[i,j] :=Hight;             end;         end;     3:  //----- Make Wave From North east     begin     //----- Set Initial Value --------         for j := 0 to 0 do         for i := MapW-10 to MapW-1 do           begin               Wnow[i,j] :=Hight;           end;     end;     4:  //----- Make Wave From east     begin     //----- Set Initial Value --------\\       for j := 0 to MapH-1 do       for i := MapW-10 to MapW do          begin            Wnow[i,j] :=Hight;          end;     end;     end; end; ---------------------------------------------------------------------- osddeitf · 2015-06-18 08:41 First, the code: Code: [Select]    Wnew := Nil;     Wnow := Nil;     Wold := Nil; must be Code: [Select]    setLength(Wnew, 0, 0);     setLength(Wnow, 0, 0);     setLength(Wold, 0, 0); And make sure you use a Timer with an event of it to repeat the wave like this: Code: [Select]// create a timer with an interval such as 100 to create a wave repeatly after 100ms procedure TForm1.Timer1Timer(Sender: TObject); begin   //put your wave create procedure here   //if you have a timer before like Timer2, add this:   //Timer2.enabled:= true;   //and put Timer2.enabled:= false; before the 'end' keyword of Timer2 Ontimer Event end; And create some code that reset the wave when click Button2 on its event ---------------------------------------------------------------------- jc99 · 2015-06-18 17:45 Quote from: osddeitf on June 18, 2015, 10:41:57 amFirst, the code: Code: [Select]    Wnew := Nil;     Wnow := Nil;     Wold := Nil; must be Code: [Select]    setLength(Wnew, 0, 0);     setLength(Wnow, 0, 0);     setLength(Wold, 0, 0); Explanation: if you just set the values to nil, you most likely don't free the formerly used memory. Setlength makes sure to release the memory used by the array. My suggesstion here is: Initialize the field's in an OnCreate or other initialization-event. And just Put all the Knots to your desired value.   Quote from: osddeitf on June 18, 2015, 10:41:57 amAnd make sure you use a Timer with an event of it to repeat the wave like this: Code: [Select]// create a timer with an interval such as 100 to create a wave repeatly after 100ms procedure TForm1.Timer1Timer(Sender: TObject); begin   //put your wave create procedure here   //if you have a timer before like Timer2, add this:   //Timer2.enabled:= true;   //and put Timer2.enabled:= false; before the 'end' keyword of Timer2 Ontimer Event end; And create some code that reset the wave when click Button2 on its event Here I Disagree. if you have a simulation, than you already have a timer / thread, or whatever you use compute the simulation. With your on-Button-event you create a defined disturbance which runs trough your simulation-field. If you want a constant wave / or wavefront to run through your field, you should set a knot of your choice to a function of your choice (e.G: a sine, or triangular ) for a wavefront you have to set a bunch of adjected knots. This acts as a Wave generator. you could also play with an adjustable damping at the edges, to simulate other reflections. Think of the whole Sim as a waterbasing, and with the on Button you hit the surface once. If you want an ongoing thing you have to simulate that too. ---------------------------------------------------------------------- Farida Basri · 2015-06-19 13:01 Hi osddeitf Hi jc99 Thank you guys for suggestion. Sorry i just reply your comment now, i already try your suggestion guys, but still got an error. Could you tell me please if did something wrong? ---------------------------------------------------------------------- zbyna · 2015-06-19 13:30 If you want to help from somebody post also other files from your project directory. Here is information: http://wiki.freepascal.org/Lazarus_Tutorial#The_Lazarus_files ---------------------------------------------------------------------- Farida Basri · 2015-06-19 14:39 Hi zbyna Thank you already remind me  i realized make mistake https://www.dropbox.com/s/mxa59h836nfn02f/JakartaOk7_Edit.rar?dl=0 ---------------------------------------------------------------------- jc99 · 2015-06-19 15:45 Hi, First of all your loop-Variables should be local, You can use the Command-Completion-Shortcut --C to do it. ( referes to all Code: [Select]FOR i := ... FOR j := ... Statements. ) Second Assigning an absolute PAth in the Source-code is a very unpleasant thing. do something relative e.G: Code: [Select]'..' + DirectorySepator +'Data' + DirectorySepator + 'oki.txt' or ask the User what file to load. Third: Set the Timer to Enabled := false and enable it at the end of Butten1Click. Then set your Simulation time in the Timer-Event (FSimTime) and set Hight to a function relating that time. Code: [Select][...]   FSimTime := Fsimtime + 0.1;     trackbaru := TrackBar2.Position;     Hight := trackbaru/10 * cos ( FSimtime); [...] ---------------------------------------------------------------------- jc99 · 2015-06-19 16:41 try that ... data is in ../data/WaveSim/.. ---------------------------------------------------------------------- howardpc · 2015-06-19 18:01 Good work, jc and farida. (Note, in Button1Click() you need to free bitmap and resizedbitmap to avoid a memory leak). ---------------------------------------------------------------------- jc99 · 2015-06-20 05:03 @howardpc: You are right, i wanted to point that out on my last post that there is a mem-leak (i had not figured out where), but i was a little in a hurry. So: Code: [Select]  [...]   //Open pic    bitmap := tbitmap.create;    resizedbitmap := tbitmap.create;    TRY  // <--    bitmap.loadfromfile(FDataDir + DirectorySeparator+'WaveSim'+ DirectorySeparator+'Jakarta.bmp');    newheight := Round(MapH/4);  //resize pic    newwidth := Round(MapW/4); [...]    resizedbitmap.Canvas.StretchDraw(stretchrect, bitmap);    Image1.picture.bitmap := resizedbitmap;   ShowMessage(' MapW = '+FloatToStr(MapW)+ ' MapH = ' + FloatToStr(MapH)+' DataMax = '        + FloatToStr(DataMax) + ' DataMin = '+ FloatToStr(DataMin));   FINALLY // <---     freeandnil(bitmap); // <---     freeandnil(resizedbitmap); // <---   end; ---------------------------------------------------------------------- Farida Basri · 2015-06-21 02:50 Hi jc99 Thank you for your help  but, can you answer my question about : 1) Why i used this code :  assert( fileExists (FDataDir + DirectorySeparator+'WaveSim'+ DirectorySeparator+'oki.txt'), 'No Oki.txt - File');  AssignFile(F,FDataDir + DirectorySeparator+'WaveSim'+ DirectorySeparator+'oki.txt'); because normally when i want to put directory for my map i used : AssignFile(F,'D:\\Smad-Lock(Brankas_Smadav)\\#MATH MODEL#\\JakartaOk7_Edit\\JakartaOk7\\JakartaOk\\oki.txt'); 2) Could you explain, what you mean with directory separator? sorry, but i am confused ---------------------------------------------------------------------- jc99 · 2015-06-21 03:40 Quote from: Farida Basri on June 21, 2015, 04:50:01 am1) Why i used this code :  assert( fileExists (FDataDir + DirectorySeparator+'WaveSim'+ DirectorySeparator+'oki.txt'), 'No Oki.txt - File');  AssignFile(F,FDataDir + DirectorySeparator+'WaveSim'+ DirectorySeparator+'oki.txt');   because normally when i want to put directory for my map i used : AssignFile(F,'D:\\Smad-Lock(Brankas_Smadav)\\#MATH MODEL#\\JakartaOk7_Edit\\JakartaOk7\\JakartaOk\\oki.txt'); Assert makes sure the some axioms are set. Here: that the file exists. You used an absolute path. This is fine as long you are the only on to run your App. But no one else can run your app. And not even you when you move it to another computer. So you should set it to something relative to your Exe , or your Home- Dir, or somthing more project related. Quote from: Farida Basri on June 21, 2015, 04:50:01 am2) Could you explain, what you mean with directory separator? sorry, but i am confused  DirectorySeparator is replacement for '\' on windows, and '/' on Linux or whatever character an operating system is using to separate directories in a path. I have a strict separation between files. Project, source, data and binary-files are each in separate folders. (So i can switch between compilers, computers, os's ... ) Project: .\fpc, .\dxe, .\d7 ... Source: .\source Data: ..\data\ Bin: ..\bin\    Units: ..\bin\\units To get the thing running I had to move your data-files. in your case I suggest you initialize FBaseDir to 'D:\\Smad- Lock(Brankas_Smadav)\\#MATH MODEL#\\JakartaOk7_Edit' or whatever you see as your basic Project-path. and fill the rest in like  + DirectorySeparator+'JakartaOk7'+ DirectorySeparator+ 'JakartaOk'+ DirectorySeparator+ ---------------------------------------------------------------------- Farida Basri · 2015-07-01 14:23 hi jc99 hi osddeitf hi howardpc hi friends, i really appreciate your help to me. nice to know about all of you thank you very much