Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / AGK Random Function

Author
Message
DannyD
7
Years of Service
User Offline
Joined: 29th Aug 2017
Location:
Posted: 28th Apr 2019 13:38
Hi

I use the random and Random2 functions to place 6 sprites randomly on the screen without success.
It doesn't spread the sprites nicely. (See attached images)
Anyone with a better solution / example code to Spread the sprite "better"


Attachments

Login to view attachments
Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 28th Apr 2019 16:22
How about this. Before placing a new sprite, loop through the others, checking how far away they are from the one you are about to place. If one of them is too close, grab another random number and try that. In case there are loads of sprites, to avoid getting stuck in the loop, you could limit the number of 'tries' the program has at getting an empty space.

Just a thought!
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 28th Apr 2019 17:17
they look like 3D objects to me
but I would check for sprite collisions when you are randomly
placing them if they overlap then assign a new random position

similarly you could check within your own sprite bounds
ie SpritePositionbybyOffsetX-100 and SpritePositionbyOffsetX + 100

so you would need a loop to place your sprites where a function
could be called to check if its in bounds of one of the other sprites
if the function returns 1 for example it proceeds to place next sprite

just some ideas to try

fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 28th Apr 2019 19:42
The issue might be your random code. Maybe you chose bad random intervalls.
Have you got some code to show?


PSY LABS Games
Coders don't die, they just gosub without return
DannyD
7
Years of Service
User Offline
Joined: 29th Aug 2017
Location:
Posted: 29th Apr 2019 00:16
@PSY
It's just images of 3D objects, but not 3D. Can be any image...

Normal for/next loop

Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 29th Apr 2019 02:56 Edited at: 29th Apr 2019 03:03
Quote: "It doesn't spread the sprites nicely."


i think you want quasi-random or low-descrepancy sequences (vs truly random or pseudo-random). i've seen the term "scatter", too.

while i wouldn't begin to know what all that math is about, i would fake it by dividing the area you want the sprites to fall in into what i'll call "quadrants" (or some other form of dividing the area you want the sprites to fall in).

then, randomly pick a quadrant, randomly place the sprite inside the quadrant, mark the quadrant as "occupied" then move on to the next sprite.

if the number of sprites > than the number of quadrants, add 1 to each quadrant as the sprites are placed and use the quadrant "counts" as "weight" for subsequent choices.

it's been awhile since i wrote code to "uniformly scatter" (dunno what the real term is), but i feel the theory is sound

PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 29th Apr 2019 09:27
Yeah,

the issue could've been some bad numbers for the spread/sprite coords, but in your case, it's what the guy above me said


PSY LABS Games
Coders don't die, they just gosub without return

Login to post a reply

Server time is: 2024-11-22 21:53:02
Your offset time is: 2024-11-22 21:53:02