1
General / Re: Vector of Vectors Collision Detection
« on: May 29, 2018, 04:09:00 am »
Thanks Hapax for helping me out again. I'm watching a youtube tutorial on auto (and glancing at my Stroustrup book), it's simply just ignorance on my part for not knowing useful keywords and how they work. At a glance it just appears to be magic and/or syntax sugar.
I appreciate this advice, and I will try to make this happen.
I have an initializer function (English not c++ lol) that loads "enemies" (rectangle shapes) into a vector of "enemy" type...and once that is loaded, I load that vector into a vector (the vector of vectors). I trimmed out the node functionality (enemy movement). I do realize I need to work on my naming conventions...it is confusing to say the least.
I am passing that vector of vectors by reference to the player bullet "move" function to check for collisions by iterating through all the enemies while the player bullet is "moving". The function BulletEnemyCollision is my original post.
I am gonna take a stab at using auto, thanks again.
Anyway, one thing to consider applying to your function is to get the player bullet rectangle's global boundaries once at the beginning of the function and then use that to compare intersections. This avoids the 'getting' of the bounds for every collision check for every enemy. Note that this isn't a massive deal really but it seems wasteful if there are a lot of enemies to check.
I appreciate this advice, and I will try to make this happen.
What doesn't work in your second code? What is passed into enemyVectors? Should it not be a vector of references instead a vector of elements?
I have an initializer function (English not c++ lol) that loads "enemies" (rectangle shapes) into a vector of "enemy" type...and once that is loaded, I load that vector into a vector (the vector of vectors). I trimmed out the node functionality (enemy movement). I do realize I need to work on my naming conventions...it is confusing to say the least.
void Levels::LevelLoadNodes(Player& pO, Enemy& enemyObject, std::vector<Enemy>& enemy, int numberOfEnemies)
{
enemyObject.loadEnemies(numberOfEnemies, enemy, enemyObject, pO);
enemyVectors.push_back(enemy);
{
enemyObject.loadEnemies(numberOfEnemies, enemy, enemyObject, pO);
enemyVectors.push_back(enemy);
I am passing that vector of vectors by reference to the player bullet "move" function to check for collisions by iterating through all the enemies while the player bullet is "moving". The function BulletEnemyCollision is my original post.
void Bullet::BulletPlayerMove(float delta, std::vector<std::vector<Enemy> >& enemyVectors)
{
for (unsigned int i = 0; i < bulletVector.size(); i++)
{
bulletVector[i].move(cos(bulletAngleVector[i]) * bulletSpeed * delta, sin(bulletAngleVector[i]) * bulletSpeed * delta);
bulletTimeFVector[i] = bulletTimeVector[i].getElapsedTime().asSeconds();
window.draw(bulletVector[i]);
BulletEnemyCollision(bulletVector[i], enemyVectors);
}
}
{
for (unsigned int i = 0; i < bulletVector.size(); i++)
{
bulletVector[i].move(cos(bulletAngleVector[i]) * bulletSpeed * delta, sin(bulletAngleVector[i]) * bulletSpeed * delta);
bulletTimeFVector[i] = bulletTimeVector[i].getElapsedTime().asSeconds();
window.draw(bulletVector[i]);
BulletEnemyCollision(bulletVector[i], enemyVectors);
}
}
I am gonna take a stab at using auto, thanks again.