Hello everyone, I am currently creating a "Bubble Shooter" like (see
https://bubble-shooter.co/) only with RectangleShapes but I am facing a problem with collision accuracy. In fact, the collision is well detected, but not accurate.
See these images :
https://ibb.co/F0sckGdhttps://ibb.co/MPYMWw5As you can see, collision isn't accurate.
What i've found so far :
Increasing Framerate limit (setFramerateLimit) to something like 600 and decreasing the move speed to 1 fix this issue.
I'm quite new to game development, but I guess that running a game at 600 fps isn't a good idea right ?
It looks like the issue is related to the speed and the direction vector, but cannot find out what's wrong.
Here's some part of my source code.
void Game::processEvent(sf::Event e)
{
if(e.type == sf::Event::MouseButtonPressed && canShoot == true)
{
float mouseX = sf::Mouse::getPosition(gameWindow).x; //Getting mouse position
float mouseY = sf::Mouse::getPosition(gameWindow).y;
if(mouseY < gameSize.y - blockSize.y)
{
blockDirection.x += mouseX - (gameSize.x / 2); //Calculating direction vector (gameSize.x / 2) is the x origin of the projectile.
blockDirection.y += mouseY - (gameSize.y - blockSize.y); //gameSize.y - blockSize.y is the y origin of the projectile
canShoot = false;
}
}
}
void Game::gameUpdate()
{
int id = isGameBlockColliding();
if(id == -1 && canShoot == false)
{
blockDirection = normalizeDirection() * blockSpeed; //When blockSpeed is set to 1, there's no collision issue, but the game is much too slow.
gameBlock.moveBlock(blockDirection);
}
else if(id == -2) //In case we collide with screen borders (only x axis for now)
{
blockDirection.x = -blockDirection.x;
gameBlock.moveBlock(blockDirection);
}
else
{
if(canShoot == false)
{
canShoot = true;
staticGameBlocks.push_back(gameBlock);
if(gameBlock.getBlockColorID() == staticGameBlocks[id].getBlockColorID())
{
staticGameBlocks.erase(staticGameBlocks.begin() + id);
staticGameBlocks.pop_back();
}
spawnRandomBlock();
}
}
}
sf::Vector2f Game::normalizeDirection() //found this method somewhere in the forums
{
float modulus = sqrt((blockDirection.x * blockDirection.x) + (blockDirection.y * blockDirection.y));
if(modulus != 0)
{
return sf::Vector2f(blockDirection.x / modulus, blockDirection.y / modulus);
}
else
{
return blockDirection;
}
}
int Game::isGameBlockColliding()
{
if(!(gameBlock.getBlockPosition().left + gameBlock.getBlockPosition().width < gameSize.x && gameBlock.getBlockPosition().left > 0))
{
return -2;
}
for(int i = 0; i < staticGameBlocks.size(); i++)
{
sf::FloatRect staticRect = staticGameBlocks[i].getBlockPosition();
sf::FloatRect gameBlockRect = gameBlock.getBlockPosition();
if(staticRect.intersects(gameBlockRect))
{
return i;
}
}
return -1;
}
Please let me know if you need more informations in order to help me.
Thank you in advance for any response.
(Sorry if my english was bad.)