Jump to content
  • Advertisement
Sign in to follow this  
Hartley

A question about a gird based system.

This topic is 3411 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

Okay so far I have a Grid made up up 32x32 pixel 'cells' Im at you point at which I would like to add Players and NPC's, now I'm wondering (im using C#) Should I made the cells store a Player and NPC object? or should I make the Players/NPC's seperate and just use the Cells position coordinates... well Im just kindof stuck right now

Share this post


Link to post
Share on other sites
Advertisement
If you are planning on having lot of players/npcs (units), the cell structure is going to become bloated.

Also, if the units are stored in cells, accessing them becomes a bit of a hassle, as you have to know where they are at all times (i.e. storing them in a list). In which case, there isn't really a need to store them in an individual cell. You can instead store the cell position in the unit.

- me

Share this post


Link to post
Share on other sites
Okay so far, I have a multidimensional array for my grid, each array member holds an instance of type cell.

I'm having numerous difficulties, probably just because I'm really new to OO Programming in general, But I'm having SO much trouble getting my variables into other classes I have.

for example, I have a method called PlayerMovement in the PlayerCharacter class, derived from my Character class, and I need to access all the objects in my cells array, and add specific ones to a list. But I'm just completely lost as how to let my method use the cells array, which is in my Grid class, and gets instantiated in its constructor. because they are in different classes.

So I've also been thinking, instead of having a Multidimensional Array (I have the Index the same at the cells position, example 1,2 3,2 ect.) It would be easier for me to use a List of cells and just search for the position when I need it.

Could someone help me out here? whats the best way to implement this...
Im terrible at explaining things when I have been thinking for awhile.

Share this post


Link to post
Share on other sites
I'm not completely sure I fully understand your question, but I'll give it a try.

First of all, there's no "best" way, since everything has certain tradeoffs.

So when your player move it needs to know about the surrounding entities for things like collision detection?

For that purpose I'd provide the player object (or some other object that contains the logic) with a means to calculate the grid cell it is in. Then check all objects in that cell and maybe in neighboring cells es well.

This suggests that each cell holds a list of the players/NPCs that are in that cell for fast retrieval. If you'd just store the cell coords in each player/NPC object you'd have to iterate through the whole list and check the coords of ever player/NPC.

However, everything depends on the number of players/NPCs and the number of cells. The more cells you have the more storage you need if each cell holds a list (you might reduce that with a hashmap that uses cell ids as key and only keep non-empty cells around). If you'd store the cell id / coords in each player/NPC you could need less storage for small number but as numbers grow storage and processing times would increase.

Share this post


Link to post
Share on other sites
in a 2d tile based game that I was making I stored the units in a std::vector. Each tile also had a pointer to the unit that was in it.

Share this post


Link to post
Share on other sites
hmmm... I'm actually working on a small demo for 2D object partitioning using a regular grid.

This is what I've got so far.

Quote:



namespace SpacePartition2D
{
class Callbacks
{
public:
~Callbacks(){}
virtual bool traceQuery(int a)=0;
virtual bool areaQuery(int a)=0;
virtual bool broadPhaseQuery(int a, int b)=0;

};

// common interface for 2D object partitioning.
class Interface
{
public:
virtual ~Interface(){}

// debug render
virtual void debugRender()const=0;



// add object for spatial sorting.
virtual bool registerObject(int objectId)=0;

// remove object completly.
virtual bool unregisterObject(int objectId)=0;

// place object in the grid.
virtual bool placeObject(int objectId, float minx, float miny, float maxx, float maxy)=0;

// which object types we can collide with.
virtual bool setObjectCollidables(int objectId, int collidables)=0;

// change object type.
virtual bool setObjectType(int objectId, int type)=0;



// returns list of object within or nearby a rectangular area.
virtual void queryArea(int objectTypes, float minx, float miny, float maxx, float maxy, Callbacks& callbacks)const=0;

// returns list of object intersecting of nearby a ray-trace.
virtual void queryTrace(int objectTypes, float startx, float starty, float endx, float endy, Callbacks& callbacks)const=0;

// find all pairs of objects close to each other.
virtual void queryBroadPhase(int objectTypes, Callbacks& callbacks)const=0;
};

extern Interface* create2DGrid(int rows, int columns, float gridMinX, float gridMinY, float gridMaxX, float gridMaxY);
}



This is in the context of collision detection. You register objects first, then place it around the world, you remove the object once it's gone. The only link with the outside is the object id, which should be unique to each object, and their bounding box, which is re-placed very frame. All the object spatial information would be internal to the grid system, since it is irrelevant to the player.

I've got the implementation and everything, but it hasn't been tested at all yet. so I need to do that first.

[Edited by - oliii on May 20, 2009 10:11:48 AM]

Share this post


Link to post
Share on other sites
for collision detection/culling etc u should have the cells containing, (pointers/ids whatever) to the objects, perhaps an object is inside multiple cells

for the opposite u can easily do
vector<int> return_list_of_cells_this_object_is_within()

Share this post


Link to post
Share on other sites
Im actually designing a Final Fantasy Tactics style game, and Trying to make it when the user clicks on a player character, then the grid spaces he/she can move to will be highlighted, avoiding things such as rocks and enemies ect.

Each cell can only store one object, and Im working pathfinding like this.

Each cell knows which cell is directly to the right, left, up and down from it, and each cell stores a parent cell, when pathfinding starts every cell in the map within the characters max movement range is added to an -open- list, then the cell he is standing on is added to a -closed- list, open represents cells than have not yet been checked to see if the player can move to them or not, so i start with a loop, check the 4 squares adjacent to the starting square, each of those 4 squares assigns its parent to the square that called it, (in this case the starting cell, so it has an arrow pointing back to it) and then add those squares to the closed list, this will continue untill all squares on the grid are in the closed list, then depending on where the player clicks to move, pathfinding will just link all the cell parents back to the starting square and have the player take that path.

im just having a problem accessing my Cell objects from my Multidimensional Grid array because I suck at OO programming, so Im thinking It would be easier for me to implement them all into a list. If anybody wants to take a look at my code and critique me for it, then go ahead, maybe you can make heads or tails out of what Im doing.

SOURCE CODE:
http://two.xthost.info/hayloe/Tactics.zip

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!