Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - R_oot42O

Pages: [1]
1
General / Re: Rectangle bottom collision issues
« on: February 06, 2022, 05:25:01 pm »
Thank you for your reply, i'll check it out.

2
General / Rectangle bottom collision issues
« on: February 06, 2022, 03:40:32 pm »
Hello everyone, I have a problem with bottom collision detection in my project.


I have a function that checks if the bottom of the character collides with a platform, it almost work as wanted, but there is a problem, actually it works only with some platforms but will fail with others.

See the screens below.


Working :


Not working :


On the first screenshot, we can see the collisions fits perfectly, but in the second screen the character is overlapping the platform.


Here's the code for bottom collision detection :

bool Character::isBottomColliding(Platform *platforms)
{
   int i;
   sf::FloatRect characterRect;
   sf::FloatRect platformRect;
   characterRect = getRect();
   for(i = 0; i < LEVEL_MAX_PLATFORMS; i++)
   {
      platformRect = platforms[i].getRect();
      if(characterRect.left + characterRect.width >= platformRect.left && characterRect.left <= platformRect.left + platformRect.width && characterRect.top + characterRect.height >= platformRect.top)
      {
         isGrounded = true;
         isJumping = false;
         return true;
      }  
   }
   return false;  
}

The update function :


void update(Level *level, Character *character)
{
   if(character->falling() && !character->isBottomColliding(level->getPlatforms()))
   {
      character->fall(); //This function is moving character down to simulate gravity
   }
   character->move();
}


I don't understand why it works with the first platforms but not with the other one.


Sorry if my english was bad, this is not my native language.


EDIT : It works fine when gravity is set to 1


void Character::fall()
{
   sprite.setPosition(getRect().left, getRect().top + gravity);
   isFalling = true;
   isGrounded = false;  
}

EDIT 2: I found that when the platform's Y position modulus (%) the gravity equals 0, there is no problem

if platformRect.top % gravity == 0 ==> No collision problems







 

3
Graphics / Re: Collision accuracy issues
« on: April 08, 2020, 01:03:32 am »
Thank you for your detailed answer.

Running the update loop faster than the frame rate and multiplying the direction vector by delta time finally solved my problem.

Have a nice day/night !

4
Graphics / Re: Collision accuracy issues
« on: April 07, 2020, 11:20:38 pm »
Thank you for you answer.

I followed your advice, and moved the blockDirection vector's y component down after collision is detected.

Here's what i've added to my code :

Code: [Select]

//After collision has been checked
gameBlock.moveBlock(sf::Vector2f(blockDirection.x, -blockDirection.y));

The result is illustrated in the picture below :




So now, the projectile isn't going too far, but not far enough, it is stoped before "real" collision.
This means I have to find the right value to move down the block.

I keep searching for it.





5
Graphics / [SOLVED] Collision accuracy issues
« on: April 07, 2020, 07:34:20 pm »
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/F0sckGd

https://ibb.co/MPYMWw5

As 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.)






     
     

Pages: [1]