Skip to main content
gamedev.net
🔒 Locked

Infinite Grid

Started by psyjax May 17, 2004 at 12:14 AM 7 replies 1.5k views
Original Post
psyjax
psyjax
I want to create an Infinite Grid, like the sort you would see in a CAD program, where the dots are spaced an even amount of units apart in the background. My camera moves about the sceen, and I need the grid to keep being drawn as it moves. Unfortunetly I can''t figure out how to do this as I have only done finite, things before :D How would I make it apear as if there is an infinit grid under the camera?
psyjax
psyjax
Well, let me post my code.


void DrawWorldGrid (float left, float right, float top, float bottom )
{
float start_x = left/GRID_UNIT;
float start_y = top/GRID_UNIT;
float end_x = right/GRID_UNIT;
float end_y = bottom/GRID_UNIT;

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

for(float x = start_x; x < end_x; x++)
{
for(float y = start_y; y < end_y; y++)
{
glBegin(GL_POINTS);
glVertex2f(x * GRID_UNIT, y * GRID_UNIT);
glEnd();
}
}
}


Ok, now see the top, right, bottom, left parameters?

These are the cameras "view rectangle". It''s called from here:


void CCamera::DrawView( void )
{
UpdateCamera();
glTranslatef(x_pos,y_pos,0.0f);

DrawWorldGrid(x_pos, x_pos + width, y_pos, y_pos + height);
}


Now, I thought this would work. Why isn''t it working?
Fruny
Fruny
Imagine you have a grid with a 5 unit step, the grid will be drawn the same whether the camera is at (0,0) or (0,5) or (5,0) or (5,5) or...

So all you care about is the fractional offset - the remainder of the division of the camera''s position with the size of the grid (if your variables are integers, that would be offset = position % size;)

With floats:

float offset(float position, float gridsize)
{
position /= gridsize;
position -= floor(position);
position *= gridsize;
return position;
}


Make your grid slightly (one square) larger than the viewport, offset it by the appropriate offset, and you''re done.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
psyjax
psyjax
quote:
Original post by Anonymous Poster
y is the z parameter always 0?


yes, this is an orthographic projection

sorry. I should have mentioned that.
psyjax
psyjax
Fruny,

could you explain your post a bit more.

It sounds like exactly what I want!

You have gridsize, and position.

I use x_pos and y_pos for my camera position, the grid starts at 0,0 and goes to about 10 units each way.

So, how would I use your little equation to manipulate this?

Keep in mind that I use to variables (x and y) to keep track of the positions.
Fruny
Fruny
could you explain your post a bit more.

What don''t you understand? The problem and code are simple, really.

I use x_pos and y_pos for my camera position, the grid starts at 0,0 and goes to about 10 units each way.

So, how would I use your little equation to manipulate this?


float x_off = offset(x_pos, 10);
float y_off = offset(y_pos, 10);


Then use x_off and y_off as the camera position when rendering your grid.

Keep in mind that I use to variables (x and y) to keep track of the positions.

Your two variables are trivially independent. Think about it for a second.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
psyjax
psyjax
Ah! thanx.

Now I understand. I was a bit confused.

I think you nailed it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.