Spawn stuff within the bounds of a camera


(Nate ) #1

Hey guys! I have a small demo I am working on, and there is a player and some camera movement. Thinking about it, I have never done this before so I am not quite sure how to go about executing it.

Basically I would like items and such to appear but only within the bounds of the camera to effectively not slay the used ram.

I thought about cheating, and using an invisible box that was the same dimensions as the screen, set the scrollX and scrollY to 0(to make this box scroll with the player) then just set the coordinates of the spawned items to the invisible screen coordinates to only spawn stuff where the player can see it.

Let me know what you guys think!

Thanks


(David Williams) #2

That’d work, or just using FP.camera coordinates.


(Nate ) #3

Still having some issues using the invisible box method… Can you give me some pseudo code?

Thank you


(Ultima2876) #4
if(objectX > cameraX && objectX < cameraX + screenWidth)
{
  if(objectY > cameraY && objectY < cameraY + screenHeight)
  {
    //object is within the screen.
  }
}

You should also add a margin around the screen (basically 1 of your largest object’s size - so if your biggest object is 128 pixels, go with that on each side of the screen) to make sure objects spawn outside of the camera view. That should be simple to add to the above code :wink: