Spawn Within Circular Band

Hi,

I’m unsure how to go about this, perhaps you guys can point me in the right direction.

Basically, what I’m looking to do is spawn a bunch of asteroids randomly in a circular band with an adjustable max/min radius.

Easy enough doing it along say, the X and Z axis as I just gotta check if it’s within Xmin and Xmax or radiusMin/Max along said axis and then add/sub a random number between 0 and 25 for some height variance, but going in the circle is different, and I need some help.

There is the node ‘get vector length’, you could generate random spawn locations based on it being in a square, and then just subtract the location with the middle of the circle location (and plug the result into ‘Get Vector Length’), and if it is greater than the circle’s radius, find a new location.

That’s relying on a lot of luck seeing as the square would be rather big and the asteroids are fairly small, could make the times generation times terrible and I don’t really fancy sitting for several minutes for it to get it right as there will be a lot of em. And to do that every time something changes (in the editor).

It’s worth a try, though, perhaps it won’t be too bad.

What comes to mind would be making a vector along the x axis say, which definitely falls in your band (maybe using ‘random float in range’ with your min and max values). Then you could rotate that vector by a random number of degrees between 0 and 360 (although that might be doubling up the intial position since 0 and 360 give you the same position) to get somewhere random along the band.

I managed to get something working, rather nicely too I think.
Basically, what I do is rotate a vector whose length is 1 to a random value between 0 and 360. Then I multiply that by a random number between the min and max radius, and add a random min and max height to the Z axis.

Now my main problem is performance :frowning:

One could say you’re reinventing the wheel. :wink: How about this?

The range should be 0-360 in your case, ofc.

Why 360 rather than 359? 360 would be a duplicate possibility in case of 0 being a possibility.

That would have been easier lol, need to learn more math :confused:
Also, would it be better to use the degrees variants of sin and cos?

Just out of curiosity, as I’m feeling dumb: why use sine and cosine functions?

We’re looking for a point on the circumference. I’m pretty sure more than one method exists, like bogieman987 displayed above. In my head x = cos(angle) * radius gets you there.
I guess you could attach a springarm component and rotate it, it would involve vector transformation - not sure how efficient it is from the performance point of view, though. Negligible in some cases, critical in others.

@Horus: I doubt you’re feeling dumb, you have something on your mind. How would you approach it?

@Jamendxman3: 0-359 will not give you 359.5, for example. And the chances of rolling precisely 0 and 360 with floats are pretty slim.