Jump to content

  • Log In with Google      Sign In   
  • Create Account

stillLearning()

Member Since 10 Jun 2012
Offline Last Active May 11 2014 01:36 PM

Topics I've Started

How to aquire relevant data without iterating through vector containing all the data to...

08 March 2013 - 02:54 PM

Why hello there! How nice of you to come by. Have a seat.

 

My game is 2D-topdown and made in C++. Zeldaish. Love that game. ANYHO, I am trying to figure out how to make a vector contain only relevant Units. You might wanna skip this part and answer my question at once. No f*** you, read my wall of text. I'll try to explain with collisiondetection as example (have in mind this issue is not collision-specific, that's not what I am getting at..). I want to check if any Unit has collided with another Unit. I currently achieve this by doing this:

 

1. Create a vector which contain all Units.

2. Iterate through it so that all Units have checked collision with each other only once.

 

Now what if I have 30 Units in my world? If all 30 are to compare with each other only once, we got a total of 435 collisionchecks per frame, y = (x2 - x) / 2. This is inefficient. If there is 40 we get 780 collision checks.. It scales terribly. For some calculations the terrible scale isn't to much of an issue since the operation isn't complicated, but overall this will slow down my game if I don't come up with another way of comparing objects.

 

Instead I figured I'd like to create a member vector of relevant entities within the Unit - not a vector containing all Entities. This would greatly reduce the need of collisionchecks. Relevant entities would be entities that are close to the unit. I can take advantage of indexing, storing pointers to all Units in a 2-dimensional array that would represent and be identical to my 2-dimensional worldmap-array. Then it would be easy to grab some pointers to relevant, physically close, Units via indexing by coordinates and push them into a member vector of the Unit. Now this vector only contains other Units that is physically close to my unit and the result is that each unit only need to check collision with at most 4-5 Units, instead of all present Units.

 

In theory this would work out, assuming that creating the index isn't more inefficient than iterating through the vector of all Units (or items, or enemies or whatever object I have to access), but creating the index and maintaining it with moving objects etc is always slower when I try to implement it.

 

This issue persists with all kind of stuff within my game - not only collisiondetection. I actually got to write this thread since I'm currently at making Items storable within an inventory. When the Unit, my character, should pick up an item that is in front of him I only iterate through a vector of all items and check which one are within range. It'd be unealistic if my character could pick up an item from 100 meters. Well Slenderman has long arms, but this guy is more of a Gnome Urist McTransracialGnome. Same issue here - I want a vector that consists of relevant objects, not all of them. When combating enemies I iterate through a vector containing all enemies to check if the spoon sword hit any, instead of iterating through a vector containing all enemies that are likely to get hit. It's oblivious! Perhaps I shouldn't even iterate through a vector?

 

 

If you still haven't grasped my issue I'll make a final try: In Mount&Blade there is lots of players. When I shoot an arrow I can't imagine it iterates through all players every single frame and check if the arrow has collided with any of them? Surely there must be a more efficient way?! What way would that be? What alternatives to that way is there? Any design-pattern I should look into? (I did read GoF: Design Patterns but I grasped approx 10% of it the English language is inferior and nothing could be explained properly. Perhaps anyone have a Swedish translation of it?)

 

Seriously now, thanks for reading and I'd be REALLY happy if someone could point me in a good direction smile.png!


Dangling pointers in vectors

23 September 2012 - 04:47 PM

Hi there.



First thread on GameDev. First I shall say I am somewhat confused about my problem, hence my description might be lacking or difficult to understand. Ill try my best. In my game I have vectors of pointers to different objects, IE enemies & trees etc.

vector<Object*> allObjects;

void addObject(Unit* obj)
{
	allObjects.push_back(obj);
}

addObject(new Unit(...));
Using addObject allocates an Unit to allObjects. So far everything is fine.

During the lifetime of the Unit, lots of pointers to this Unit is created at several different locations in my program. Now when this particular Unit is deallocated, I end up having lots of vectors with dangling pointers.

The only way Ive come up with to solve this is to manually keep track of every single vector containing a pointer to the Unit by having a Vector of Vectors of Units.
[source lang="cpp"]vector<vector<Unit*>* > existsInVectors;[/source]
and then in the destructor of Unit calling existsInVectors.erase(iterator to Unit). For every "send unit to vector"-function, I need to call a "RemindUnitOfThisVector"-function... I find this solution ugly, inefficient and difficult to maintain...

What would be an efficient way of having multiple vectors pointing to an instance of Unit without manually tracking what vectors the Unit is stored in? If only vectors did autoerase on dangling pointers...

PARTNERS