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

Author Topic: Collision dont work exactly  (Read 1515 times)

0 Members and 1 Guest are viewing this topic.

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Collision dont work exactly
« on: July 18, 2022, 12:33:45 pm »
Hello,
I have a collision question, the following code works, but when the player come the wall closer, the wall attracts the player like a magnet, can someone tell me how to fix it?

for (auto& wall : walls){
       
        FloatRect wallBounds = wall.getGlobalBounds();
        FloatRect playerBounds = player.getGlobalBounds();
       
nextPos = playerBounds;
nextPos.left += Vec.x ;
nextPos.top += Vec.y;
       
        if(wallBounds.intersects(nextPos)){
                       
        //Bottom Collision
 if (Vec.y > 0 && playerBounds.left < wallBounds.left + wallBounds.width && playerBounds.left + playerBounds.width > wallBounds.left )
         {
        Vec.y = 0;
          player.setPosition(playerBounds.left, wallBounds.top - playerBounds.height);
          }
        //Top Collision
if(Vec.y < 0 && playerBounds.left < wallBounds.left + wallBounds.width && playerBounds.left + playerBounds.width > wallBounds.left)
          {
                Vec.y = 0;
                player.setPosition(playerBounds.left, wallBounds.top + wallBounds.height);
            }  
 //Right Collision
if(Vec.x > 0 && playerBounds.top < wallBounds.top + wallBounds.height && playerBounds.top + playerBounds.height > wallBounds.top)
                   {
                  Vec.x = 0;
                 player.setPosition(wallBounds.left - playerBounds.width, playerBounds.top);
                   }
//Left Collision
if (Vec.x < 0 && playerBounds.top < wallBounds.top + wallBounds.height && playerBounds.top + playerBounds.height > wallBounds.top)
                      {
                      Vec.x = 0;
player.setPosition(wallBounds.left + wallBounds.width , playerBounds.top);

                      }
           
      }
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Collision dont work exactly
« Reply #1 on: July 21, 2022, 11:18:55 am »
Would require some more mental effort to fully understand your code.

The usual issue in such a case is, that you're not correctly reacting to the collision.
Once two object overlap, i.e. collide, then you need to correctly handle the moving of the two objects into a state where they aren't overlapping anymore. If you don't do that in the right way, the objects end up permanently "glued" together.
Also if you do that check too early, it can feel like whenever you get close that they "attract" each other, so make sure you're not doing the check when they aren't yet colliding.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Collision dont work exactly
« Reply #2 on: July 21, 2022, 11:23:17 am »
If it helps I wrote a post some time back which covers basic collision detection with SFML: https://trederia.blogspot.com/2016/02/2d-physics-101-pong.html