   Good Way to Store Game Objects
  Hi again,I'm working on a simple 2D RTS, and I was wondering what the best way to store the units is. It seems like you could use a vector and whenever a unit was trained, push it onto the vector, but then how would you update the position of the unit? Whenever I've tried before the unit just stays in the same spot. Am I on the right track?Is there a better way to do this?Tell me if you need some code examples.Thanks   
  The unit's position should be an aspect of that unit; when you want to change the unit's position, you'd find it in whatever data structure you've stored it in, and say "Hey, you! Move to (x, y)!". What I'd recommend as far as data structures is to use a quad tree. Quad trees have a big advantage for 2D games in that they let you find objects that are close to other objects, without having to iterate over the entire set of objects. So a quad tree would let you easily do things like: * Find targets that are within range of a given unit * Figure out which units are inside of a given rectangle * Find which unit just got clicked onOf course, you'll probably want to keep track of all of the units on a given side, as well; for that, a vector would work well. For the most part you'd only be accessing that structure when you need to add or remove units from the army, which is infrequent enough that looking through the entire set of units is not a big deal.   ;Quote:The unit's position should be an aspect of that unit; when you want to change the unit's position, you'd find it in whatever data structure you've stored it in, and say "Hey, you! Move to (x, y)!". I had something similar to that:The Unit class had x and y members (both floats), and a move(float x, float y) function. I created a unit and added it to a vector<Unit>, and tried to move the unit with unitVec[0].move(1.0f, 1.0f). This move function works properly when the unit is not in the vector, but the unit in the vector did not move.Quote:What I'd recommend as far as data structures is to use a quad tree.I'll look into this, thanks.Quote:Quad trees have a big advantage for 2D gamesDo they also work for 3D games?   ;Quote:Original post by DasilQuote:Quad trees have a big advantage for 2D gamesDo they also work for 3D games?Yep, but it's called an Octree for 3D.;Quote:Yep, but it's called an Octree for 3D.Thanks!   ;Quote:The Unit class had x and y members (both floats), and a move(float x, float y) function. I created a unit and added it to a vector<Unit>, and tried to move the unit with unitVec[0].move(1.0f, 1.0f). This move function works properly when the unit is not in the vector, but the unit in the vector did not move.There's nothing particularly special about manipulating objects that are stored in a container such as std::vector. If this wasn't working for you, it's probably just because you were doing something wrong.(I know that's not particularly helpful, but without seeing the actual code we can't do much more than guess as to the actual cause of the problem.)   ;Quote:Original post by deadstarQuote:Original post by DasilQuote:Quad trees have a big advantage for 2D gamesDo they also work for 3D games?Yep, but it's called an Octree for 3D.One thing to remember is to make the distinction between a 3D game/World, and 3D graphics. You can render 3D character onto a 3D landscape that is inside a 2D World. If you cannot fly or move up/down in the world, then you are still 2D, and storage/access methods suitable to 2D are likely the best method.   ;  Thanks for the help so far.Ok, I have a Unit class that inheirits from a GameObject class.Unit.hclass Unit : public GameObject{public:Unit();virtual ~Unit();void create(std::string name, LPCTSTR filename);// unimportant stuff removedprivate:UnitState mState;int mHealth;};GameObject.hclass GameObject{public:GameObject();virtual ~GameObject();void set(const Vec2 &pos, float w, float h, const Vec2 &vel);float getWidth();float getHeight();Vec2 getPosition();Vec2 getVelocity();void move(float x, float y);virtual void initializeTexture(LPCTSTR filename, float width, float height, int index);virtual void draw(int frame);virtual void createTextures();void moveTo(Vec2 location);LPD3DXSPRITE getSprite();std::string getName();   //unimportant stuff removedprotected:Vec2 mPosition;Vec2 mVelocity;float mWidth;float mHeight;LPD3DXSPRITE mSprite;std::string mName;};Move is defined asvoid GameObject::move(float x, float y){mPosition.x += x;mPosition.y += y;}In the game, I created a Unit: Unit soldier;and set it up:Vec2 soldierPos(300.0f, 200.0f);Vec2 soldierVel(1.0f, 1.0f);soldier.set(soldierPos, 122.0f, 113.0f, soldierVel);soldier.createTextures();soldier.setName("Soldier");and added it to a vector<Unit> called unitVec (unitVec.push_back(soldier)), and tried to move it using:unitVec[0].move(unitVec[0].getVelocity().x, unitVec[0].getVelocity().y);This works and the unit moves properly if I do:soldier.move(soldier.getVelocity().x, soldier.getVelocity().y);Using the vector, the unit just stayed in the same spot.Tell me if you need more code.Edit - this is my first "big" game (past text-based and games like pacman), so I'm sure there are better ways to do things than what I'm currently doing, so if you can see anything from what I've shown you that I could do differently/more efficiently, please let me know. Thanks.[Edited by - Dasil on June 30, 2009 4:17:40 PM];  My best guess is that you are still trying to draw soldier after inserting it into the vector and moving it, when you should be trying to draw unitVec[0].   ;  Your problem sounds like you've managed to create a copy of the soldier, so that the one you're drawing and the one you're trying to move are different. However, the code you quoted shouldn't cause that behavior.Still, try giving a unique ID for every unit you create (you can do this by having a global int that you increment every time you make a new unit), and then print out soldier.id and unitVec[0].id.   