SFML community forums

Help => General => Topic started by: ka0s420 on March 08, 2016, 08:00:47 am

Title: issues with random point inside a floatRect
Post by: ka0s420 on March 08, 2016, 08:00:47 am
Hello everyone, I'm currently working on a finite state machine for my enemy ai. One of the states i want to implement is a patrolling state. I want this to be the default state when the enemy spawns, so they patrol the given spawn area which is passed as a floatRect.

Now to do this, I figured i would make a randomly positioned floatrect, which is fine, but to get a random point inside the floatrect at which to spawn the enemy sprite i used the following method:

spawn = the name of the floatrect in question...


sf::Vector2f(float(rand() % (int(spawn.left) + int(spawn.width)) + int(spawn.left)),
                float(rand() % (int(spawn.top) + int(spawn.height)) + int(spawn.top)));

 
Now i've tried various versions of casting to int and not casting to int, without any noticeable change. they need to be ints for rand() to work, and each number needs to also be cast back into a float to work with vector2f. i add the left to the width to get the global position of the far side of the rect, and then add the left side to the formula to make it so that rand() starts its range at the beginning of the rect and not at 0.

Either way, none of this is working and the sprites appear seemingly wherever they like.

can include more code if needs be, but for testing purposes all im doing is drawing them to the screen in the initial position contained in the above vector2f.

Thanks for your time.
Title: AW: issues with random point inside a floatRect
Post by: eXpl0it3r on March 08, 2016, 08:12:44 am
You should use C++11's <random> header: https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

And if you cast use C++ casts (e.g. static_cast<int>).
Title: Re: issues with random point inside a floatRect
Post by: ka0s420 on March 08, 2016, 08:39:16 am
okay well, i might use the <random> header at some point, but I'm not too bothered about the distribution of numbers. As long as they look random at this point that is fine by me. I changed the casts to be static_cast<>, but no change sadly. Should the formula work? If so there has to be a problem elsewhere.

Here's the snippet updated with static casting:

sf::Vector2f(static_cast<float>(rand() % (static_cast<int>(spawn.left) + static_cast<int>(spawn.width)) + static_cast<int>(spawn.left)),
                                        static_cast<float>(rand() % (static_cast<int>(spawn.top) + static_cast<int>(spawn.height)) + static_cast<int>(spawn.top))),
 
Title: Re: issues with random point inside a floatRect
Post by: Laurent on March 08, 2016, 09:19:28 am
If you have to add left/top twice then there's something wrong, obviously ;)

The formula should rather be Vector2f(left + (rand % width), top + (rand % height)). If the results are still incorrect, then there must be something wrong elsewhere. Don't hesitate to compare the numbers that you get with what you expect. You can even add assert(spawn.contains(position)).
Title: Re: issues with random point inside a floatRect
Post by: ka0s420 on March 08, 2016, 09:44:11 am
 :D Thank you Laurent, worked like a charm. Thank you all, case closed ;)