Gravitaaaay (Needs tweeking for performance)

This commit is contained in:
Simon Robertshaw 2011-04-22 17:06:09 +01:00
parent a75de30782
commit 13ff21bb22
6 changed files with 90 additions and 2 deletions

View File

@ -2,6 +2,11 @@
#define AIR_H
#include "defines.h"
extern float ogravmap[YRES/CELL][XRES/CELL];
extern float gravmap[YRES/CELL][XRES/CELL];
extern float gravx[YRES/CELL][XRES/CELL];
extern float gravy[YRES/CELL][XRES/CELL];
extern float vx[YRES/CELL][XRES/CELL], ovx[YRES/CELL][XRES/CELL];
extern float vy[YRES/CELL][XRES/CELL], ovy[YRES/CELL][XRES/CELL];
extern float pv[YRES/CELL][XRES/CELL], opv[YRES/CELL][XRES/CELL];
@ -16,6 +21,8 @@ extern float kernel[9];
void make_kernel(void);
void update_grav(void);
void update_air(void);
#endif

View File

@ -100,6 +100,8 @@ void draw_icon(pixel *vid_buf, int x, int y, char ch, int flag);
void draw_air(pixel *vid);
void draw_grav(pixel *vid);
void draw_line(pixel *vid, int x1, int y1, int x2, int y2, int r, int g, int b, int a);
void addpixel(pixel *vid, int x, int y, int r, int g, int b, int a);

View File

@ -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;

View File

@ -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;

View File

@ -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]);
}
}
}

View File

@ -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;