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

Author Topic: issues with random point inside a floatRect  (Read 1685 times)

0 Members and 1 Guest are viewing this topic.

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
issues with random point inside a floatRect
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10783
    • View Profile
    • development blog
    • Email
AW: issues with random point inside a floatRect
« Reply #1 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>).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: issues with random point inside a floatRect
« Reply #2 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))),
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: issues with random point inside a floatRect
« Reply #3 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)).
Laurent Gomila - SFML developer

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: issues with random point inside a floatRect
« Reply #4 on: March 08, 2016, 09:44:11 am »
 :D Thank you Laurent, worked like a charm. Thank you all, case closed ;)