1
General / How to handle collision for multiple objects efficiently ?
« on: August 21, 2017, 01:45:35 am »
Hey I'm currently making an arkanoid clone, I'm not very experienced with C++ and just started with SFML so I think I'm not doing the right thing here.
My ball is checking for collision in it's update function (that handles movement and collisions.)
I've excluded the movement, here's the collision with the paddle bit :
Now this works but the problem is that I have to give the player's bounding box to the function, It's not a problem as of right now but since there's dozens of boxes in Arkanoid I'll have to feed dozens of boxes to the function.
My thought was to store every box in a vector and iterate over it to check if the ball is colliding with a box inside the vector.
That's a fix but it doesn't look very efficient, is there a way to make it better ?
Thanks to those who will help.
My ball is checking for collision in it's update function (that handles movement and collisions.)
I've excluded the movement, here's the collision with the paddle bit :
void Ball::update(float deltaTime, Paddle &paddle)
{
sf::FloatRect playerBox(paddle.getBoundingBox()); // Paddle bounding box
m_boundingBox = m_shape.getGlobalBounds(); // ball bounding box
// Collision check with paddle
if (m_boundingBox.intersects(playerBox))
{
std::cout << "Colliding" << std::endl;
}
}
{
sf::FloatRect playerBox(paddle.getBoundingBox()); // Paddle bounding box
m_boundingBox = m_shape.getGlobalBounds(); // ball bounding box
// Collision check with paddle
if (m_boundingBox.intersects(playerBox))
{
std::cout << "Colliding" << std::endl;
}
}
Now this works but the problem is that I have to give the player's bounding box to the function, It's not a problem as of right now but since there's dozens of boxes in Arkanoid I'll have to feed dozens of boxes to the function.
My thought was to store every box in a vector and iterate over it to check if the ball is colliding with a box inside the vector.
That's a fix but it doesn't look very efficient, is there a way to make it better ?
Thanks to those who will help.