Fluid Dynamics for lower editions

Posted By: the_mehmaster

Fluid Dynamics for lower editions - 07/05/09 07:28

This post is updated
Firstly, I'd like to thank evilSOB for recoding the entire system, which now works great. Second, thanks to VeT for his immense interest and help.

Here's the first demo in a series:
Fluid simulation demo 1 download (hosted my own site this time, so the link won't die)
left click and drag across the plane for a nice 'liquidy' effect.

TODO and Upcoming things:
  • Improve the frame independancy(less 'shaking') - DONE
  • Release a demo (and tutorial) that works with newton physics - 70%
  • Splash particle effects - 60%


Anyone with suggestions, bug reports, or anything like that, please share!

Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/05/09 08:51

Something i need fixed:

For some reason when i call vec_to_mesh, only every second vertex works?

changing to this makes it crash:
Code:
c.z = dest[iloop+1][eloop+1];
ent_setvertex(waterent,c,counter);


anyone know why?
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/05/09 13:50

Im looking into this for you. But my results wil be a bit "piece-meal"
as I am getting often interruped right now.
Feel free to ask questions about anything I say / fix at any time.

Firstly, debug is failing (intermittantly but often) because your
"source" and "dest" arrays are defined wrong.
Youve defined them both with "19" columns/rows, but you are writing to 20.
(element 0 is the first, through to element 19 is the 20th)
Change their declarations to
var dest[20][20];
var source[20][20];

and that problem is repaired.

Im doing some general lite-c optimisations while Im in here too.
This code looks like either you based it on an old c-script code,
or that YOU are more familiar with c-script than lite-c.
Either way, these improvements should help you grasp lite-c better.
(and make it easier for ME to debug your core code problems wink )

[EDIT]
Originally Posted By: EvilSOB
...This code looks like either you based it on an old c-script code,...
Not that Im saying you based it on someone-elses work,
but that youve used an old mesh-deformation c-script to put your water-physics onto.



Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/05/09 14:40

Heres the closest I can get to what I think your trying to achieve.
I cant help with the array values themselves, as I dont inderstand what you are doing.
(look at the time-stamp on this post and you'll understand that laugh )
But it think its those calculations that is cancelling out every second vertex.
I tested this by adding in a random value so I THINK Im right.

Take a look and see how its going.
Im looking forward to your reply with interest...
This is an interesting topic.

Code:
#include <acknex.h>
#include <default.c>

var dest[20][20];
var source[20][20];
var tempv;

ENTITY* waterent;

PANEL* skeletonpan =
{
  	pos_x = 0;		pos_y = 0;
	digits( 0,  0, "nexus: %f", "Arial#18b", 1, nexus);
	digits( 0, 20, "tempv: %f", "Arial#18b", 1, tempv);
  	flags =  VISIBLE;
}


function main()
{
	video_mode = 10;
	video_screen = 1;
	level_load("");
	vec_set(camera.x,   vector(0,50,50));
	vec_set(camera.pan, vector(270,-45,0));
	waterent = ent_create("waterent.hmp",nullvector,NULL);



	var iloop=0, eloop=0;
	CONTACT* c;
	//
	// Initialise arrays
	for(iloop=0; iloop<20; iloop++)
	{
		for(eloop=0; eloop<20; eloop++)
		{
			dest[iloop][eloop] 	 = 0;
			source[iloop][eloop] = 0;
		}
	}
	//
	//
	while(1)
	{

		for(iloop=1; iloop<18; iloop++)
		{
			for(eloop=1; eloop<18; eloop++)
			{
				//perform simulation
				dest[iloop+1][eloop+1] = ((source[iloop-1+1][eloop+1]+source[iloop+1+1][eloop+1]+source[iloop+1][eloop-1+1]+source[iloop+1][eloop+1+1])/2)-dest[iloop+1][eloop+1];
				dest[iloop+1][eloop+1] *= 0.95;
				vec_to_mesh(vector(0,0,dest[iloop+1][eloop+1]), waterent, iloop*20+eloop+1);
				//debugging
				tempv = source[iloop+1][eloop+1];
			}
		}
		for(iloop=1; iloop<18; iloop++)
		{
			for(eloop=1; eloop<18; eloop++)
			{
				//perform simulation
				source[iloop+1][eloop+1] = ((dest[iloop-1+1][eloop+1]+dest[iloop+1+1][eloop+1]+dest[iloop+1][eloop-1+1]+dest[iloop+1][eloop+1+1])/2)-source[iloop+1][eloop+1];
				source[iloop+1][eloop+1] *= 0.95;
				vec_to_mesh (vector(0,0,source[iloop][eloop]), waterent, iloop*20+eloop+1);
				//debugging
				//tempv = source[iloop+1][eloop+1];
			}
		}


		//
		if(key_cuu)
		{
			source[10][10]+=30;
			while(key_cuu)wait(1);
		}
		wait(1);
	}
}


Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/05/09 21:12

I found a webpage here:
http://freespace.virgin.net/hugo.elias/graphics/x_water.htm
And that's where i ported it from.

Fixing the debug panel was one of the main issues..thanks!

About adding a random value..
I tried that myself before(but to the counter variable which is now gone).
It seems to be adding twice as much as it should? i don't understand as i'v looked through this code thouroghly.. I will work on this later today(got to go to school now).

I thought this was an interesting topic too. I was originally going to do navier-stokes fluid(3d fluid) but that was too complicated. maybe when this is finished.

Thanks for your interest and help!

EDIT: i would of used 'for' loops like you but i didn't know lite-c supported them..
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/05/09 21:30

this:
Code:
vec_to_mesh(vector(0,0,dest[iloop+1][eloop+1]), waterent, (iloop*20)/2+(eloop+1)/2);


instead of what is already there,
halfs the reference value. This works, but then only a quarter of the terrain is used. The simulation also has noticeable 'waves' with this alteration
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/06/09 03:03

Cool, I'll look into it when I get to work tonite.
About 7 hours after this post.
Im sposed to be sleeping now, oh how I hate night shift...
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/06/09 07:56

Hmm.. worked on it for about an hour and still confused.. Why every second vertex?

I tried doubling the array size then reading it at half.. Which works very well, but it's not very efficient (about 170fps, compared to 430fps), which is very slow for a water simulation..

EDIT: It must be something to do with the arrays. Hopefully that webpage helps you to understand.. Maybe you could rewrite the 2 lines of simulation?

I used to be a c++ guy. I might turn this into a DLL if it works..

Thanks again for your help..
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/06/09 10:14

Im about to start now.
At this point, I suspect a simple typo in one of the dest[iloop+1][eloop+1] = ((source[iloop-1+1][eloop+1]+...... lines
is zeroing out every second vertex value in either array 1 or array 2.
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/06/09 11:22

I hope your right. I have talked to VeT about using this with his wrapper's buoyancy feature. The feature will be released later today.

I was also thinking of a feature (that I will code) that uses each vertex's z coordinate. It should have a threshold, and when the z value reaches that threshold it emits water particles(splashes). Just for a touch more realism..

But before any of that, this needs to be finished.

Just as an offtopic:
I noticed you also live in Australia. I live on a farm in Wagga, NSW. What town are you in at the moment?

Regards,
Seb.
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/06/09 11:44

Heres a largely fixed version of your code, it turns out that the BIG reason
that every second vertex was zero was purely because there was no wait between
the first vertex-loop and the second.

All other changes Ive made were mostly part of my fault hunting process and can probably be reversed.
Im going to try and do a more "self-contained" and flexible version of this tonight,
but there wont be much of your code left in it Im afraid, cause I will start from scratch,
based on the webpage you sent me, rather than any of your code.
I'll post it here if I have any luck. And if my version gets posted in contributions, I'll credit you.
Code:
#include <acknex.h>
#include <default.c>

var dest[20][20];
var source[20][20];

ENTITY* waterent;

PANEL* skeletonpan =
{
  	pos_x = 0;		pos_y = 0;
	digits( 0,  0, "nexus: %f", "Arial#18b", 1, nexus);
	digits( 0, 20, "tempv: %f", "Arial#18b", 1, tempv);
  	flags =  VISIBLE;
}


action fluid_action()
{
	CONTACT* c;
	var tempv, iloop, eloop;
	//
	// Initialise arrays
	for(iloop=0; iloop<20; iloop++)
	{
		for(eloop=0; eloop<20; eloop++)
		{
			dest[iloop][eloop] 	 = 0;
			source[iloop][eloop] = 0;
		}
	}
	//
	//
	while(1)
	{

		for(iloop=1; iloop<19; iloop++)
		{
			for(eloop=1; eloop<19; eloop++)
			{
				//perform simulation
				dest[iloop][eloop]  = (source[iloop-1][eloop]+source[iloop+1][eloop]+source[iloop][eloop-1]+source[iloop][eloop+1])/2-dest[iloop][eloop];
				dest[iloop][eloop] *= 0.95;
				vec_to_mesh(vector(0,0,dest[iloop][eloop]), my, iloop*20+eloop+1);
				//debugging
				tempv = source[iloop+1][eloop+1];
			}
		}
		wait(1);
		for(iloop=1; iloop<19; iloop++)
		{
			for(eloop=1; eloop<19; eloop++)
			{
				//perform simulation
				source[iloop][eloop]  = (dest[iloop-1][eloop]+dest[iloop+1][eloop]+dest[iloop][eloop-1]+dest[iloop][eloop+1])/2-source[iloop][eloop];
				source[iloop][eloop] *= 0.95;
				vec_to_mesh (vector(0,0,source[iloop][eloop]), my, iloop*20+eloop+1);
				//debugging
				//tempv = source[iloop+1][eloop+1];
			}
		}
		wait(1);
	}	
}



function main()
{
	video_mode = 10;
	video_screen = 1;
	level_load("");
	vec_set(camera.x,   vector(0,50,50));
	vec_set(camera.pan, vector(270,-45,0));
	waterent = ent_create("waterent.hmp", nullvector, fluid_action);
	//
	while(1)
	{
		if(key_cuu)
		{
			source[10][10]+=10;
			while(key_cuu)	wait(1);
		}
		wait(1);
	}
}



[Off-topic]Im in Wallsend, Newcastle, NSW myself, on the far side of Sydney from you.
So Im not really too far away, Im at work here, but I live in Morisset, a half-hour south.
Posted By: Germanunkol

Re: Fluid Dynamics for lower editions - 07/06/09 16:22

I don't think the thing's framerate independent.

The loops are executed every frame (while(1){...wait(1);}) and thus the wave reaches is end very fast. If a computer runs at 10fps it will cover 10 vertecies a second, if it runs at 60, the wave will make a speed of 60 vertecies a second.

The problem is, I don't see how to change that easily. The best way would, I think, be a sin() function that depends on time AND distance to the initial force on the water.
Btw, I think this is a very cool idea and I'd like to see it evolve more, but could you make it look coler? like make it blue and then maybe add a light so the movement is easier to see...?
As I think you want this for the extra version (did I understand that right?) I'm not recommending using shaders, otherwise I would.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/06/09 17:08

boys, new version is uploaded wink
http://depositfiles.com/files/9x1svcbe4
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/06/09 21:16

@vet: Awesome work Vet!
@evilSOB: Wow! works perfectly now. I recommend setting the specular 'power' of the terrain to a high value, you can see the waves easier. I'll make a demo level later today for the public, since they'll probably want screenshots and a demo level to download. Unless you don't want to make one..

Only problem is to make it frame rate independent. I think that the 'velocity' value is in the end of the first processing line.

Other than that, you did an awesome job.

regards,
seb.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/06/09 21:53

I'll wait when you'd finish this work and i'll add this to the wrapper with credits for you smile
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/07/09 06:59

Code:
#include <acknex.h>
#include <default.c>

var dest[20][20];
var source[20][20];

ENTITY* waterent;

PANEL* skeletonpan =
{
  	pos_x = 0;		pos_y = 0;
	digits( 0,  0, "nexus: %f", "Arial#18b", 1, nexus);
	digits( 0, 20, "tempv: %f", "Arial#18b", 1, tempv);
  	flags =  VISIBLE;
}


action fluid_action()
{
	CONTACT* c;
	var tempv, iloop, eloop;
	//
	// Initialise arrays
	for(iloop=0; iloop<20; iloop++)
	{
		for(eloop=0; eloop<20; eloop++)
		{
			dest[iloop][eloop] 	 = 0;
			source[iloop][eloop] = 0;
		}
	}
	//
	//
	while(1)
	{

		for(iloop=1; iloop<19; iloop++)
		{
			for(eloop=1; eloop<19; eloop++)
			{
				//perform simulation
				dest[iloop][eloop]  = (source[iloop-1][eloop]+source[iloop+1][eloop]+source[iloop][eloop-1]+source[iloop][eloop+1])*(time_step*0.8)-dest[iloop][eloop];
				dest[iloop][eloop] *= 1*(1.1-time_step*1.1);
				vec_to_mesh(vector(0,0,dest[iloop][eloop]), my, iloop*20+eloop+1);
				//debugging
				tempv = source[iloop+1][eloop+1];
			}
		}
		wait(1);
		for(iloop=1; iloop<19; iloop++)
		{
			for(eloop=1; eloop<19; eloop++)
			{
				//perform simulation
				source[iloop][eloop]  = (dest[iloop-1][eloop]+dest[iloop+1][eloop]+dest[iloop][eloop-1]+dest[iloop][eloop+1])*(time_step*0.8)-source[iloop][eloop];
				source[iloop][eloop] *= 1*(1.1-time_step*1.1);
				vec_to_mesh (vector(0,0,source[iloop][eloop]), my, iloop*20+eloop+1);
				//debugging
				//tempv = source[iloop+1][eloop+1];
			}
		}
		wait(1);
	}	
}



function main()
{
	video_mode = 10;
	video_screen = 1;
	fps_max = 100;
	fps_min = 25;
	level_load("");
	vec_set(camera.x,   vector(0,50,50));
	vec_set(camera.pan, vector(270,-45,0));
	waterent = ent_create("waterent.hmp", nullvector, fluid_action);
	//
	while(1)
	{
		if(key_cuu)
		{
			source[10][10]+=20;
			while(key_cuu)	wait(1);
		}
		wait(1);
	}
}


It is now frame-rate independent.
But I think i'll stop working on my code and wait for yours, because that will be neater...

EDIT: changed some values to make it work better.

Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/07/09 07:34

Quote:
I'll wait when you'd finish this work and i'll add this to the wrapper with credits for you

smile

Just a question.. Can i use .hmp as a buoyancy plane?
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/07/09 09:08

for now you can't, it is defined as plane.. i'll see what i can do smile
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/07/09 09:31

Quote:
for now you can't, it is defined as plane.. i'll see what i can do

It's allright, not necessary. At the moment what i am doing is modifying your buoyancy demo by putting a passable .hmp on top of the existing plane. The positions of the rigid bodies are read into predefined variables. It works.

But if you did support .hmp, that would help, especially when evilSOB rolls out the new code. smile

@evilSOB: If you want to make this work with .mdl as well you'll have to have a vec_for_mesh instruction as well as the existing vec_to_mesh, because otherwise the .mdl's vertices will be always at the same x and y coordinates. I can give an example if you want.

Are you using the new 'ent_setmesh' and 'ent_getmesh' instructions instead of the old ones? just for compatibility..

Can't wait for this to be finished! should be really cool.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/07/09 09:44

//Can't wait for this to be finished! should be really cool.
yep smile
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/07/09 10:32

Im still working on this, Ive gotten the code to go VERY short now.
eg.
Code:
var Data[2][20][20];

action fluid_action()
{	var x, y, i=0;
	for(y=0; y<20; y++)   for(x=0; x<20; x++)   Data[0][x][y] = Data[1][x][y] = 0;
	while(me)
	{	
		for(y=1; y<19; y++)   for(x=1; x<19; x++)   
		{
			Data[i][x][y]  = (Data[!i][x-1][y] + Data[!i][x+1][y] + Data[!i][x][y-1] + Data[!i][x][y+1]) / 2 - Data[i][x][y];
			Data[i][x][y] *= 0.95;
			vec_to_mesh(vector(0,0,Data[i][x][y]), my, (y*20)+x+1);
		}
		i = !i;
		wait(1);
	}	
}
//



But Im currently having some difficulties trying to use malloc to define a three dimensional array,
in order for it to cope with different size HMP's.
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/07/09 16:47

Gaaah! So close and yet so so far....

Ive streamlined the code, and it now includes MY version of accounting for FPS.
I couldnt get yours to work "the same" under different FPS rates.
[EDIT] I dont think this FPS-matching is correct. Ive got another idea in mind but I wont get to it till tonight...

But my TWO remaining issues are these.
1> Craps out if terrain is larger than 32x32. Is there some odd limitation on terrains?
(The vec_to_mesh seems to refuse to go higher than 1024 vertices.)

Issue fixed by using CONTACT structure.

2> I cannot sucessfully store a pointer to data-array[0] into the entities skill1.
Please check my code on this. Its not something Im familiar with....

Thanks guys....
Code:
#include <acknex.h>
#include <default.c>




#define	vertex_arrays	skill1		//vertex data arrays (var*)
#define	vertex_count	skill2		//number of vertices per array
#define	vertex_wide	skill3		//width of HMP in vertices
#define	vertex_high	skill4		//height of HMP in vertices
#define	viscosity	skill5		//"thickness" of the fluid.  0=Water -> 100=EngineOil

action fluid_action()
{
	if(!ent_status(my,2)||!ent_status(my,3))		return;		//not a terrain entity.
	var size_x=ent_status(my,2)+1, size_y=ent_status(my,3)+1;
	var x, y, f, tmp, ***data = malloc(sizeof(var)*2);
	for(f=0; f<2; f++)	
	{	data[f] = malloc(sizeof(var)*size_x);
		for(x=0; x<size_x; x++)	
		{	(data[f])[x] = malloc(sizeof(var)*size_y);
			for(y=0; y<size_y; y++)		
				((data[f])[x])[y]  = 0;
		}
	}
	my.vertex_arrays = (void*)(data[0])[0];
	my.vertex_count  = ent_status(my, 0);
	my.vertex_wide   = size_x;
	my.vertex_high   = size_y;
	my.viscosity	 = 0;
	while(me)
	{	
		f = !f;
		for(y=1; y<(size_y-1); y++)   for(x=1; x<(size_x-1); x++)   
		{	
			tmp  = ((data[!f])[x-1])[y] + ((data[!f])[x+1])[y] + ((data[!f])[x])[y-1] + ((data[!f])[x])[y+1];
			((data[f])[x])[y]  = (tmp / 2 - ((data[f])[x])[y]) * 0.95;
			CONTACT* c = ent_getvertex(my,NULL,(y*size_x)+x+1);    
			c.v.y = ((data[f])[x])[y];
			ent_setvertex(my,c,(y*size_x)+x+1);
		}
		if(key_1)	((data[0])[2])[size_y/2] += 5;		//TEST POKE - debug only
		wait(-(25+my.viscosity)/1000);
	}	
	//cleanup
	for(x=0; x<size_x; x++)	{  free((data[0])[x]);	free((data[1])[x]);  }
	free(data[0]);		free(data[1]);	 	free(data);	 
}



ENTITY* waterent;

function main()
{
	video_mode = 10;
	video_screen = 1;
	level_load("");
	vec_set(camera.x,   vector(0,50,50));
	vec_set(camera.pan, vector(270,-45,0));
	//
	waterent = ent_create("waterent32.hmp", nullvector, fluid_action);
	//
	while(1)
	{
		if(key_cuu)
		{
			beep();
			((var*)waterent.vertex_arrays)[110]	+= 15;
			while(key_cuu)	wait(1);
		}
		wait(1);
	}
	//cleanup
	for(x=0; x<size_x; x++)	{  free((data[0])[x]);   free((data[1])[x]);  }
	free(data[0]);		free(data[0]);	 	free(data);	 
}
//


Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/07/09 21:07

In the manual it is recommended to have terrains smaller than 32x32. It is probably unneeded to support larger terrains. oh you fixed it. cool!

Pointer to skill? hmm.. I have no idea.

That code is a lot more compact than mine, which is a very good thing. My 14-year-old code is no where near your standard!

I get a compilation error in line 73. the variable 'x' is undeclared. When i get rid of the cleanup lines it compiles though..

When i press the up arrow, no ripples are anywhere. Or is this part of the problem?

Anyway, great job!

Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/07/09 21:19

Updated the code, now works with larger than 32*32.

Try testing my code "as is" before splicing it into yours. That would explain the error at cleanup.

Its still got a bit of testing to do, which I wont get to till after 7pm tonight.
Its time for bed...

[EDIT] Damn, I gotta drive home from work first....
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/07/09 21:36

It is tested 'as-is'.

Why is there 2 clean up functions? one at the and of 'main' and another at the end of the action?

And they both have exactly the same instructions.
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/07/09 22:34

Bad post... Sorry.

But here is a repost, complete EXCEPT that the frame-rate dependance still needs tuning.
But as I said before, I'll get to that tonight. I need to sleep, but thought of how to "control" the
entitys fluid from outside while driving home. Eureka!...ohshit.ohshit...Swerve!
With this edition, all you need to do is directly adjust one of the vertices,
and the action will now see it and react accordingly. See the loop in main for how its done.
Code:
#include <acknex.h>
#include <default.c>



#define	viscosity		skill1		//"thickness" of the fluid.  0=Water -> 100=EngineOil
#define	vertex_wide	skill2		//width of HMP in vertices
#define	vertex_high	skill3		//height of HMP in vertices

action fluid_action()
{
	if(!ent_status(my,2)||!ent_status(my,3))		return;		//not a terrain entity.
	var size_x=ent_status(my,2)+1, size_y=ent_status(my,3)+1;
	var x, y, f, tmp, ***data = malloc(sizeof(var)*2);
	for(f=0; f<2; f++)	
	{	data[f] = malloc(sizeof(var)*size_x);
		for(x=0; x<size_x; x++)	
		{	(data[f])[x] = malloc(sizeof(var)*size_y);
			for(y=0; y<size_y; y++)		
				((data[f])[x])[y]  = 0;
		}
	}
	my.viscosity	 = 0;
	my.vertex_wide   = size_x;
	my.vertex_high   = size_y;
	while(me)
	{	
		f = !f;
		for(y=1; y<(size_y-1); y++)   for(x=1; x<(size_x-1); x++)   
		{	
			CONTACT* c = ent_getvertex(my,NULL,(y*size_x)+x+1);    
			if((c.v.y!=((data[0])[x])[y])&&(c.v.y!=((data[1])[x])[y]))
				((data[f])[x])[y]  = c.v.y;
			else
			{
				tmp  = ((data[!f])[x-1])[y] + ((data[!f])[x+1])[y] + ((data[!f])[x])[y-1] + ((data[!f])[x])[y+1];
				((data[f])[x])[y]  = (tmp / 2 - ((data[f])[x])[y]) * 0.95;
				c.v.y = ((data[f])[x])[y];
				ent_setvertex(my,c,(y*size_x)+x+1);
			}
		}
		wait(-(25+my.viscosity)/1000);
	}	
	//cleanup
	for(x=0; x<size_x; x++)	{   free((data[0])[x]);   free((data[1])[x]);   }
	free(data[0]);		free(data[1]);	 	free(data);	 
}



ENTITY* waterent;

function main()
{
	video_mode = 10;
	video_screen = 1;
	level_load("");
	vec_set(camera.x,   vector(0,50,50));
	vec_set(camera.pan, vector(270,-45,0));
	waterent = ent_create("waterent.hmp", nullvector, fluid_action);
	while(1)
	{
		if(key_cuu)
		{
			CONTACT* c = ent_getvertex(waterent,NULL,210);    
			c.v.y += 5;	//YES .y IS correct in D3D terms
			ent_setvertex(waterent,c,210);
			while(key_cuu)	wait(1);
		}
		wait(1);
	}
}
//


Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/08/09 06:35

Quote:
all you need to do is directly adjust one of the vertices,
and the action will now see it and react accordingly

Yes! no more predefined variables for every action!

That's what i used to do with newton..

Great work! i'll try to get this working with newton now.
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/08/09 06:41

Before i upload a demo, do you know of a fixed-function environment mapping shader? would make the demo look a heap cooler..

EDIT: couldn't find a working one in the wiki..

EDIT2:
Just thought i'd submit this:
FluidSimDemo1
What it is:
This demo is the simplest. It is just a terrain applied with the fluid action. To interact with the fluid, hold down your left mouse button and drag it around the plane.

This is the first (and simplest) demo in a series, which I made in about half an hour (shows how easy your code is to work with).
I am almost finished the newton one which i will submit very soon.
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/08/09 08:53

I was going to post EDIT3, but that's a bit too far..

The FPS independancy works quite well already. What needs to be improved?

EDIT: It's very hard to resist the temptation of posting the demo in contributions..Gotta wait until all the demos are finished though.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/08/09 09:01

hm... looking like it shakes too much, as for me
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/08/09 09:04

You can change the 'viscosity' value if you like. In line 24.
evilSOB, you made this so simple! very user-friendly.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/08/09 09:14

Yes, but i mean about fps-independent feature... i think, there is a trouble, in the evening i'll send you video
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/08/09 09:28

I think Vet is right, the fps-independance is a bit of a "fudge" ATM.
Im going to have another look at it shortly, just NEED coffee first.

Glad its user-friendly, thats something I take pride in...

Also guys, I think we may need to change the "name" of the viscosity define.
Because if the terrain size IN QUANTS is small, the viscosity needs to be small to simulate a small body of water.
But when the terrain size is large IN QUANTS, but has the same number of wertices as the small one,
then you need to set the viscosity high to simulate a large body of water.

So maybe "viscosity_scale" or something? Any ideas...
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/08/09 09:36

Quote:
viscosity_scale

That would be perfect.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/08/09 09:39

hmm... i dont think so: for example, user throw brick into the large lake and into the small lake... so, velocity may be the same

No, you're right: if large terrain has the same number of vertices, as the small one, looking you really need to scale velocity
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/08/09 09:52

My thoughts exactly.
To my aging mind, the "velocity scale" and a fluids "viscosity" will be directly related to one another.
So my "viscosity_scale" allows the programmer to "tweak" those values at the same time.
A higher viscosity_scale means that either the terrain is a large body of "water",
or a small body of a "thicker" fluid.

Would you guys agree with this? Play with the viscosity value with what Ive already posted to see what I mean if need be.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/08/09 09:57

"tweak" is good, but i dont think that its good in this case smile

I think, that you may find distance between 2 vertexes and count viscosity_scale automatically

For example, if dist_between_vertexes = 1 Quant, then viscosity_scale = 1,
if dist_between_vertexes = 2 Quants, then viscosity_scale = 0.5,
if dist_between_vertexes = 0.5 Quant, then viscosity_scale = 2

Something like this
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/08/09 10:41

Sorry, but I think that would be bad.
FI, what if you have a level where one quant has a "game" scale of one inch,
then another level/game where the game-scale is one quant = one foot.
It would look awful.
Its got to be up to the level designer to decide on the "viscosity_scale" that
"matches" both the game-scale of the level, and the "type" of fluid being represented.

[EDIT] Getting this thing frame-rate independant is being a real bastard!

Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/08/09 11:18

Hm.. what about this variant: if designer set viscosity_scale = 0, then it is counted aitomatically, and if != 0, then engine would use that value? smile
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/08/09 11:52

Ill keep it in mind, cause I dont think it really makes any difference, after thinking about it.
After all, the viscosity_scale calculation will take place at the start of the action anyway,
and IF the entity is being created by ent_create, then the calculation will be completed by
the time the calling function over-writes it with a hard-coded value anyway.

I'll have to get back to you on this later though, cause the time_scaling is being a real
bastard, and the viscosity_scale will piggy-back off that when its done.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/08/09 12:12

Good luck smile
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/08/09 20:02

Gack! Brain melting...
I think I'll have to sleep on this one.
I just know Im going to have nightmares about three-dimensional arrays drifting through time.
With any luck I'll wake up with an answer.

If I keep going round in circles like this, Im going to disappear up my own algorithm.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/08/09 20:12

//I just know Im going to have nightmares about three-dimensional arrays drifting through time.
With any luck I'll wake up with an answer.

Maybe you'd share, whats the problem ? smile
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/08/09 20:26

Just trying to get the DAMNED thing to be frame-rate independant.
I cant seem to find a balanced way to put in time_step or time_frame.
Either it goes too slow, and if you increase the speed the waves increase in size expotentially,
Or the waves propagation is nearly non-existant, and fade to nothing before hitting the walls.

The core algorithm is just so finely tuned, and the documentation its based on is rather vague
in regards to timing, hell, it doesnt really mention timing at all.

PS: The core loop of the "fluid_action" code Im working with is still the same as my last code-post.
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/08/09 21:14

Hmm.. I'm trying to get it working with newton ATM so don't have much time for this. Newton will be released tomorrow sometime.

What have you got so far? all i can see is the 'wait' instruction, which is the only FPS independent instruction.

About the documentation:
Yeah, the documentation has nothing about timing..But it's the best i could find.
Posted By: Germanunkol

Re: Fluid Dynamics for lower editions - 07/09/09 08:13

I still think you should use sin curves.
In physics class, to show/define waves, you use sin curves, even (or especially) for water-waves.
it would be something like:

var startTime; //set this at the total_ticks value when the wave is started
VECTOR* startpos; //set this to the position of the
var waveSpeed = 1; //(speed of propagation? is that what you call it?)
var myDistance; //distance from this vertex to startpos (with vec_dist)
while((total_ticks-startTime)*waveSpeed < myDistance)wait(1); //wait until the wave has reached this vertex.
startTime = total_ticks; //now start time is the time at which it starts to move
my_vertex_height = max_height*sin(2*Pi*(total_ticks-startTime)/waveSpeed);

max_height would need to decrease, of course.

Also, this is (obviously, it's only for one vertex) just pseudo-code, but something like this could work for making it fps independent, because the wave would spread at an even rate.
There must be a reason why physicist use sin functions for waves.

[edit] tested the fluidsimdemo1. It's much better, and the pullling with your mouse to get the effect idea was a very good one!
When clicking on one of the vertecies on the edge it doesn't come back down though, and, as Vet said, it's still not FPS independent, making it "jitter" much too fast on faster computers.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/09/09 08:22

Really, using sin() function is good idea smile
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/09/09 08:40

Quote:
It's much better, and the pullling with your mouse to get the effect idea was a very good one!

That was me smile
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/09/09 08:44

With the newton demo in development,

Do you think i should add particle effects for splashes when vertice height gets above a certain value? Or should this be in a separate demo?
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/09/09 08:48

I think you may add particles, why not? smile
Maybe, also, it would be nice if they would be optional.
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/09/09 10:29

Quote:
Maybe, also, it would be nice if they would be optional.

Good idea! I'll do that.
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/09/09 10:45

I dont know if Im going to be able to time-step this.
To get it FPS independant I need to understand whats going on,
and I no-longer do...

This code is actually setting every vertex back to zero every second frame.
It doesnt LOOK like it, and its thrown me a serious curve-ball..
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/09/09 10:48

EvilSOB, imho, you should go and rest this evening smile
Maybe some(well, "some" could be changed to "a lot of") beer with friends, or romantic evening with girlfriend wink
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/09/09 11:09

I cant, Im at work. Damned night-shift...

(If youve read the web-page from meh_master that he based his code on),
Im thinking of dumping its entire process, except the
vertices<-->neighbouring-vertices averaging filtering, and build up from that.
It seems (after playing a bit) to be the important part.

Germanunkol: this "averaging" filter bypasses the need for the complexities surrounding the usage of SIN-type functions.
Using SIN functionality drags it too close to a REAL water simulator. And that just gets too CPU hungry.
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/09/09 11:36

Quote:
Germanunkol: this "averaging" filter bypasses the need for the complexities surrounding the usage of SIN-type functions.
Using SIN functionality drags it too close to a REAL water simulator. And that just gets too CPU hungry.

True. The reason i chose this method is because of its (blush) simple method and speed compared to SIN based methods.
Turns out that it let us down on the 'simple' bit..
Posted By: MMike

Re: Fluid Dynamics for lower editions - 07/12/09 00:29

i would like to see w youtube demo.. anyone?
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/12/09 01:00

Quote:
i would like to see w youtube demo.. anyone?

There will be youtube when all is finished. For now, you can have a play with the first demo.
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/12/09 06:36

Newton interface is finished. I won't post a download link just yet but i'll give the procedure on how add fluid dynamics to a newton level (that already has a buoyancy plane):
  • In WED, place a .hmp right on top of the buoyancy plane. The .hmp MUST have a z coordinate of 0. If it doesn't, you must move the entire level so it does. Assign the 'fluid_action' action.
  • If you do not have WED, just place it in script, and assign the 'fluid_action' action.
  • in the fluid's action, assign the 'viscoscity'(0-100). You must also assign the 'surface_tension' (0-50) which specifies how deep an object has to be for the water to stop reacting to it.
  • An entity pointer called 'waterent' must be set to the fluid .hmp .
  • You must copy and paste a simple 10 lines of code (15 with particles) at the end of any object that you want the water to react to's action.
  • Make the existing buoyancy plane invisible
  • Run the main script!

It may sound quite complicated, but it's actually pretty simple.

I know this won't help to implement in your own projects yet, it's just to give an idea. I will release everything tomorrow (unless i go to a friends place) Promised!

The reason it took so long was that i was over complicating the action assigned to newton bodies. It was originally a complicated ~50 lines, but now is an easy-to-understand-even-for-newbies 10.

If I don't get any replies in my 'water particle bitmaps' thread, i won't add particles to this release.

@evilSOB: Making any progress? You haven't posted here for a couple of days..
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/12/09 14:02

Still zero progress, but I havent given up either.

[EDIT] Getting close though. I should have a result within hours.
Frame-rate independance - FIXED (WhooHoo grin ).
Viscosity - Progressing well.
Auto-viscosity Calculation - Unlikely to be hard.

Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/12/09 21:44

Quote:
[EDIT] Getting close though. I should have a result within hours.
Frame-rate independance - FIXED (WhooHoo).
Viscosity - Progressing well.
Auto-viscosity Calculation - Unlikely to be hard.

Awesome! I hope you succeed!

Did you end up rewriting the code, or did you edit the existing program?

Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/12/09 21:50

Dammit. I forgot that posts expire after 1 week, so i can't change the front page.

VeT is a mod.. Could you please change the percentage of completion for frame rate independancy to 'DONE'? that would be much appreciated.
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/12/09 22:05

Re-write was a write-off. The whole concept is just not suitable for time-scaling.
(With my limited brainpower anyway, too long since school for this aging old brain)

Heres what Ive got so far.
Its basically done, visvosity works well,
but I just cant get the size-scale-factor to work right.
That is, getting the ripples to "spread" out faster or slower.

Oh, yeah. Looking at the code code, you'll see a new line of array elements
being added to tmp. The 'original' code you gave me calculated this
average based on the 4 up-down-left-right vertices.
My additional line now takes the diagonal vertices into account too.
Give the ripples a much more circular pattern...

Feel free to play and comment or suggest.
Any questions welcome too.
Im going to rest from this problem for a few hours, but Im still around.
Enjoy.
Code:
#include <acknex.h>
#include <default.c>


#define	scale		skill1		//INPUT/OUTPUT  - scale of the terrain
#define	viscosity		skill2		//INPUT/OUTPUT  - "density" of the fluid.  1=Water, 2=EngineOil, 3=Honey
#define	vertex_wide	skill3		//OUTPUT - width of HMP in vertices
#define	vertex_high	skill4		//OUTPUT - height of HMP in vertices

action fluid_action()
{
	//validate if a valid entity for fluid simulation (must be a terrain)
	if(!ent_status(my,2)||!ent_status(my,3))	return;		//this is NOT a terrain.
	//create and initialise "workspace" variables & pointers
	CONTACT* c;		
	var x, y, f, tmp, frametime=0;
	var size_x=ent_status(my,2)+1, size_y=ent_status(my,3)+1;
	//create and initialise "workspace" arrays
	var ***data = malloc(sizeof(var)*2);
	for(f=0; f<2; f++)	
	{	data[f] = malloc(sizeof(var)*size_x);
		for(x=0; x<size_x; x++)	
		{	(data[f])[x] = malloc(sizeof(var)*size_y);
			for(y=0; y<size_y; y++)		((data[f])[x])[y]  = 0;
	}	}
	//setup feedback skills
	if(!my.scale)	
	{
		//auto-calculate  scale based on VETs idea.
		c = ent_getvertex(my,NULL,1);
		x=c.v.x;	y=c.v.z;
		c = ent_getvertex(my,NULL,2+size_y);
		x-=c.v.x;	y-=c.v.z;
		my.scale = (abs(x) + abs(y))/2/2.54;	//quants per "square"  (2.54qu = scale of 1.0)
	}
	if(!my.viscosity)	my.viscosity = 1;		//default to "water" viscosity
	my.vertex_wide  = size_x;
	my.vertex_high	= size_y;
	//
	var sim_fps	= 10;	//Simulation FPS. AVOID changing if possible, because
	//					//if changed, it will skew BOTH Scale & Viscosity ranges
	//
	while(me)
	{	
		my.scale=clamp(my.scale,0.5,50);
		frametime += time_frame;
		if(frametime>(16/sim_fps*my.scale*2))
		{
			frametime -= (16/sim_fps*(my.scale*2));
			f = !f;
			my.viscosity=clamp(my.viscosity,0.9,5);
			for(y=1; y<(size_y-1); y++)   for(x=1; x<(size_x-1); x++)   
			{	
				c = ent_getvertex(my,NULL,(y*size_x)+x+1);    
				if((c.v.y!=((data[0])[x])[y])&&(c.v.y!=((data[1])[x])[y]))
				{	((data[f])[x])[y] = ((data[!f])[x])[y] = c.v.y;
					c = ent_getvertex(my,NULL,((y--)*size_x)+(x--)+1);		}
				tmp  = (((data[!f])[x-1])[y]+((data[!f])[x+1])[y]+((data[!f])[x])[y-1]+((data[!f])[x])[y+1]);
				tmp += (((data[!f])[x-1])[y-1]+((data[!f])[x-1])[y+1]+((data[!f])[x+1])[y-1]+((data[!f])[x+1])[y+1])/2;
				((data[f])[x])[y] = ((tmp/4*(my.scale))-((data[f])[x])[y])*(0.3*(3/my.viscosity));
				((data[f])[x])[y] = c.v.y = integer(((data[f])[x])[y]*10000)/10000;	//idle-damping
				ent_setvertex(my,c,(y*size_x)+x+1);
			}
		}		
		wait(1);
	}	
	//cleanup "workspace" arrays
	for(x=0; x<size_x; x++)	{   free((data[0])[x]);   free((data[1])[x]);   }
	free(data[0]);		free(data[1]);	 	free(data);	 
}



ENTITY* waterent;

void poke()
{
	CONTACT* c = ent_getvertex(waterent,NULL,210);    
	c.v.y += 5;
	ent_setvertex(waterent,c,510);
}


function main()
{
	video_mode = 10;
//	video_screen = 1;
	level_load("");
	vec_set(camera.x,   vector(0,25,25));
	vec_set(camera.pan, vector(270,-45,0));
	waterent = ent_create("waterent.hmp", nullvector, fluid_action);

	on_cuu = poke;

}
//


Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/12/09 22:10

Wow, waves look much better! I'll wait until the autocalculation is finished before newton implementation.
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/13/09 05:34

Sorry guys, but my last post is the best I can squeeze out of it.
I give up.

But hey, its supposed to be SIMPLE fluid sim isnt it?

Sorry.
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/13/09 05:38

Quote:
Sorry guys, but my last post is the best I can squeeze out of it.
I give up.

But hey, its supposed to be SIMPLE fluid sim isnt it?

Sorry.

You don't have to be sorry at all!
What you did was perfect!
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/14/09 05:13

Hmm.. Not working as expected with the new version, terrain keeps raising exponentially.

I'll just use the old version. Sorry for letting all that hard work go to waste..
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/14/09 05:21

I dont mind. All that hard work was mostly going round in circles and
having the terrain "blow-out" every second modification.
The core algorithm is VERY delicately balanced(pronounced "unstable").

Im rather glad your using the more stable version, seeing as my name is in the credits. grin
Thanks for that.
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/14/09 05:38

There probably won't be a thread in "lite-c contributions" for another week or so. I've still got another few demos to make, and a tutorial.

I can't say 'thankyou' enough for how much work you put into this. It is appreciated. Very much so.

I was wondering.. Gamestudio comes with fluid dynamics in the pro edition. Would this be the same algorithm?
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/14/09 06:18

Unlikely in my opinion, but hey, Who knows?
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/14/09 06:35

I'd like to think that this algorithm is faster than conitec's grin
That would be cool..
Posted By: EvilSOB

Re: Fluid Dynamics for lower editions - 07/14/09 06:52

I think it will be "faster", but seeing as it is less controllable, its not so good.
UNLESS conitec found a workaround for the timing issue.
You never know, they may have some-one who is a better programmer than me.
Possibly... grin
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/14/09 06:56

Quote:
they may have some-one who is a better programmer than me.

maybe..but unlikelygrin
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/14/09 09:42

//VeT is a mod.. Could you please change the percentage of completion for frame rate independancy to 'DONE'? that would be much appreciated.

I changed it on the first page, is there what you want? smile
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/14/09 10:17

Yes, that's perfect. Thanks!
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/14/09 10:37

Okay smile
When there would be released v2 ? smile
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/14/09 10:47

Quote:
When there would be released v2 ?

You mean the newton interface?
I just got some replies in the 'water particle bitmaps' thread. Particles will be in this release. I've decided to release everything (updated demo 1, demo 2, demo 3, tutorial) at once. I expect completion in a few days or so. Sooner if i get my other projects finished. Sorry about the release getting delayed.. New projects keep popping up!

Would you rather i modify your buoyancy demo or make an entirely new one?

[EDIT]Well i have actually already modified your buoyancy demo, i was just wondering whether you'd rather me make a new one.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/14/09 11:41

Oh, if you already updated it, i can't wait to download it smile
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/14/09 22:50

Well if you really want to download it:
LitecNewtonFluidMod
Note that this isn't the newest version, nor the best. It's just the most stable so far.

Just run your buoyancy demo normally. The transparent cube has been replaced with a fluid. At the moment, you cannot change levels and back again, otherwise the fluid will be gone. That is fixed in the unstable version.

[EDIT] You must '0' move the camera to get it into a position where you can see the fluid.
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/15/09 21:39

Any issues/bugs?
Posted By: the_mehmaster

Re: Fluid Dynamics for lower editions - 07/17/09 05:34

Hm.. I guess not.
Posted By: VeT

Re: Fluid Dynamics for lower editions - 07/17/09 09:02

Wait a little: i had reinstalled Windows smile
© 2024 lite-C Forums