Gravity: only calculate the difference

This commit is contained in:
Simon Robertshaw 2011-04-26 16:15:23 +01:00
parent 776ae73f8e
commit 061d6ba7af
2 changed files with 28 additions and 18 deletions

View File

@ -43,6 +43,8 @@
#define MAX_DISTANCE sqrt(pow(XRES, 2)+pow(YRES, 2)) #define MAX_DISTANCE sqrt(pow(XRES, 2)+pow(YRES, 2))
#define GRAV_DIFF
#define MAXSIGNS 16 #define MAXSIGNS 16
#define TAG_MAX 256 #define TAG_MAX 256

View File

@ -41,6 +41,8 @@ void make_kernel(void) //used for velocity
void update_grav(void) void update_grav(void)
{ {
int x, y, i, j, changed = 0; int x, y, i, j, changed = 0;
float val, distance;
#ifndef GRAV_DIFF
//Find any changed cells //Find any changed cells
for (i=0; i<YRES/CELL; i++) for (i=0; i<YRES/CELL; i++)
{ {
@ -54,26 +56,32 @@ void update_grav(void)
} }
} }
} }
if(changed) memset(th_gravy, 0, sizeof(th_gravy));
{ memset(th_gravx, 0, sizeof(th_gravx));
memset(th_gravy, 0, sizeof(th_gravy)); #endif
memset(th_gravx, 0, sizeof(th_gravx)); for (i = 0; i < YRES / CELL; i++) {
for (i=0; i<YRES/CELL; i++) for (j = 0; j < XRES / CELL; j++) {
{ #ifdef GRAV_DIFF
for (j=0; j<XRES/CELL; j++) if (th_ogravmap[i][j] != th_gravmap[i][j])
{ {
if(th_gravmap[i][j]>0.0001f || th_gravmap[i][j]<-0.0001f) //Only calculate with populated or changed cells. #else
for (y=0; y<YRES/CELL; y++) if ((th_gravmap[i][j] > 0.0001f || th_gravmap[i][j]<-0.0001f) && changed) //Only calculate with populated or changed cells.
{ {
for (x=0; x<XRES/CELL; x++) #endif
{ for (y = 0; y < YRES / CELL; y++) {
if(x == j && y == i)//Ensure it doesn't calculate with itself for (x = 0; x < XRES / CELL; x++) {
continue; if (x == j && y == i)//Ensure it doesn't calculate with itself
float distance = sqrt(pow(j - x, 2) + pow(i - y, 2)); continue;
th_gravx[y][x] += M_GRAV*th_gravmap[i][j]*(j - x)/pow(distance, 3); distance = sqrt(pow(j - x, 2) + pow(i - y, 2));
th_gravy[y][x] += M_GRAV*th_gravmap[i][j]*(i - y)/pow(distance, 3); #ifdef GRAV_DIFF
} val = th_gravmap[i][j] - th_ogravmap[i][j];
#else
val = th_gravmap[i][j];
#endif
th_gravx[y][x] += M_GRAV * val * (j - x) / pow(distance, 3);
th_gravy[y][x] += M_GRAV * val * (i - y) / pow(distance, 3);
} }
}
} }
} }
} }