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 / Spawning Sprite

Author
Message
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 1st May 2012 19:09
I am spawning new sprites at a random position on screen. I want to make sure that the new spawned sprite is not located on top of another sprite already on screen. I don't want overlapping sprites.

Right now I am checking the hit status of the new sprite with every other sprite onscreen!!!

Is there a more efficient way to do this?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st May 2012 21:19
If you are using physics sprites you can check the sprite contacts list. Even checking all sprites in an array doesn't take very long.

kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 1st May 2012 21:23 Edited at: 1st May 2012 21:24
Well, if you would be using a grid, then it could be easly achieved with arrays, but I guess you aren't using grid.

Therefore, you could do it this way:

Create (sample) 10x10 array of type/class that will hold all sprite's positions inside (if you use Tier 2, use vector for that). When spawning a sprite, check to which field will it be attached. When you know it's for example grid 2x5, check if that sprite doesn't overlap with any other sprite inside that one grid.
This will increase collision check speed, as you check for collision, but only for sprites inside that one field. (There won't be that many sprites I suppose?)

Field's size is specified by you.

Additional:

How to check to which grid that sprite belongs?
When you have a field of, say, 100x100 pixels, and you have 10x10 fields you can do it this way:

Get sprite's X and divide it by 100, then turn the result into integer. This is field's X in array. Same goes for Y.
In the result, you'll have, for example, if your sprite's X was 234 and it's Y was 123, then corresponding field will be: [1][2] (Y, X).

I hope it's clear, but it's hard to explain such complicated thing.

Baxslash probably gave you easier way, but my way can be used across engines/APIs without need to use Box2d.

bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 1st May 2012 21:29
unfortuantely not using physics sprites on this project
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 1st May 2012 21:36
Quote: "unfortuantely not using physics sprites on this project "


Consider using my solution

Login to post a reply

Server time is: 2024-05-04 18:38:34
Your offset time is: 2024-05-04 18:38:34