Page 1 of 2

Finding a random point on a circle

PostPosted: Mon Mar 30, 2009 12:55 pm
by Fuzzy
Bee Ant asked me how to do this, and we thought that everyone should know. So thank him if its useful to you.

First, pick a number from 0 to 359. Store it in a variable. I used i.

Next, use two variables to store floats. that is variable type real. Call them xr and yr for radius. Next you need an integer to store distance from center. I used d.
After these lines of code you can use xr and yr to add to an actor's x and y. This is good for special effects.

Code: Select all
i = rand(360);
xr = cos(degtorad(i))*d;
yr = sin(degtorad(i))*d;


visual demonstration here.

http://www.dreade.org/applets/circlerandom/

Thanks to DST for the hosting.

Re: Finding a random point on a circle

PostPosted: Mon Mar 30, 2009 1:06 pm
by Kalladdolf
Thanks a bunch, this is way less complicated than the formula my brother-in-law once came up with!

Re: Finding a random point on a circle

PostPosted: Mon Mar 30, 2009 4:09 pm
by Fuzzy
Kalladdolf wrote:Thanks a bunch, this is way less complicated than the formula my brother-in-law once came up with!


You will find it develops holes when the radius gets big.

Re: Finding a random point on a circle

PostPosted: Mon Mar 30, 2009 6:33 pm
by Kalladdolf
And if you use i as a float (or is that what you're already doing)?

Re: Finding a random point on a circle

PostPosted: Tue Mar 31, 2009 2:51 am
by Fuzzy
It doesnt matter if i is a float or an int.

Re: Finding a random point on a circle

PostPosted: Sat Apr 04, 2009 1:15 pm
by Bee-Ant
Fuzzy wrote:WE thought that everyone should know

I didn't think about it... :arrow:

Re: Finding a random point on a circle

PostPosted: Sat Apr 18, 2009 12:30 pm
by Kalladdolf
Anyway... until now it's worked great so far.
Are those holes regular or irregular?

Re: Finding a random point on a circle

PostPosted: Sat Apr 18, 2009 10:12 pm
by Bee-Ant
Whaddya mean with regular or irregular?
I used this code to make recharge effect.

Re: Finding a random point on a circle

PostPosted: Sun Apr 19, 2009 9:52 am
by Fuzzy
Kalladdolf wrote:Anyway... until now it's worked great so far.
Are those holes regular or irregular?


Regular.

Re: Finding a random point on a circle

PostPosted: Sun Apr 19, 2009 11:35 am
by Bee-Ant
Btw fuz,do you know how to make formula that has the same function with "MoveTo" using angle and directional_velocity?

Re: Finding a random point on a circle

PostPosted: Sun Apr 19, 2009 11:38 am
by Kalladdolf
Fuzzy wrote:
Kalladdolf wrote:Anyway... until now it's worked great so far.
Are those holes regular or irregular?

Regular.


From that I would conclude that with a certain radius, normal angle values (like 1,2,3,4, 89, 134, 343) become too unspecific.
I think I might try a float variable with more precise numbers, maybe the holes'll get smaller.
EDIT:
@Bee-ant: You mean just the simple MoveTo function without avoid? I always used:
Code: Select all
angle = direction(x,y,destination.x,destination.y); directional_velocity = whatever;

And then some coordinates to stop the actor from moving once it's there.

Re: Finding a random point on a circle

PostPosted: Sun Apr 19, 2009 5:03 pm
by Bee-Ant
What!!!
Ehm...thanks
Thats what i need...

Re: Finding a random point on a circle

PostPosted: Sun Apr 19, 2009 5:26 pm
by Kalladdolf
You sure that wasn't sarcastic?

Re: Finding a random point on a circle

PostPosted: Sun Apr 19, 2009 11:55 pm
by Bee-Ant
Huh?course not...

Hmmm,actualy i was thinking about direction,but just dont know the syntax yet...

Oh yah,do you have machinary sound such as motor,robot,laser,metal,etc?
Also some pic of futuristic machinary building or room?
Want them sooo much...well,i think i need to topic this.

Re: Finding a random point on a circle

PostPosted: Mon Apr 20, 2009 1:50 pm
by Kalladdolf
Bee-Ant wrote:Huh?course not...

Oh, right, yeah, it sounded to me like I defeated the purpose completely. Glad I didn't. :)