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

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

0 Members and 3 Guests are viewing this topic.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #30 on: February 06, 2014, 08:49:14 pm »
Success!

Okay, I downloaded the Thor library and searched for the SquareLenght function. I found it and Implemented it.
So heres my complete Collision detection:
float dotProduct(sf::Vector2f lhs, sf::Vector2f rhs)
{
        return lhs.x * rhs.x + lhs.y * rhs.y;
}
float Square(float value)
{
    return value * value;
}

float SquaredLength(sf::Vector2f vector)
{
   return dotProduct(vector, vector);
}
bool Collision(sf::Vector2f lhs, sf::Vector2f rhs)
{

    return SquaredLength(lhs - rhs) < Square(2*ballradius);
}
 

Thank you Nexus, for making such an awesome library!
« Last Edit: February 06, 2014, 08:52:08 pm by Cadisol87 »

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #31 on: February 07, 2014, 01:22:38 pm »
Now I got a question about calculating the new velocities and directions.

I read the tutsplus article (http://gamedevelopment.tutsplus.com/tutorials/when-worlds-collide-simulating-circle-circle-collisions--gamedev-769) , but the comments say that the method to calculate new directions and velocities is wrong.

So how should I calculate the collisions now?

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #32 on: February 08, 2014, 09:57:25 am »
Has anyone an Idea?

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #34 on: February 08, 2014, 02:44:20 pm »
Have you tried the techniques in the gamasutra article I linked you to in my first reply in this thread?
I've used what's described in that article myself to calculate collisions between circles and circles and circles and straight surfaces, calculate the bounce and speed after collision etc. and it works very nicely.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #35 on: February 08, 2014, 04:35:52 pm »
Okay, I tried to Implement the Collision-Resolving.
But there's a problem: When the balls collide, they stick at each other and don't bounce away.

Here's some Code:
int ballradius = 50;
int mass = ballradius;

float newVelY1(float yVel1, float yVel2)
{
     float NEWyVel1 = (yVel1 * (mass - mass) + (2 * mass * yVel2)) / (mass + mass);
     return NEWyVel1;
}

float newVelY2(float yVel1, float yVel2)
{
    float NEWyVel2 = (yVel2 * (mass - mass) + (2 * mass * yVel1)) / (mass + mass);
    return NEWyVel2;
}

float newVelX1(float xVel1, float xVel2)
{
    float NEWxVel1 = (xVel1 * (mass - mass) + (2 * mass * xVel2)) / (mass + mass);
    return NEWxVel1;
}

float newVelX2(float xVel1, float xVel2)
{
    float NEWxVel2 = (xVel2 * (mass - mass) + (2 * mass * xVel1)) / (mass + mass);
    return NEWxVel2;
}

 // In the main-loop:
for (int i = 0; i < ballcount; i++)
{
    for (int j = i + 1; j < ballcount; j++)
    {
        if (Collision(sf::Vector2f(Ball[i].getPosition().y, Ball[i].getPosition().x), sf::Vector2f(Ball[j].getPosition().y, Ball[j].getPosition().x)))
        {


            Ball[i].move(newVelX1(xSpeed[i], xSpeed[j]) * dt,  newVelY1(ySpeed[i], ySpeed[j]) * dt);
            Ball[j].move(newVelX2(xSpeed[i], xSpeed[j]) * dt,  newVelY2(ySpeed[i], ySpeed[j])* dt);


            xSpeed[i] = newVelX1(xSpeed[i], xSpeed[j]);
            xSpeed[j] = newVelX2(xSpeed[i], xSpeed[j]);
            ySpeed[i] = newVelY1(ySpeed[i], ySpeed[j]);
            ySpeed[j] = newVelY2(ySpeed[i], ySpeed[j]);

        }
    }
}
 

Has anybody an idea what I did wrong?


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Beginner] Help with Bouncing Circles
« Reply #36 on: February 09, 2014, 11:00:46 am »
(mass - mass)

Why are you now again using separate coordinates instead of vectors? I think we agreed that vectors would make the code simpler and more readable ;)

And don't duplicate code, you can merge the four functions to one.
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 #37 on: February 09, 2014, 11:46:29 am »
(mass - mass)

Why are you now again using separate coordinates instead of vectors? I think we agreed that vectors would make the code simpler and more readable ;)

And don't duplicate code, you can merge the four functions to one.

Yeah, you're right, but I don't think I have to use vectors, because every ball has the same mass.  :)

But are the calculations right?
I don't understand why the balls stick together...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Beginner] Help with Bouncing Circles
« Reply #38 on: February 09, 2014, 12:01:36 pm »
The very first line in my post! ::)

Of course you don't have to use vectors, but they simplify everything a lot. If you prefer verbose and badly abstracted code, that's your decision.
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 #39 on: February 09, 2014, 04:29:04 pm »
Ok, Nexus, now I defined the mass as a vector.  :)

But how can I solve the sticky-balls problem?


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Beginner] Help with Bouncing Circles
« Reply #40 on: February 09, 2014, 04:56:52 pm »
I think that mass might be the only value that shouldn't be a vector since it's a single value. :p Vectors (in this case) are for positions, velocities etc.

As for your calculations, Nexus pointed out a tiny bit of your code that you should really study:
(mass - mass)
What does it do?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #41 on: February 09, 2014, 06:15:08 pm »
I think that mass might be the only value that shouldn't be a vector since it's a single value. :p Vectors (in this case) are for positions, velocities etc.

As for your calculations, Nexus pointed out a tiny bit of your code that you should really study:
(mass - mass)
What does it do?

Yeah, I thought Nexus meant that I should define mass as a vector... I don't know why is should do this, maybe I misunderstood him  :-\

And the mass - mass thing is from the tutsplus article mentioned earlier. http://gamedevelopment.tutsplus.com/tutorials/when-worlds-collide-simulating-circle-circle-collisions--gamedev-769

Its important when each ball has a different mass.
And yeah, then Vectors would be helpful  ;)

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #42 on: February 09, 2014, 07:35:11 pm »
(mass - mass)

Why are you now again using separate coordinates instead of vectors? I think we agreed that vectors would make the code simpler and more readable ;)

And don't duplicate code, you can merge the four functions to one.

If you are suggesting that mass should be a vector, it can't be as it is a scalar.

However, all of these functions can be combined into a single function and if the vector arithmetic is set up correctly, you do not have to calculate the values of the vectors separately.

Also, beware of changing the velocity of one before you calculate the new velocity of the other. If you do not, then you will not have a correct collision.
« Last Edit: February 09, 2014, 07:39:47 pm by Azaral »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Beginner] Help with Bouncing Circles
« Reply #43 on: February 09, 2014, 07:39:18 pm »
It's mass1 - mass2. It's the difference between the two masses. At the moment (and I didn't think I'd have to point this out explicitly) you are subtracting something from itself and it will always be zero. If the circles were to have different masses, you would use them there, but since they're all the same, you can remove (mass-mass) since it is 0.

The vectors (i.e. sf::Vector2f) are for the positions and velocities so that x and y can be conveniently in one variable. In some cases, calculation can be applied to the vector rather than directly to its components.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #44 on: February 09, 2014, 09:01:56 pm »
Also, beware of changing the velocity of one before you calculate the new velocity of the other. If you do not, then you will not have a correct collision.

I did that, didn't I?  ???

 

anything