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

Author Topic: [Beginner] Help with Bouncing Circles  (Read 45131 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 #60 on: February 11, 2014, 04:38:45 pm »
Thanks Golden Eagle, for solving the problem :)

I could have noticed that aswell, I should be more concentrated during programming...  ::)

I made some changes with the ball position:
Ball[i].setPosition( i * 70 ,i * 70);

Now the Balls don't stick together when you start the program.

But I noticed something like a glitch:

When a ball, collides with a wall, and at the same moment another ball collides with him, the ball is being pushed into the wall, and the two balls stick together.

It's hard to explain, if you like, you can just see it by yourself.

I think this problem can't be solved, can it?

It's still not perfect

Why is it still not perfect? Now it's the same code as in the tutorial, isn't it?


Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Beginner] Help with Bouncing Circles
« Reply #61 on: February 11, 2014, 05:32:19 pm »
Thanks Golden Eagle, for solving the problem :)
You're welcome.

But I noticed something like a glitch:
Why is it still not perfect? Now it's the same code as in the tutorial, isn't it?
I think you answered your own question  ;)
It's not perfect because the tutorial isn't perfect. The tutorial shows how to deal with collisions with discs and which angle and velocity they should be after a collision. One of the things it doesn't cover (if I remember correctly) is when the velocity is so large that they simple pass over one another. Obviously, that's covered by testing in the tiniest of increments at a time, which can mean needing to slow down your discs. Try it with small discs that move quickly and they'll just skip over each other  ;)
Also, when the discs go into the window edge, they are just slid back into the window at a tangent of the window's edge rather than calculating where the disc should be after bouncing.
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 #62 on: February 11, 2014, 07:00:28 pm »
Hmmm, and what should I do now?
I didn't know that the tutorial wasn't completely right, I'll have another look at the Gamasutra atricle, although it seems a little bit more compilcated to me.

If anyone has an idea how to solve this, please tell me! :)

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Beginner] Help with Bouncing Circles
« Reply #63 on: February 11, 2014, 07:05:28 pm »
It's not about "solving" it; it's about the level of accuracy you desire. It can't be 100% physically perfect, ever. It's just how close to that you want to go. The closer to perfect you go, the complicated it can become.
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 #64 on: February 11, 2014, 08:01:02 pm »
But there has to be a way that the balls don't glitch, using the current  way to calculate the collision...


Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Beginner] Help with Bouncing Circles
« Reply #65 on: February 11, 2014, 08:19:13 pm »
But there has to be a way that the balls don't glitch, using the current  way to calculate the collision...
Using the same calculations, they will "glitch" the same way.
I think, instead of "glitch", you mean "noticably glitch". Generally, the more "glitchless", the more involved the process.
Have a look through that Gamasutra article that was linked earlier; it has some techniques that should "solve" your current problems  :)
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 #66 on: February 11, 2014, 08:37:42 pm »
But there has to be a way that the balls don't glitch, using the current  way to calculate the collision...
Using the same calculations, they will "glitch" the same way.
I think, instead of "glitch", you mean "noticably glitch". Generally, the more "glitchless", the more involved the process.
Have a look through that Gamasutra article that was linked earlier; it has some techniques that should "solve" your current problems  :)

Thanks for pointing that out, I will try to understand the Article ;)

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #67 on: February 12, 2014, 04:58:33 pm »
Now I've read the Gamasutra  Article again, but I find it hard to understand the equations in the 'Calculating Bounce' Section.

So I thought it maybe would be easier to have a look at the Java Code:
// First, find the normalized vector n from the center of
// circle1 to the center of circle2
Vector n = circle1.center - circle2.center;
n.normalize();
// Find the length of the component of each of the movement
// vectors along n.
// a1 = v1 . n
// a2 = v2 . n
float a1 = v1.dot(n);
float a2 = v2.dot(n);

// Using the optimized version,
// optimizedP =  2(a1 - a2)
//              -----------
//                m1 + m2
float optimizedP = (2.0 * (a1 - a2)) / (circle1.mass + circle2.mass);

// Calculate v1', the new movement vector of circle1
// v1' = v1 - optimizedP * m2 * n
Vector v1' = v1 - optimizedP * circle2.mass * n;

// Calculate v1'
, the new movement vector of circle1
// v2' = v2 + optimizedP * m1 * n
Vector v2' = v2 + optimizedP * circle1.mass * n;

