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

Author Topic: [Beginner] Help with Bouncing Circles  (Read 45207 times)

0 Members and 1 Guest are viewing this topic.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #15 on: February 02, 2014, 02:58:53 pm »
You forgot to move ball so it doesn't collide anymore. For example ball at {x:50 y:0} and radius 10 should be moved to {x: 50 y:10}.
Okay, but what can I do to solve this Problem? First I set every x and y Speed to over 50, but that didnt work. So I think I misunderstood you  :-\

Which Values do I need to change?

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #16 on: February 02, 2014, 03:54:54 pm »
I meant position. If RightBorderTouching returns true, you should move the ball and then change velocity.
SFML.Utils - useful extensions for SFML.Net

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #17 on: February 02, 2014, 06:10:49 pm »
Wow, thanks krzat!   :)

I modified my code and now it looks like this:
for(int i = 0; i < 10; i++  )
    {
        if(RightBorderTouching(Ball[i].getPosition().x)) {Ball[i].move( -ballradius * dt, 0 * dt); xSpeed[i] = -xSpeed[i];}
        if(LeftBorderTouching(Ball[i].getPosition().x)) {Ball[i].move(  ballradius * dt, 0 * dt); xSpeed[i] = -xSpeed[i];}
        if(TopBorderTouching(Ball[i].getPosition().y)) {Ball[i].move(  0 * dt, ballradius * dt); ySpeed[i] = -ySpeed[i];}
        if(BottomBorderTouching(Ball[i].getPosition().y)){Ball[i].move( 0 * dt, -ballradius * dt); ySpeed[i] = -ySpeed[i];}
    }

I think the Problem is gone now!

(If I did something wrong please tell me  ;) )

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #18 on: February 04, 2014, 09:36:38 pm »
Ok, today I finally got time to continue working on my project.

I created a bool to check for collisions:
bool Collision(const float& TempPositionY1, const float& TempPositionY2, const float& TempPositionX1, const float& TempPositionX2)
    {
        int distance = ((TempPositionX1 - TempPositionX2) * (TempPositionX1 - TempPositionX2)) + ((TempPositionY1 - TempPositionY2) * (TempPositionY1 - TempPositionY2));
        if (distance < ballradius + ballradius )
        {
            return true;
        }
        else return false;

    }

Okay, I think this should work.
But now I have to create a loop to check all possible combinations of Collisions.

I created something like that first:
for (int i = 0; i < ballcount; i++)  
{  
    for (int j = i + 1; j < ballcount; j++)  
    {  
        if (Collision(Ball[i].getPosition().y, Ball[j].getPosition().y, Ball[i].getPosition().x, Ball[j].getPosition().x))
        {
            // Calculate new direction
        }
    }
}

But that doesnt detect all the collisions, for example Ball[4] and Ball[8].

So my question is:

How can I effectively cover all the possible Collisions in my loop?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Beginner] Help with Bouncing Circles
« Reply #19 on: February 04, 2014, 09:51:56 pm »
You forgot to square the radiuses.

Short tip: You should abstract your code to make it much more readable.
How I would write the collision:
float SquaredLength(sf::Vector2f vector);
float Square(float value);

// 1. vectors instead of floats
// 2. pass by value instead of reference (for floats for sure, for vectors it's arguable)
// 3. shorter names (lhs, rhs are conventions for "left/right hand side")
bool Collision(sf::Vector2f lhs, sf::Vector2f rhs)
{
    // 4. reuse existing functions
    // 5. instead of      if (...) return true; else return false;
    //         write      return ...;
    return SquaredLength(lhs - rhs) < Square(2*ballRadius);
}

If you deal a lot with vector operations, you could reuse the functions I wrote inThor.Vectors. You don't need to build Thor for that, the Vectors module is header-only and can be used directly.
« Last Edit: February 04, 2014, 10:04:27 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #20 on: February 05, 2014, 03:26:41 pm »
Thanks, Nexus!

But when I try to compile my code, it gives me these two errors: undefined reference to `SquaredLength(sf::Vector2<float>)', and
 undefined reference to `Square(float)'.

Heres my if-statement:
    sf::Vector2f lhs(Ball[1].getPosition().y, Ball[1].getPosition().x);
    sf::Vector2f rhs(Ball[2].getPosition().y, Ball[2].getPosition().x);
    if (Collision(lhs,  rhs))
        {

        }

What did I do wrong?
« Last Edit: February 05, 2014, 05:55:47 pm by Cadisol87 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10828
    • View Profile
    • development blog
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #21 on: February 05, 2014, 05:20:56 pm »
You need to implement SquaredLength and Square of course. Nexus just gave the deceleration of the function. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #22 on: February 05, 2014, 05:22:35 pm »
You did not implement the functions.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #23 on: February 05, 2014, 06:44:49 pm »
Im not getting it...
I declared Squared Length and Square at the Beginning:
    float SquaredLength(sf::Vector2f vector);
    float Square(float value);

Heres my Collision Bool:
  bool Collision(sf::Vector2f lhs, sf::Vector2f rhs)
{
    return SquaredLength(lhs - rhs) < Square(2*ballradius);
}
 

And heres my if-statement:
    sf::Vector2f lhs(Ball[1].getPosition().y, Ball[1].getPosition().x);
    sf::Vector2f rhs(Ball[2].getPosition().y, Ball[2].getPosition().x);
    if (Collision(lhs,  rhs))
        {
        }

Okay, and what has to be implemented now?
For me, this seems very plausible.

Oh god, im feeling stupid right now...



Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #24 on: February 05, 2014, 06:57:26 pm »
You have declared the signatures of the functions but you have not implemented them.
They are not pre-existing functions. You have to write their actual implementation yourself.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #25 on: February 05, 2014, 08:00:13 pm »
Ahh, thanks Jesper, now I understand...  :)



For me!  :D

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #26 on: February 05, 2014, 10:21:07 pm »
Okay, I've got a function for calculating the Square:
float Square(float value)
{
    return value * value;
}
 
It think this should work.

But what is the Squared lenght?
Which lenght do you mean?
How do I have to calculate that?

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #27 on: February 06, 2014, 12:32:15 am »
The squared length is what you get from the dot product of a vector with itself. Its an optimization to not take the squareroot of it when you just need it for comparing it with another length and just square that length instead.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Beginner] Help with Bouncing Circles
« Reply #28 on: February 06, 2014, 09:52:30 am »
As stated earlier:
Quote
If you deal a lot with vector operations, you could reuse the functions I wrote in Thor.Vectors. You don't need to build Thor for that, the Vectors module is header-only and can be used directly.

You can also have a look at the documentation and/or implementation in order to understand what these functions do. But they require some basic knowledge about vector algebra.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #29 on: February 06, 2014, 06:09:25 pm »
Okay, but where can I find the Header for SFML 2.1?