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

Author Topic: [Beginner] Help with random-colored Circles  (Read 12005 times)

0 Members and 2 Guests are viewing this topic.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
[Beginner] Help with random-colored Circles
« on: February 22, 2014, 12:21:07 am »
Hey Guys, I'm here again with my problem:
I got some circles bouncing around in a window. But I want that the circles change their color when they collide with a wall.

This may seems very simple, but trust me, it's harder than It looks.
The problem I have, is that the colors are always similar.

Let me explain the problem more in detail:

I got a random function, which generates a random number between 1 and 255. I uses the elapsed time of a clock in microseconds as seed. The function works fine.

But now theres a problem: When I try to set the FillColor of the Circle like this:
Circle.setFillColor(sf::Color(Random(clockSeed) , Random(clockSeed) , Random(clockSeed));
 

The Circle will always get a grey color, because Red, Green and Blue have the same value.

Then I tried something like this:
Circle.setFillColor(sf::Color(Random(clockSeed * 2) , Random(clockSeed * clockSeed) , Random(clockSeed));
 

And a LOT of other methods to give each color a different value.
But that works neither, now the Circles have similar colors, because the 'proportions' of the color's values are always the same.

I have some Ideas for nasty solutions, but does anyone have an smart solution?

(Probably the solution is obvious and my brain is just stuck...)

Thanks In advance for your answers! :)

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: [Beginner] Help with random-colored Circles
« Reply #1 on: February 22, 2014, 12:42:34 am »
Show your Random function.
Seed your random generator only once, you'll get different random numbers each time you call your function.
« Last Edit: February 22, 2014, 07:44:28 am by G. »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Beginner] Help with random-colored Circles
« Reply #2 on: February 22, 2014, 05:55:38 am »
If you seed a PRNG multiple times with the same number (which it looks like you are doing) you'll get the same sequence of numbers each time. So it's no surprise that you get grey colors like 100,100,100 or 42,42,42 with what you are doing.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [Beginner] Help with random-colored Circles
« Reply #3 on: February 22, 2014, 09:10:49 am »
Moreover, using a random value for each RGB component will always give more or less grey colors. To have a better distribution within the color space, you should use HSV components to define your colors.
Laurent Gomila - SFML developer

Njifra

  • Guest
Re: [Beginner] Help with random-colored Circles
« Reply #4 on: February 22, 2014, 10:06:35 am »
This might help you:

srand(time(NULL));
Circle.setFillColor(sf::Color(rand() % 255, rand() % 255, rand() % 255));
 

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with random-colored Circles
« Reply #5 on: February 22, 2014, 10:46:51 am »
This might help you:

srand(time(NULL));
Circle.setFillColor(sf::Color(rand() % 255, rand() % 255, rand() % 255));
 

Haha, no sorry :) First of all, this method would only generate random numbers from 0 to 254, not 255.
Moreover, every number generated would be the same for every color, using this method. And if you use the time as seed, the seed will only change every second, so if two different balls collide with the wall within a second, they will have the same color.


If you seed a PRNG multiple times with the same number (which it looks like you are doing) you'll get the same sequence of numbers each time. So it's no surprise that you get grey colors like 100,100,100 or 42,42,42 with what you are doing.

I know, but I said that in my first post too ;) :
But now theres a problem: When I try to set the FillColor of the Circle like this:
Circle.setFillColor(sf::Color(Random(clockSeed) , Random(clockSeed) , Random(clockSeed));
 

The Circle will always get a grey color, because Red, Green and Blue have the same value.
... you'll get different random numbers each time you call your function.

But that isn't the problem! I get different numbers each time I call the function...
Moreover, using a random value for each RGB component will always give more or less grey colors. To have a better distribution within the color space, you should use HSV components to define your colors.
Hmmm, Okay I will try to implement that, but I think I have to write a function to convert HSV to RGB?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [Beginner] Help with random-colored Circles
« Reply #6 on: February 22, 2014, 07:22:44 pm »
Quote
And if you use the time as seed, the seed will only change every second, so if two different balls collide with the wall within a second, they will have the same color.
The seed is just the initial value. You still get a new random value everytime you call std::rand().
Laurent Gomila - SFML developer

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: [Beginner] Help with random-colored Circles
« Reply #7 on: February 23, 2014, 11:56:08 pm »
srand(time(NULL));
Circle.setFillColor(sf::Color(rand() % 255, rand() % 255, rand() % 255));
 

Haha, no sorry :) First of all, this method would only generate random numbers from 0 to 254, not 255.
Moreover, every number generated would be the same for every color, using this method. And if you use the time as seed, the seed will only change every second, so if two different balls collide with the wall within a second, they will have the same color.
Every call to rand() will give you different result.
Seed is the starting point of the algorithm that is used to produce the number.

If you want to be fancy you can just create your own pallet of colors and random from those, that will provide you with nice choice of colors and to the user "random" color each time.

From "Laurent"s post the "HSV" , googling "HSV components to define your colors." gave a great result/answet to the post.
« Last Edit: February 24, 2014, 12:00:45 am by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with random-colored Circles
« Reply #8 on: February 24, 2014, 08:56:11 pm »
Oh, Okay... Thanks for pointing that out, I didn't know that... I rarely used random numbers in C++, thanks for your help :)

 

anything