summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2011-04-22 16:06:09 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2011-04-22 16:06:09 (GMT)
commit13ff21bb228d131b0b48f81ad57629a4d4756bc2 (patch)
tree06ebde58e8e24170c9b1512957c6e27a692b8bc0 /src
parenta75de307820df32119d02715bbc2f474c5ab6fd8 (diff)
downloadpowder-13ff21bb228d131b0b48f81ad57629a4d4756bc2.zip
powder-13ff21bb228d131b0b48f81ad57629a4d4756bc2.tar.gz
Gravitaaaay (Needs tweeking for performance)
Diffstat (limited to 'src')
-rw-r--r--src/air.c47
-rw-r--r--src/graphics.c24
-rw-r--r--src/main.c6
-rw-r--r--src/powder.c6
4 files changed, 81 insertions, 2 deletions
diff --git a/src/air.c b/src/air.c
index 8c11f30..2947d93 100644
--- a/src/air.c
+++ b/src/air.c
@@ -4,6 +4,11 @@
#include <defines.h>
float kernel[9];
+float ogravmap[YRES/CELL][XRES/CELL];
+float gravmap[YRES/CELL][XRES/CELL];
+float gravx[YRES/CELL][XRES/CELL];
+float gravy[YRES/CELL][XRES/CELL];
+
float vx[YRES/CELL][XRES/CELL], ovx[YRES/CELL][XRES/CELL];
float vy[YRES/CELL][XRES/CELL], ovy[YRES/CELL][XRES/CELL];
float pv[YRES/CELL][XRES/CELL], opv[YRES/CELL][XRES/CELL];
@@ -29,6 +34,48 @@ void make_kernel(void) //used for velocity
for (i=-1; i<2; i++)
kernel[(i+1)+3*(j+1)] *= s;
}
+void update_grav(void)
+{
+ int x, y, i, j, changed = 0;
+ //Find any changed cells
+ for (i=0; i<YRES/CELL; i++)
+ {
+ if(changed)
+ break;
+ for (j=0; j<XRES/CELL; j++)
+ {
+ if(ogravmap[i][j]!=gravmap[i][j]){
+ changed = 1;
+ break;
+ }
+ }
+ }
+ if(changed)
+ {
+ memset(gravy, 0, sizeof(gravy));
+ memset(gravx, 0, sizeof(gravx));
+ for (i=0; i<YRES/CELL; i++)
+ {
+ for (j=0; j<XRES/CELL; j++)
+ {
+ if(gravmap[i][j]>0.0f) //Only calculate with populated or changed cells.
+ for (y=0; y<YRES/CELL; y++)
+ {
+ for (x=0; x<XRES/CELL; x++)
+ {
+ if(x == j && y == i)//Ensure it doesn't calculate with itself
+ continue;
+ float distance = sqrt(pow(j - x, 2) + pow(i - y, 2));
+ gravx[y][x] += M_GRAV*gravmap[i][j]*(j - x)/pow(distance, 3);
+ gravy[y][x] += M_GRAV*gravmap[i][j]*(i - y)/pow(distance, 3);
+ }
+ }
+ }
+ }
+ }
+ memcpy(ogravmap, gravmap, sizeof(gravmap));
+ memset(gravmap, 0, sizeof(gravmap));
+}
void update_air(void)
{
int x, y, i, j;
diff --git a/src/graphics.c b/src/graphics.c
index b92f12c..703e972 100644
--- a/src/graphics.c
+++ b/src/graphics.c
@@ -1255,6 +1255,30 @@ void draw_air(pixel *vid)
}
}
+void draw_grav(pixel *vid)
+{
+ int x, y, i;
+ float nx, ny, dist;
+
+ for (y=0; y<YRES/CELL; y++)
+ {
+ for (x=0; x<XRES/CELL; x++)
+ {
+ if(fabsf(gravx[y][x]) <= 0.001f && fabsf(gravy[y][x]) <= 0.001f)
+ continue;
+ nx = x*CELL;
+ ny = y*CELL;
+ dist = fabsf(gravx[y][x])+fabsf(gravy[y][x]);
+ for(i = 0; i < 4; i++)
+ {
+ nx -= gravx[y][x]*0.5f;
+ ny -= gravy[y][x]*0.5f;
+ addpixel(vid, (int)(nx+0.5f), (int)(ny+0.5f), 255, 255, 255, (int)(dist*20.0f));
+ }
+ }
+ }
+}
+
void draw_line(pixel *vid, int x1, int y1, int x2, int y2, int r, int g, int b, int a) //Draws a line
{
int dx, dy, i, sx, sy, check, e, x, y;
diff --git a/src/main.c b/src/main.c
index f1f26bb..bbcd5f5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2869,8 +2869,9 @@ int main(int argc, char *argv[])
memset(vid_buf, 0, (XRES+BARSIZE)*YRES*PIXELSIZE);
}
#endif
+ draw_grav(vid_buf);
- //Can't be too sure...
+ //Can't be too sure (Limit the cursor size)
if (bsx>1180)
bsx = 1180;
if (bsx<0)
@@ -2881,6 +2882,7 @@ int main(int argc, char *argv[])
bsy = 0;
update_particles(vid_buf); //update everything
+ update_grav();
draw_parts(vid_buf); //draw particles
if (cmode==CM_PERS)
@@ -3577,7 +3579,7 @@ int main(int argc, char *argv[])
sprintf(heattext, "Empty, Pressure: %3.2f", pv[(y/sdl_scale)/CELL][(x/sdl_scale)/CELL]);
if (DEBUG_MODE)
{
- sprintf(coordtext, "X:%d Y:%d", x/sdl_scale, y/sdl_scale);
+ sprintf(coordtext, "X:%d Y:%d. GX: %.2f GY: %.2f", x/sdl_scale, y/sdl_scale, gravx[(y/sdl_scale)/CELL][(x/sdl_scale)/CELL], gravy[(y/sdl_scale)/CELL][(x/sdl_scale)/CELL]);
}
}
}
diff --git a/src/powder.c b/src/powder.c
index 7dd1ad8..e697f24 100644
--- a/src/powder.c
+++ b/src/powder.c
@@ -1463,6 +1463,12 @@ void update_particles_i(pixel *vid, int start, int inc)
pGravX = ptypes[t].gravity * ((float)(x - XCNTR) / pGravD);
pGravY = ptypes[t].gravity * ((float)(y - YCNTR) / pGravD);
}
+ //Get some gravity from the gravity map
+ if(!(ptypes[t].properties & TYPE_SOLID))
+ {
+ pGravX += gravx[y/CELL][x/CELL];
+ pGravY += gravy[y/CELL][x/CELL];
+ }
//velocity updates for the particle
parts[i].vx *= ptypes[t].loss;
parts[i].vy *= ptypes[t].loss;