I'm creating an organism simulator, and the organism objects are situated inside of a particle simulation. I want the organisms to be able to 'sense' the particles.
The particles exist inside of a 1920x1080 array. Currently, the organisms sort of 'draw' lines out from their current position, checking for non-null values in the array, and if any of the lines it draws intersect with a particle inside of the particle array, it senses the particle.
Here's an example of how it looks (green particles are detected by the organism):
This method of sensory input is extremely realistic, as the probability of particle detection decreases as the particles get further away from the organism.
However, there's one huge problem. It's extremely CPU-intensive, and it runs slowly, because there are hundreds of organisms in a simulation at once, all performing the same CPU-intensive scanning process.
This is the code I'm currently using to draw lines and check for non-null points in the particle array (this code is called several times per simulation frame):
float distance = 100.f;
float angle = 2.f*M_PI*randomNumber;
for (int i = 0; i < distance; i++) {
this->point->addVectorFromAngle(angle, 1.1f); //this gets the next point in the line
int x = static_cast<int>(this->point->x);
int y = static_cast<int>(this->point->y);
if (mainController->spaceTaken[x][y] != NULL) {
return mainController->spaceTaken[x][y]; // Particle detected
}
}
//this is a member function of the Vector class I use to handle co-ordinates
void addVectorFromAngle(float angle, float magnitude) {
this->x += cosf(angle)*magnitude;
this->y += sinf(angle)*magnitude;
}
Is there a way to make this code use less CPU while maintaining roughly the same behaviour?