Basic particle simulation using SDL

Hey, so I'm making just a very basic (and not particularly realistic!) particle simulation. The idea is I want to add a much larger 'particle' i.e. one big circle which again moves randomly around. The way I stop the little particles colliding so far is using booleans and so I was wondering how I could set all the pixels within the larger circle to 'true' using what I already have. Any help would be much appreciated!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <sstream>
#include <vector>

#include "Graphics.hpp"
											
Graphics *gfx;
std::vector<int> x_disp;
std::vector<int> y_disp;
int n_particles=1000;
int width=800;
int height=600;

std::vector<bool> look_up(width*height,false);

void draw_pixels() {
	gfx->clearScreen(gfx->BLACK);
	for (int i=0; i<n_particles; i++) {
		gfx->setPixel(x_disp[i],y_disp[i],gfx->WHITE);
	}
	gfx->flipScreen();
}


int main() {
	srand(time(NULL));
	// initialise graphics

	gfx=new Graphics();
	gfx->init(width,height);

	bool closed=false;
   SDL_Event event;

	// set initial positions
	for (int i=0; i<n_particles; i++) {
		x_disp.push_back(rand()%800);
		y_disp.push_back(rand()%600);
		look_up[x_disp[i]*y_disp[i]]=true;
		// set relevant index in the lookup array to true 
	}

		//int rr = 20;
		//gfx->circle(x_circle, y_circle, rr, gfx->GREEN); //Here is where I want to make a circle where all the pixels contained within are set to true
																			// so that the other little pixel 'particles' don't enter it.


	// enter the event loop
	while(!closed) {

		// small delay
		gfx->delay(10);
		draw_pixels();
		
		for (int i=0; i<n_particles; i++) {
			if (y_disp[i]<0) {						//Checks for wrap around
				y_disp[i] +=height;
			}
			if (y_disp[i]>height-1) {				//''
				y_disp[i] -= height;
			}
			if (x_disp[i]<0) {						//''
				x_disp[i] += width;
			}
			if (x_disp[i] > width-1) {				//''
				x_disp[i] -= width;
			}
		}

		// update positions
		for (int i=0; i<n_particles; i++) {
			// choose random direction
			int dir=rand()%4;
			switch(dir) {
				case 0 : // N
					if (look_up[x_disp[i]*(y_disp[i]--)]=true) {
						break;
					}
					else {
						look_up[x_disp[i]*y_disp[i]]=false;
						y_disp[i]--;
						look_up[x_disp[i]*y_disp[i]]=true;
						break;
					}	
				case 1 : // E
					if (look_up[(x_disp[i]++)*y_disp[i]]=true) {
						break;
					}
					else {
						look_up[x_disp[i]*y_disp[i]]=false;
						x_disp[i]++;
						look_up[x_disp[i]*y_disp[i]]=true;
						break;	
					}	
				case 2 : // S
					if (look_up[(x_disp[i])*(y_disp[i]++)]=true) {
						break;
					}
					else {
						look_up[x_disp[i]*y_disp[i]]=false;
						y_disp[i]++;
						look_up[x_disp[i]*y_disp[i]]=true;
						break;
					}		
				case 3 : // W
					if (look_up[(x_disp[i]--)*(y_disp[i])]=true) {
						break;
					}
					else {
						look_up[x_disp[i]*y_disp[i]]=false;
						x_disp[i]--;
						look_up[x_disp[i]*y_disp[i]]=true;
						break;
					}		
			}
		}
		while(SDL_PollEvent(&event)) {
			switch (event.type) {

				// react to the QUIT event (i.e. closing the window)
				case SDL_QUIT:
					closed = true;
					break;
			}
		}
	}

	// clean up graphics
	delete gfx;

	return 0;
}
Last edited on
Topic archived. No new replies allowed.