I am trying to create a physics engine for a custom game engine. At the moment everything works fine however i am having some performance issues when the engine has to deal with approximately 4000 physics bodies. I am quite certain this is not the fault of the render engine as it uses instanced rendering for particle effects (witch i am currently testing) and can handle around 200K particles if they are all static.
so far once all the collisions have been resolved i update all of the physics bodies in the scene by applying a gravity force and translating the bodies by there velocity
the function looks like this:
void mint::physics::PhysicsEngine::SymplecticEuler(mint::physics::PhysicsBody* body)
{
mint::graphics::Entity *entity = body->GetEntity();
// -- Symplectic Euler
glm::vec2 gravity = glm::vec2(0.0f, (1.0f / core::Timer::Instance()->DeltaTime()) * 9.81f) * body->GravityScale();
glm::vec2 dv = (body->Force() * body->GetMassData()->inv_mass + gravity * core::Timer::Instance()->DeltaTime());
body->Velocity(body->Velocity() + dv);
glm::vec2 dxy = glm::vec2(body->Velocity() * core::Timer::Instance()->DeltaTime());
entity->Translate(glm::vec3(dxy, 0.0f));
// -- END -- Symplectic Euler
// -- update the collider
body->UpdateCollider();
// -- END -- update the collider
}
this function will run once per physics body and is called in a for loop like so
auto start = std::chrono::high_resolution_clock::now();
for (auto body : all_bodys)
{
//SymplecticEuler(body);
// -- using std::async
fEulerFutures.push_back(std::async(std::launch::async, SymplecticEuler, body));
//SymplecticEuler(body);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> duration = end - start;
std::cout << "physics update took: " << duration.count() << std::endl;
i am using std::chrono to see how long the update too and i have two different ways of implementing this, one is just by calling SymplecticEuler(body)
and the other way is by using
std::async and that future that is returned from the function is stored in a member vector of the physics engine class witch is cleared once every update
using the timing code i wrote it took the sequential loop 0.00014s and the multithreaded loop took 0.005s. I would not expect the multithreaded loop to take longer then the sequential loop but it did so i am assuming that i am either using std::async wrong or am using it in the wrong context. The program i am running this on is running a simple particle simulation with 300 particles so nothing to big yet.
Can someone please let me know if i am using std::async correctly because i am still very new to the concept of multithreading or if i am using too many threads to slow down the performance of the engine or if i should use compute shaders instead of multithreading (if the use of compute shaders would improve the performance of the engine please leave some links for tutorials of how to use compute shaders in modern openGL with c++)
both these functions are members of a physics engine class and the SymplecticEuler()
function is a static function
Thanks