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

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

0 Members and 2 Guests are viewing this topic.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #75 on: February 13, 2014, 05:13:50 pm »
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

I know, I'll  implement a feature that allows you to add and remove the outlines of the circles by pressing a button soon. :)

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.

And I thought It couldn't get more complicated anymore... ;)



Giblit

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #76 on: February 13, 2014, 08:51:25 pm »
You could also use negative outlines where it goes into the fill.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Beginner] Help with Bouncing Circles
« Reply #77 on: February 13, 2014, 08:56:33 pm »
And I thought It couldn't get more complicated anymore... ;)
I did say that the more accurate you want it, the more involved it becomes  ;)
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 #78 on: February 13, 2014, 10:44:51 pm »
You could also use negative outlines where it goes into the fill.

Good Idea! Didn't know that this is possible :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Beginner] Help with Bouncing Circles
« Reply #79 on: February 13, 2014, 11:30:45 pm »
You could also use negative outlines where it goes into the fill.
Good Idea! Didn't know that this is possible :)
It's all here:P
I'd forgotten about this, actually. I got mixed up with line drawing which expands from the centre  ;D

EDIT: fixed the link. Thanks to Giblit for pointing out the error  :)
« Last Edit: February 14, 2014, 02:07:06 am by Golden Eagle »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Giblit

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #80 on: February 14, 2014, 01:20:17 am »
Your link is a bit messed up.

http://sfml-dev.org/documentation/2.1/classsf_1_1Shape.php#a5ad336ad74fc1f567fce3b7e44cf87dc

Quote
void sf::Shape::setOutlineThickness   (   float    thickness   )   
Set the thickness of the shape's outline.

Note that negative values are allowed (so that the outline expands towards the center of the shape), and using zero disables the outline. By default, the outline thickness is 0.

Parameters
thickness   New outline thickness

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #81 on: February 14, 2014, 01:25:37 am »
Wow, 6 pages for this thread  :o

OP, can you please state clearly if you problem is solved, or if there is anything else left to solve?  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #82 on: February 14, 2014, 08:57:12 am »
OP, can you please state clearly if you problem is solved, or if there is anything else left to solve?  ;)

When everything is solved, I'll post that, no worries :)



Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #83 on: February 14, 2014, 09:02:56 am »
Hmmm, I got an Interesting solution from Geheim via PM.
The solution is a bit 'dirty' but seems to work.

I added this to my if-collusion-statement:
Ball[i].move(-xSpeed[i] * 0.001f, -ySpeed[i] * 0.001f);
Ball[j].move(-xSpeed[j] * 0.001f, -ySpeed[j] * 0.001f);
 

Full Source-Code as attachment :)

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #84 on: February 14, 2014, 10:31:55 am »
Thats like I said just a "dirty" approach to avoid the visual jumping you experience. For me your code seems to work like it should, however you should test a bit more yourself before posting again. Of course we could give you fully working source code, but thats not what we want. You should manage it with all the help you got now yourself...
Failing to succeed does not mean failing to progress!

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #85 on: February 14, 2014, 05:32:42 pm »
Just noticed that it doesn't work, glitches are still occurring.
Seems like I have to go through the 'Fix your timestep' Article...  :-\

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #86 on: February 14, 2014, 10:31:44 pm »
Ok, still trying to fix my timestep, but meanwhile I added two features:

1. Outline can be (de-)activated by pressing the 'r' key
2. Circles change color when colliding with a window-border

But there's one thing I don't understand: It seems like the colors are always nearly the same. What did I do wrong?  :-\




Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #87 on: February 14, 2014, 10:43:19 pm »
Ok, still trying to fix my timestep, but meanwhile I added two features:

1. Outline can be (de-)activated by pressing the 'r' key
2. Circles change color when colliding with a window-border

But there's one thing I don't understand: It seems like the colors are always nearly the same. What did I do wrong?  :-\

It looks like you keep resetting the seed for the random function. Take a look at the <random> part of the stl. IT has a lot better functionality than the old random.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #88 on: February 14, 2014, 11:30:06 pm »
It looks like you keep resetting the seed for the random function. Take a look at the <random> part of the stl. IT has a lot better functionality than the old random.
Okay, I now I use the new random:
default_random_engine engine;
uniform_int_distribution<int> distribution(1,255);
auto random = bind ( distribution, engine );
 
I use this no matter which border is touched:
Ball[i].setFillColor(sf::Color(random(), random(), random()));
 

Now it definetely looks better, but do I need to set an extra seed for the generator?

Giblit

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #89 on: February 15, 2014, 01:34:13 am »
You only need to seed the generator once.

PRNG use the seed as S0. Since a PRNG is just a sequence. I don't know the exact forumas but they are something like: Sn+1 = ( A * Sn + B ) % M; Where A , B , M are given before hand. So for example a generator that looks like:
Sn+1 = ( 10 * Sn + 11 ) % 12 with a seed of 0 it would look like this: ( repeats really fast since I used really small values )

11 , 1 , 9 , 5 , 1 , ...now it is repeating

Most of the PRNG won't repeat for a very long time

 

anything