Jump to content
  • Advertisement
Sign in to follow this  
dzeligman

[Solved] 2d Camera used for Drawing filtering ,"culling"

This topic is 3624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having a few issues with how I currently have my 2d camera setup. The game is a top down shooter. The camera has a position and a view rectangle. The view rectangle is essentially its viewport. I move it at a set speed through the course of the level until the end of the level where it stops. Currently for enemies I either hardcode or randomly set their "world position." Which is a normal vector with an X between 0 and my screen width and the Y is between 0 and my world height. Worldheight is a multiple of screenheight currently. For each ememy during each update call I check and see if the camera view rectangle contains the enemies worldposition, if so I transform the world position into the enemies draw position and draw it to the screen. The transformation goes like: finalPosition = nodePosition - cameraPosition , where nodePosition is the entities current worldPosition. It works alright thus far, but I've had a few issues where the enemies arent being changed to "active" until theyre in the middle of screen or it might be that the translation changes them from a worldPosition at the top of the screen to the middle. Regardless I would like some advice on how to specify the positions of objects with a camera or how to modify the camera to aid in this request. The camera is currently a global singleton. Thanks [Edited by - dzeligman on July 23, 2008 8:28:50 AM]

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure what you're trying to ask... But,

Try transforming and drawing them anyway, regardless if they're not in the viewing rectangle. IMO, you should probably only worry about culling when you have so many objects drawing on the screen. I haven't bothered with 2D culling yet, and have gotten 40,000 objects to exist on screen at 60 fps (10,000 objects in debug mode, however).

Also, it may depend on the capabilities of the graphics engine you're using; most of them should already handle culling.

Share this post


Link to post
Share on other sites
Enemies are only becoming active when their in the middle of the screen? It's probably a problem with your numbers. This is how I would implement it.

constants:
Camera.CAMERA_WIDTH : public const int
Camera.CAMERA_HEIGHT : public const int

variables:
Camera.pos : Vector2 containing (x,y) of the camera
this.pos : Vector2 containing (x,y) coordinates of the enemy
this.active : bool showing whether the enemy is active

This code would be placed in the Update() of the enemy in order to check to see if the enemy is active.
if (this.pos.X >= Camera.pos.X &&
this.pox.X <= Camera.pos.X + Camera.CAMERA_WIDTH &&
this.pos.Y >= Camera.pos.Y &&
this.pox.Y <= Camera.pos.Y + Camera.CAMERA_HEIGHT)
this.active = true;
else
this.active = false;


In order to change the camera values either make fancy scroll methods so you can hide the speeds (if it's fixed) from the other objects. Or simply change Camera.pos.
Camera.pos.X += 10; // Move the camera 10 pixels to the right

Share this post


Link to post
Share on other sites
Thanks for the advice, my camera actually does all of that right now. I relooked and my calculations were slightly off as well as I did something kind of silly.

I made an enemy inactive if the camera view did not contain the enemies worldposition, thus if I made an enemy outside the view then my cleanup would delete the enemy before it was even shown.

I just changed how the view/inactive stuff works among a few other things.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!