So I'm running a particle simulation where all particles orbit an object but there are some issues with the speed of my code, I did some testing and found two parts of my code which have issues, 1. my distance equation function, and 2. my force calculator
double distance(sf::Vector2f position1, sf::Vector2f position2) {
float dx = position1.x - position2.x;
float dy = position1.y - position2.y;
return sqrt(pow(dx, 2) + pow(dy, 2) * 1.0);
}
sf::Vector2f gravitate(float fa, sf::VertexArray* blackHoles, sf::Vertex point, sf::Vector2f velocity) {
for (int i = 0; i < (*blackHoles).getVertexCount(); i++) {
float dist = distance(point.position, (*blackHoles)[i].position);
float force = fa / (std::pow(dist, 2));
velocity += ((*blackHoles)[i].position - point.position) * force;
}
return velocity;
}
?