Jump to content
  • Advertisement
Sign in to follow this  
lodoss118

Collision using vectors of characters

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

Hi i want to know how i should handle collision detection in my game using vectors of characters (note it is an scrolling beat em up engine) I have a base class called character in which all chars in game inherit. In it i have a method for collision:-
bool checkHit(Character *target) // this is in character base class
{
   for(int i = 0; i < NUM_BBOX; i++)
   {
 	if(collide.ppsrite(&this->getBbox.getsprite(), target->getBbox- >getSprite()) // actual collision
	{
		if(this->getPosZ() >= target->getposZ() - z_TARGETAMOUNT && (this->getPosZ() <= 	

			target->getposZ() + z_TARGETAMOUNT) 
		{
			if(this->getPosZ() < -1) // if going down and hit then stop is this right?
			{
				this->move(0, +this->getspeed();

			}
			//etc
			return true;

		}

	}
	else return false;
   }
}


now in my renderManager i have a vector of characters what i wanted to know is how do make sure it doesn't collide with itself and does collide with other characters. renderManager() :- basic concept of collision in renderManager how should i handle this

bool checkCollision(Character *target) // this is in renderManager class?
{
  vector<Character*>::iterator iCharacter;
  for loop
   if target == (*iCharacter)
   contine
  //call character collision?
  (*iCharacter)->checkHit(i*Character); //is this even right?

}



Share this post


Link to post
Share on other sites
Advertisement
Not quite sure what i*Character is trying to achieve. I assume that was a typo. Let's have a look at some actual code for that function.


// assume std::vector<Character*> Characters;

void checkCollision(Character *target)
{
vector<Character*>::iterator iCharacter=Characters.begin();
while(iCharacter!=Characters.end())
{
if(*iCharacter!=target) // try to avoid continue where you can
{
if(target->checkHit(*iCharacter))
{
HandleCollision(target,*iCharacter); // or whatever
}
}

++iCharacter; // your continue would have skipped this line
}
}







Having your function return a bool when it finds a collision would mean that it would then stop checking for collisions against subsequent characters, which may not be what you want.

I'd also suggest that iCharacter is an absurdly long name for a local variable and that your code would be far easier to read and maintain if you were to just use 'i', but that is perhaps just a personal preference.

I also don't see what place collision checking has in a RenderManager class. Try to avoid confusing responsiblities like that.

Share this post


Link to post
Share on other sites
thanks mate, i was only testing it in render manager to lazy to create a test class. Also for example if i have 2 players in the game how can i have two players not hit each other or hit each other, in my character class i have a charType varibale which evaluates to PLAYER, ENEMY etc so how could i implement this in collision with players, i.e players are chars

Share this post


Link to post
Share on other sites
Use the same functions that you use to test the player against each enemy in the vector to test each player against each other, just do it explicitly.

Either that or just have one vector of game objects, of which player and enemy are different types.

I would just point out that, since you are already using inheritance anyway, why have a type-field for players and enemies etc? Why not have player and enemy as seperate classes that inherit from the same base and use virtual functions to express the differences between them?

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!