#Collision detection not working properly
28 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Please send the relevent code
We're all here helping for free :) please make it as easy as possible for us to help
basically this here :
int minX = 0;
int minY = 0;
int maxX = 0;
int maxY = 0;
// check for collisions and render balls
for (size_t i = 0; i < balls.size(); ++i) {
Ball* ball = balls[i];
minX = ball->ballX - 50;
minY = ball->ballY - 50;
maxX = ball->ballX + 50;
maxY = ball->ballY + 50;
//check if there is a collision
if (gun->pelletX >= minX && gun->pelletX <= maxX && gun->pelletY >= minY && gun->pelletY <= maxY) {
ball->isHit = true;
std::cout << "hit!" << std::endl;
}
std::cout << "hit!" << std::endl;
//render balls if there is no collision
if (ball->isHit == false) {
ball->render(game->getRenderer(), ballPositionX[i], ballPositionY[i]);
}
}
Why are you rendering balls if they haven't collided instead of just popping balls off the data structure when they do collide
Your ishit bool already marks them as deletable
yeah ok
yeah i'd probably do an erase if loop here
So unless you care about their order you can just copy the back element into the deleted element and then call pop-back()
ok
In the update loop
i don't care about order
Then yeah
no
it's a std::vector<Ball*> balls;
for (int i = 0; i < objects.size(); ++i) {
if (objects[i].is_dead()) {
objects[i] = objects.back();
objects.pop_back();
}
}```
Smth like this
Are you using ball pointers for inheritance
yes
Oki