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

Author Topic: why does my ball keep getting stuck together when my ball bounce into each other  (Read 310 times)

0 Members and 1 Guest are viewing this topic.

R4nd0mP3rSon

  • Newbie
  • *
  • Posts: 15
    • View Profile
void ball::update( sf::RenderTarget *target,ball *Ball,int i,int k)
{

    this->updateAccel();

    this->dx = (this->balls.getRadius() + this->balls.getPosition().x) -
         (Ball->getRadius() + Ball->getBounds().x);
    this->dy = (this->balls.getRadius() + this->balls.getPosition().y) -
         (Ball->getRadius() + Ball->getBounds().y);
    this->distance = sqrt((this->dx * this->dx) + (this->dy * this->dy));

    if(i != k) {
        if (this->distance <= this->balls.getRadius() + Ball->getRadius()) {
            this->velocity *= -1.f;

            std::cout << "Collision!" << "\n";
            return;
        }
        return;
    }


    this->updateCollision(target);


}


for(int i = 0; i < this->balls.size();i++) {
        for (int k = 0; k < this->balls.size(); k++) {
                this->balls[i].update(this->window, &this->balls[k],i,k);
        }
    }
« Last Edit: January 15, 2024, 02:06:12 pm by eXpl0it3r »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
When they collide, they aren't being moved so that they no longer collide; the velocity is changed but not the position. This means that during the next update, they will still be colliding even if they are "about to" move away but this will cause them to move in opposite direction again (the original direction that caused them to collide in the first place) and this gets repeated.

The solution is to make sure that you respond to the collision by changing their position so that they no longer collide and not just change their velocity.
A simple way to do this is to simply add the velocity to its position immediately after changing it.

Note that if there are many circles, they may get stuck between multiple circles and can no longer just be moved out of the way on their own; they would all need to be moved to allow them to fit without colliding!
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

R4nd0mP3rSon

  • Newbie
  • *
  • Posts: 15
    • View Profile
Thanks it worked now. but do you know if there's
a way to detect the collision only once ?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Probably the best solution is just move it away so it no longer collides. It shouldn't be penetrating anyway, right?

Still, another option is to have a flag that triggers (sets) when it collides. Then, when you test for the next collision, if the flag is set, don't change its velocity. If it is no longer colliding, reset the flag.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*