   Random enemy placement
  In the game I am currently working on, I want to randomly place enemies "off" the screen. I than have the enemies converging on the player. The problem that I am having is placing these enemies. My screen is 640x480 at the moment, and I tried a few different methods but can't seem to get anything that is satisfactory. At the moment, I am just randomly placing them on the screen. Any suggestions?Thanks~Casey~   
Quote:Original post by caseydIn the game I am currently working on, I want to randomly place enemies "off" the screen. I than have the enemies converging on the player. The problem that I am having is placing these enemies. My screen is 640x480 at the moment, and I tried a few different methods but can't seem to get anything that is satisfactory. At the moment, I am just randomly placing them on the screen. Any suggestions?Thanks~Casey~Can you clarify what you mean by not satisfactory? It seems a uniform random distribution in a rectangle containing the screen but expanded by some fixed amount would do what you want, but maybe you're looking for something more complicated than that.Ah, maybe it's that you're having trouble devising a method to place them only off the screen. If so, here are a couple of ideas:- Rejection method: place randomly until the position does not fall in the visible playing area. This of course relies on your random number generator being random enough to do this fairly quickly.- Divide the 'off the screen' area into rectangles, for example 8, one for each side and one for each corner. Compute the areas of these rectangles. Generate a random number to choose one of the rectangles, weighted by its area. Then, generate two random numbers to pick a position within the rectangle. I'm not sure, but it seems the distribution would be uniform.I actually have a similar problem to solve in my project, that is, random placement of an object within N rectangular areas of varying sizes, so I'll be interested to see what other solutions are proposed. (This is all assuming I understood your question correctly.)   ;  Is this a 2D or 3D game?Is this top view?In 2D top view it would be pretty easy to list all the neighbor tiles to on screen tiles that are available, and randomly pick one to spawn the enemy...If its a 3D top view game, I'd randomly pick an angle, and have a fixed distance, test if there's something there, if not spawn the enemy...If you have an indoor game, picking random positions probably won't help much, so I'd simply use something like a djikstra algorithm on your pathfinding graph, wait until you find a node that's not visible on the screen and spawn the enemy thereNot sure if that's helpful :)Cheers!   ;  I'd personally just pick a random number between 0 and 360 (or 2pi depending on the units) and use the character's location as origin to find a point suitably far away. You can find a distance large enough that no enemies spawn on screen or you could create a bounding space around the screen (cube or rectangle depending on the type of game) and intersect the vector with this space to get the spawn point.   ;  Thanks for the responses. I will clarify further. It's a 2D top down game. I'm not using tiles as everything is vector based.I tried to randomly computer an area off the screen by trying creative arithmetic. For example randomly placing the enemy on screen, and than subtracting it's position to bring it down to 0, and than doing some arithmetic to place it a little off screen. It didn't really produce good results.I than tried defining a rectangle where I didn't want the enemies, and checked to see if the enemies position was in this. If so, I recomputed it's position. This didn't provide adequate results either. They were clumped too much. I'll continue to work with some of these methods. At least I know I am on the right track.ThanksCasey   ;  For my last game, Freezer 2, I had a cheap algorithm that determined a random angle from the player and then put a new character a given distance away from the player at that angle (so as to make them offscreen). If the location picked intersected a wall, then I just re-did it (it failed out after 10 re-tries so as not to lock the machine up).It wasn't perfect but it worked pretty well.   ;Quote:- Divide the 'off the screen' area into rectangles, for example 8, one for each side and one for each corner. Compute the areas of these rectangles. Generate a random number to choose one of the rectangles, weighted by its area. Then, generate two random numbers to pick a position within the rectangle. I'm not sure, but it seems the distribution would be uniformI'd do this.   ;  I would add "spawn" locations to the map.  This would allow you to set points on your map where enemies could spawn safely (I.E. not in a wall, under water etc).I would then take the players location and determine which spawn points were closest to him throw those into an array of some sort and then create a random number which would activate one of those spawn points.  It would generate a enemy, and then I'd repeat the process for more enemies.It wouldn't be completely random, but I think it will provide you with the results you are looking for.  Especially if you have a map editor and can easily tag locations as spawn zones.   