• 2D world and Camera question
    2 replies, posted
Okay, so i've made a grid which is stored in a vector, that's fine and works silky smooth. The grid spans several thousand units either side, but I'm trying to not render those while the camera is focused on a particular segment of the grid. I can make the camera move left or right depending on the code : camera.x(-1) which moves it left or camera.x(1) which is right. I've tried referencing the vector item like so: [code] void worldGen::renderBoundaries( float x1 , float y1 ) { int frameX = x1 + 500; int frameY = y1 + 500; for ( int i = 0 ; i < tile.size(); i ++ ) { if ( tile[i]->tileX > x1 && tile[i]->tileX < frameX ) { drawEllipse(tile[i]->tileX,tile[i]->tileY,2,2); } } } [/code] x1 and y1 are the camera positions. and frameX and frameY takes the left most value of the camera and adds the screen width and height. I'm trying to get a somewhat escalator effect in the sense that the tiles are shifted to the right as you move left and the tiles on the far left (out of scope) are then rendered and drawn on the left most part of the page. I'm pretty bad with C++ as it's my first week messing with it properly using openGL. I've managed to get it down this far into these components and they render fine in either one large sum or in blocks. Any clues, this is probably a simple solution but its getting my head around shifting two different parts at the same time.
I'm not quite sure what your actual question is, but in your code, you just check for your screen width, meaning any tiles spanning vertically above and below the screen will still render. To omit that, just duplicate the condition you have and swap Xs with Ys. Also, you would want to use x1-tilewidth and y1-tileheight (assuming their origin is top/left) as your top/left border to make sure that every tile extending into screen boundaries does indeed render. What I generally do with my 2D grids is making a std::vector<std::vector<tile*> >, then divide the screen boundary values by the tiles' dimensions and use the results directly inside the for-loops. This way I don't have to iterate over ALL tiles every time, but just the ones really visible on screen, plus I don't need additional conditional statements.
I didn't really understand your question, but it just seems like you trying to make a 2D camera with a bunch of tiles placed inside a vector. You also probably don't want to render anything outside of the screen. This is very simple. Heres an example: [CODE] void render(float camX, float camY){ for(int i=0; i<tiles.size(); i++){ if(inRect(tiles.get(i), camX, camY, 500, 500)) drawTile(tiles.get(i), tiles.get(i)->X - camX, tiles.get(i)->Y - camY, ); } } //Returns true if the given tile is within the given rectangle dimensions bool inRect(Tile *check, float X, float Y, float W, float H){ return ((check->x<X+W && check->y<Y+H) && (check->w+check->x>X && check->h+check->y>Y)); } [/CODE] Since you're new to C++ I'll try to explain some stuff: First, 'Tile' is an object representing a tile. It has x,y,w,h. the vector filled with tiles ('tiles') contains pointers to instances of the tiles. the function inRect returns true if the given tiles [i]bounding box[/i] are inside the given rectangles dimensions **NOTE** This function assumes that the tiles width/height and rectangles width/height ARENT relative. That is, the x/y values can be greater than the w/h values. eg: Tile A: ~x = 100; ~y = 250; ~w = 32; ~h = 32; Same goes for the rectangle given in the parameters of the inRect function. EDIT: also, this part: [CODE] tiles.get(i)->X - camX, tiles.get(i)->Y - camY, [/CODE] Will achieve what I think you mean by 'escalator effect' P.S. I haven't checked the code for validity since I don't have msvc open and I don't feel like opening it. Also, I've been using Java for the past couple of months and this is actually the first bit of C++ I've written since then....so excuse my Javaness :P
Sorry, you need to Log In to post a reply to this thread.