Asteroid generation

Hi everyone! It’s been a while.

I have a question about asteroid generation.

Previously, I would generate the asteroids randomly throughout a certain area. For instance, maybe I generate 250 asteroids in a range of -10000 to 10000 x and y.

However, I realize that this is inefficient. There are too many physics bodies on screen and the player might not even wander into a certain area, making those asteroids obsolete and adding to lag. I have seen this in my game, as I knock into asteroids, they push into others and keep going off screen. These collision events are slowing my game down.

I have decided to generate based on the camera. So, basically, as the player is moving in a certain direction, asteroids will appear in front of the player, and disappear behind the player. However, I am not sure how I would generate the asteroids this way. I have the asteroid creation and collision code, just not the positioning.

Long story short, I don’t know how to place the asteroids in a certain coordinate that the player is facing at.

Any tips on that would be great!

Happy holidays!

I would try the following…

  • If you have a large amount of objects, you probably want to use object pooling (*cough*, I have a plug in to assist, or you can roll your own), since you do not want to create and destroy a bunch of asteroid objects over and over again.
  • Create some off screen physics objects that will remove asteroids when a collision occurs.
  • To place objects, I would probably split the screen in half (in my mind), determine which way your ship is facing, and spawn asteroids on the side that the ship is facing.

Hope this helps.  Let me know how it goes.

>> in a certain coordinate that the player is facing at.

does this imply that the player has free rotation?  if so, then to position “in front” of the player just take some distance times cos/sin of that angle and add it to the player’s position, fe:

-- PSEUDO-CODE: local r = 1000 local ax = px + r \* math.cos(theta) local ay = py + r \* math.sin(theta)

(this approach works even if your player doesn’t have free rotation, but may be more complicated than needed if rotation is fixed - ie, with fixed rotation you might be able to just add or subtract some constant x or y to the player’s position)

By free rotation, do you mean that the player can rotate in any direction?

yes, “free” as in “not constrained, bound, fixed or limited”

so, to clarify your post:  player has a fixed rotation OR player may freely rotate?

The player may freely rotate.

I would try the following…

  • If you have a large amount of objects, you probably want to use object pooling (*cough*, I have a plug in to assist, or you can roll your own), since you do not want to create and destroy a bunch of asteroid objects over and over again.
  • Create some off screen physics objects that will remove asteroids when a collision occurs.
  • To place objects, I would probably split the screen in half (in my mind), determine which way your ship is facing, and spawn asteroids on the side that the ship is facing.

Hope this helps.  Let me know how it goes.

>> in a certain coordinate that the player is facing at.

does this imply that the player has free rotation?  if so, then to position “in front” of the player just take some distance times cos/sin of that angle and add it to the player’s position, fe:

-- PSEUDO-CODE: local r = 1000 local ax = px + r \* math.cos(theta) local ay = py + r \* math.sin(theta)

(this approach works even if your player doesn’t have free rotation, but may be more complicated than needed if rotation is fixed - ie, with fixed rotation you might be able to just add or subtract some constant x or y to the player’s position)

By free rotation, do you mean that the player can rotate in any direction?

yes, “free” as in “not constrained, bound, fixed or limited”

so, to clarify your post:  player has a fixed rotation OR player may freely rotate?

The player may freely rotate.