I’m trying to make a little physics engine and this is my function:
void handleCollisions(std::vector<Rigidbody>& rigidbodies, float collisionThreshold, float deltaTime) {
// Iterate over all pairs of rigid bodies
for (Rigidbody& bodyA : rigidbodies) {
for (Rigidbody& bodyB : rigidbodies) {
std::cout << bodyB.getPosition().y;
}
}
}
This function is called every frame. I’ve added an std::cout there. And it prints -510 at first then just prints “-nan(ind)”. The y position of the rigidbody is going down every frame to simulate gravity. It gives the correct number when i put the std::cout in the main loop like this though:
while (!glfwWindowShouldClose(window))
{
rigidbody.update(deltaTime);
boxRigidbody.update(deltaTime);
std::cout << bodyA.getPosition().y;
//this is where i call the handleCollisions function
}
I’ve tried replacing the for loops with this:
for (size_t i = 0; i < rigidbodies.size(); ++i) {
for (size_t j = i + 1; j < rigidbodies.size(); ++j) {
but it just prints 10 even though the y position is going down every frame. How can I fix this?
1