circle1.setMovementVector(v1'
);
circle2.setMovementVector(v2');

But I've got a few questions.  :P

I assume that
circle1.center
are just the coordinates of the first circle in a vector, aren't they?
What does
.normalize()
  and
.dot()
do and how can I do that in C++?
And my last question:
I assume that the 'movement vector' is just the X- and Y-Velocity saved in one Vector, am I right?

Any help is appreciated! :)











Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Beginner] Help with Bouncing Circles
« Reply #68 on: February 12, 2014, 05:54:29 pm »
I don't want to be mean but it really does tell you those things in the article. If you read it from start to finish, it tells you what normalising is, how to use the dot product etc.. It looks like you skipped to the last "version" of the code to try to understand it but it's not supposed to be alone; the earlier parts of the code are supposed to be involved or, at least, understood.
FYI, I went to the gamasutra article, and searched for normalize and it took me straight to the part that says what normalising a vector is  :P
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 #69 on: February 12, 2014, 07:42:01 pm »
... it tells you what normalising is

Yeah, it tells me what it is, but not how to calculate it :P

Anyways, I added a function to normalize a vector.

I forgot that I already implemented a function to calculate the dot product....  ::)

But there remain two questions:

I assume that
circle1.center
are just the coordinates of the first circle in a vector, aren't they?

And my last question:
I assume that the 'movement vector' is just the X- and Y-Velocity saved in one Vector, am I right?


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Beginner] Help with Bouncing Circles
« Reply #70 on: February 12, 2014, 07:51:49 pm »
You know that Thor already provides all those vector functions (normalize/unit vector, dot product, length, ...), and that I've mentioned it at least three times in this thread? :P

Just in case you don't want to reinvent the wheel in productive code. For learning purposes, it's probably not bad if you implement those functions once on your own.
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 #71 on: February 12, 2014, 08:11:39 pm »
You know that Thor already provides all those vector functions (normalize/unit vector, dot product, length, ...), and that I've mentioned it at least three times in this thread? :P

Just in case you don't want to reinvent the wheel in productive code. For learning purposes, it's probably not bad if you implement those functions once on your own.
I definetely will use thor in future projects, but since I'm still a beginner, I think I'ts better that I just write the functions by myself to learn how It works :)

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Beginner] Help with Bouncing Circles
« Reply #72 on: February 12, 2014, 09:13:16 pm »
I assume that
circle1.center
are just the coordinates of the first circle in a vector, aren't they?
That will be the center of the circle  :P But yeah, it'll be the position of the centre of the circle which will include all of the coordinates for that. In the case of two dimensions, it will have x and y.

And my last question:
I assume that the 'movement vector' is just the X- and Y-Velocity saved in one Vector, am I right?
Yes, movement is the amount that it has moved. As I said above, it'll be all the dimensions necessary but in 2D, it'll be x and y.
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 #73 on: February 12, 2014, 10:42:26 pm »
I assume that
circle1.center
are just the coordinates of the first circle in a vector, aren't they?
That will be the center of the circle  :P But yeah, it'll be the position of the centre of the circle which will include all of the coordinates for that. In the case of two dimensions, it will have x and y.

And my last question:
I assume that the 'movement vector' is just the X- and Y-Velocity saved in one Vector, am I right?
Yes, movement is the amount that it has moved. As I said above, it'll be all the dimensions necessary but in 2D, it'll be x and y.
Thank you!

Now It seems to work! :)
But there's one problem: The 'glitches' are still there! :(

What do I have to do to remove them? Look for an 'perfect' tutorial?
(I attached the source code, so I don't have to spam the whole thread)

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Beginner] Help with Bouncing Circles
« Reply #74 on: February 12, 2014, 11:57:04 pm »
The 'glitches' are still there
Which glitches? It looks fine to me.
The only thing that I can see that you may be noticing is that they still penetrate each other slightly. That is because you're calculating the radius of the filled circle, not including its outline. I believe that outlines expand outwards so you'd have to increase the radius that is tested by half of the outline width. (Not the actual radius otherwise the outline would make it even bigger!)

EDIT: I just removed the outlines and it looks awesome :p

EDIT 2: In slower situations, it glitches. It's because you are moving the discs scaled by the amount of time that has passed. There is not way of knowing how much time is going to pass. It could be five seconds! How far would they move then? You need to break time down into smaller chunks. Try reading up on timesteps. Here's an interesting read (warning: get ready to pay attention to the nitty-gritty!): Fix Your Timestep
You may find reading the previous article on integration useful before the timestep one.
« Last Edit: February 13, 2014, 12:10:43 am by Golden Eagle »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything