I'm looking for an algorithm for spawning enermies at the egdes for the gameboard/window for an Android game. I'm doing the old SpaceWars/Shooting asteriods with spaceship and i need the asteroids to spawn at random locations outside the gameboard, where after they will move into the gameboard in different directions and speeds.
It's easy to place the asteroids at random locations, but the tuff part (at least for me) is to give them the right move direction based on the random location they are giving.
For example:
Gameboard = 1280 x 720
Asteroid random location = 1100, 0 (x, y)
Right move direction:
asteroid.x < gameboard.x/2 - possible move directions = south and south east
asteroid.x > gameboard.x/2 - posiible move directions = south and south west
Then the asteroid have two possible move directions (that's how i define the gameplan for now): Direct south or south west.
I could do a lot of if statements, but i'm hoping someone can help me with some kind of algorithm for the problem?
Maybe I got wrong your question but this is how I see the solution :
If the asteroid x-position
exceeds your device width (720) then its direction should be SW otherwise it should be S.
if(asteroid.x>DEVICE_WIDTH){
asteroid.direction="SW";
}else{
asteroid.direction="S";
}
This link may help you http://forums.create.msdn.com/forums/p/100862/599129.aspx
I think you might always need some nested if statements but this way may be a bit more clever