Jump to content
  • Advertisement
Sign in to follow this  
Aztral

Using a grid

This topic is 3055 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 using a grid to implement the world in my game. The grid is used for path finding mostly and a few other things. My question comes because the world is rather large (my grid is something like 800x600) which obviously uses a ton of memory and doesn't seem all that efficient. This means that when I scroll the world I have to move 480,000 different squares. Is there a better way I can be going about using a grid?

Share this post


Link to post
Share on other sites
Advertisement
you can break up your grid into sections and only deal with the current section of the grid (or neighboring grid cells if you get near an edge or a corner).

Im confused though when you said "I have to move 480,000 different squares".

Why do you have to move them? move the player, not the grid, right? (unless I'm missing something)

Share this post


Link to post
Share on other sites
I have to move all the squares when the world scrolls. If the player is "moving" right, the world scrolls to the left. I have to move all of the squares representing the world.

I'm not sure how I can split up the grid and only use pieces of it at a time. For example if I wanted to I could tell the player to move from one corner of the world to the other, in which case I would need to know about the entire grid for path finding purposes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Aztral
I have to move all the squares when the world scrolls. If the player is "moving" right, the world scrolls to the left. I have to move all of the squares representing the world.

I'm not sure how I can split up the grid and only use pieces of it at a time. For example if I wanted to I could tell the player to move from one corner of the world to the other, in which case I would need to know about the entire grid for path finding purposes.


try to limit the amount of distance your path finding is required to do, ie don't let something travel from one end of the map to the other. Google quadtree's or portal maps, most of the design are meant for 3d but can be helpful in comeing up with ideas to splitting your world up into manageable sections.

And I'm not sure what you meant by you have to move the entire map when your character moves?

Also when it comes to pathfinding, you could take your sections and look at them like a grid, what sections will the player move through in order to make it from one point to another, and only load those sections.

Share this post


Link to post
Share on other sites
I'll look into those, thanks.

Regarding moving the entire world:

Say one grid square is initially at the point (10, 10) on the screen. Now say I want to scroll 5 pixels to the right. If I scroll the world 5 pixels to the right, then that square is now at (5, 10), correct? I don't know how I can move the world on the screen without modifying each square's x,y screen coordinates.

Share this post


Link to post
Share on other sites
Quote:

Also when it comes to pathfinding, you could take your sections and look at them like a grid, what sections will the player move through in order to make it from one point to another, and only load those sections.


Ok but then it seems like I'd be loading/deallocating big chunks a LOT of the time.

Also, say I wanted to have a minimap, and when I click on a location on the minimap the "camera" zooms to that location.

Do I have to load big chunks of the world when I want to scroll/zoom to a different location?

Share this post


Link to post
Share on other sites
Quote:
Original post by Aztral
Say one grid square is initially at the point (10, 10) on the screen. Now say I want to scroll 5 pixels to the right. If I scroll the world 5 pixels to the right, then that square is now at (5, 10), correct? I don't know how I can move the world on the screen without modifying each square's x,y screen coordinates.

What graphics API are you using? OpenGL, DirectX, GDI, etc? If you are using OpenGL or DirectX you can "translate" the grid which is more efficient that moving each square at a time. Or, similarly, you can move the "camera" which is basically the same as translating.
Please note that i am not an expert at graphics so my terminology might be a bit off.

Share this post


Link to post
Share on other sites
Associate coordinates with your player, and change those when you want to move around. Render the world with these coordinates in mind, i.e., the camera follows the player.

Pseudo-code:


class Player {
// Location in the world space
int x, y;
}

function World.Draw(Player focus) {
for(int x from -screenWidth / 2 to screenWidth / 2)
for(int y from -screenHeight / 2 to screenHeight / 2)
// Check if this offset location is valid
if(IsInWorldRange(x + focus.x, y + focus.y))
// Retrieve the offset tile from the grid, and show it at (x, y)
Draw(world[x + focus.x][y + focus.y], x, y);
}


This can of course be orthogonal to any chunking you add to map loading. Make chunk sizes some reasonable multiple of the amount of tiles across/down that can be displayed on a screen at once. You would need at most 4 chunks in memory at one time (when the camera was positioned at the corner of a chunk boundary).

Share this post


Link to post
Share on other sites
Thanks for the replies everyone.

Is this a reasonable solution?

- In my GridNode object I will have two static integers dx and dy, which reflect the x change and y change in the world on-screen location.

- I will make a quadtree to split my world up into manageable chunks and before I render a frame I will check if I need to worry about drawing each chunk.

- If I do need to worry about a specific chunk, I will update each GridNode in that chunk such that it's x and y reflects it's current dx and dy.

This would be considerably more efficient than modifying every single GridNode in the grid so that it's x and y reflect the world dx and dy every frame, right? As mattd said I'll only have to worry about 4 chunks at a time.

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!