Sign in to follow this  
lodoss118

would this work?

Recommended Posts

basically i am trying to see if my collisonManager would work like this, say for example i have a base class called character in which classes such as enemy and player inherit from now in my collision manager could i do this:-

#ifndef COLLISIONMANAGER_H
#define COLLISIONMANAGER_H

#include "player.h"
#include "Enemy.h"
#include "BoundingCOllision.h"
#include <vector>

#define Z_HIT_RANGE 135
#define X_HIT_RANGE 130

using namespace std;

class CollisionManager
{
public:
	CollisionManager();
	~CollisioManager();

	void addSprite(Character *pType);

	float diffZ(float z1, float z2);

	void playerWithEnemies(Player *p, Enemy *e);

	//final check
	void checkCollisions();

private:
	vector<Player *, Enemy *> m_vSprite;
	BoundingCollision *collide;
	

};

#endif

#include "CollisionManager.h"

CollisionManager::CollisionManager()
{
	collide = new BoundingCollision();
}

CollisionManager::~CollisionManager()
{
	delete collide;
	m_vSprite.~vector<Player *, Enemy *>(); 
}

void CollisionManager::addSprite(Character *pType)
{
	m_vSprite.push_back(pType);
}

float CollisionManager::diffZ(float z1, float z2)
{
	return (z1 >= (z2 - Z_HIT_RANGE) && (z1 <= (z2 + Z_HIT_RANGE))); 
}

void CollisionManager::playerWithEnemies(Player *p, Enemy *e)
{
	//for(int i = 0; i < NUM_BBOX; i++)
	//{
		// body bounding box
		if(collide->PPSpriteCollision(&p->getBbox(0)->getSprite(), 

&e->getBbox(0)->getSprite()))
		{
			// fix this part needs amendment
			if(diffZ(p->getPosZ(), e->getPosZ()))
			{
				// up
				if(p->getDirZ() < -1)
				{
					p->move(0, +p->getSpeed());
				}
				// up and right
				else if(p->getDirZ() < -1 &&  p->getDirX() > 1)
				{
					p->move(0, +p->getSpeed());
					p->move(-p->getSpeed(), 0);
				}
				// up and left
				else if(p->getDirZ() < -1 &&  p->getDirX() < -1)
				{
					p->move(0, +p->getSpeed());
					p->move(+p->getSpeed(), 0);
				}
				// down
				else if(p->getDirZ() > 1)
				{
					p->move(0, -p->getSpeed());
				}
				// down right
				else if(p->getDirZ() > 1 &&  p->getDirX() > 1)
				{
					p->move(0, -p->getSpeed());
					p->move(-p->getSpeed(), 0);
				}
				// down left
				else if(p->getDirZ() > 1 &&  p->getDirX() < -1)
				{
					p->move(0, -p->getSpeed());
					p->move(+p->getSpeed(), 0);
				}
			}

			if(diffZ(p->getPosZ(), e->getPosZ()))
			{
				// behind p2 sprite
				if(p->getPosX() > (e->getPosX() - X_HIT_RANGE))
				{
					if(p->getDirX() < -1)
					{
						p->move(+p->getSpeed(), 0);
						e->move(-e->getSpeed(), 0);
					}
				}
				// infront of p2 sprite
				else if(p->getPosX() < (e->getPosX() + X_HIT_RANGE))
				{
					if(p->getDirX() > 1)
					{
						p1->move(-p->getSpeed(), 0);
					}
				}
			}
		} 
	//}	
}

void CollisionManager::checkCollisions()
{
	vector<Player *, Enemy *>::iterator isprite;

	for(isprite = m_vSprite.begin(); isprite != m_vSprite.end(); isprite++)
	{
		playerWithEnemies((*iSprite), (*iSprite))		
	}	
}


Share this post


Link to post
Share on other sites
I took a quick look through your source. The first thing I noticed is the declaration of your vector;
vector<Player *, Enemy *> m_vSprite;
This won't work (unless Enemy is an allocator, which I doubt). See here

BoundingCollision *collide;
Why is this a pointer? Don't allocate memory dynamically unless you have to.

m_vSprite.~vector<Player *, Enemy *>();
Again, why do you call the destructor? It will be called automatically, as m_vSprite isn't a pointer. (read this)

My biggest concern though: did you try to compile the source before you posted it? Things like
vector<Player *, Enemy *> m_vSprite;
make me doubt it.

Share this post


Link to post
Share on other sites
If you want Player and Enemy in the same vector, you should probably make them both derive from the same class, say Entity, or Sprite (given the name of your vector). Then, make a vector of that class: std::vector<Entity*> entities; Then, add both the player and the enemies to the vector.

Edit: This is assuming that enemies and playeres have something in common. If they don't, you probably shouldn't be doing this, and should be using two vectors or soemthing.

Share this post


Link to post
Share on other sites


#ifndef COLLISIONMANAGER_H
#define COLLISIONMANAGER_H

#include "player.h"
#include "Enemy.h"
#include "BoundingCOllision.h"
#include <vector>

#define Z_HIT_RANGE 135
#define X_HIT_RANGE 130

using namespace std;

class CollisionManager
{
public:
CollisionManager();
~CollisioManager();

void addSprite(Character *pType);

float diffZ(float z1, float z2);

void playerWithEnemies(Player *p, Enemy *e);

//final check
void checkCollisions();

private:
vector<Character *> m_vSprite;
BoundingCollision collide;

};

#endif

#include "CollisionManager.h"

CollisionManager::CollisionManager()
{

}

CollisionManager::~CollisionManager()
{

}

void CollisionManager::addSprite(Character *pType)
{
m_vSprite.push_back(pType);
}

float CollisionManager::diffZ(float z1, float z2)
{
return (z1 >= (z2 - Z_HIT_RANGE) && (z1 <= (z2 + Z_HIT_RANGE)));
}

void CollisionManager::playerWithEnemies(Player *p, Enemy *e)
{
//for(int i = 0; i < NUM_BBOX; i++)
//{
// body bounding box
if(collide.PPSpriteCollision(&p->getBbox(0)->getSprite(), &e->getBbox(0)->getSprite()))
{
// fix this part needs amendment
if(diffZ(p->getPosZ(), e->getPosZ()))
{
// up
if(p->getDirZ() < -1)
{
p->move(0, +p->getSpeed());
}
// up and right
else if(p->getDirZ() < -1 && p->getDirX() > 1)
{
p->move(0, +p->getSpeed());
p->move(-p->getSpeed(), 0);
}
// up and left
else if(p->getDirZ() < -1 && p->getDirX() < -1)
{
p->move(0, +p->getSpeed());
p->move(+p->getSpeed(), 0);
}
// down
else if(p->getDirZ() > 1)
{
p->move(0, -p->getSpeed());
}
// down right
else if(p->getDirZ() > 1 && p->getDirX() > 1)
{
p->move(0, -p->getSpeed());
p->move(-p->getSpeed(), 0);
}
// down left
else if(p->getDirZ() > 1 && p->getDirX() < -1)
{
p->move(0, -p->getSpeed());
p->move(+p->getSpeed(), 0);
}
}

if(diffZ(p->getPosZ(), e->getPosZ()))
{
// behind p2 sprite
if(p->getPosX() > (e->getPosX() - X_HIT_RANGE))
{
if(p->getDirX() < -1)
{
p->move(+p->getSpeed(), 0);
e->move(-e->getSpeed(), 0);
}
}
// infront of p2 sprite
else if(p->getPosX() < (e->getPosX() + X_HIT_RANGE))
{
if(p->getDirX() > 1)
{
p1->move(-p->getSpeed(), 0);
}
}
}
}
//}
}

void CollisionManager::checkCollisions()
{
vector<Character *>::iterator isprite;

for(isprite = m_vSprite.begin(); isprite != m_vSprite.end(); isprite++)
{
playerWithEnemies((*iSprite), (*iSprite))
}
}


Share this post


Link to post
Share on other sites
hi i compiled the above code and got this error?

\CollisionManager.cpp(108) : error C2664: 'CollisionManager::playerWithEnemies' : cannot convert parameter 1 from 'Character *' to 'Player *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

it is complaining about this part



void CollisionManager::checkCollisions()
{
vector<Character *>::iterator iSprite;

for(iSprite = m_vSprite.begin(); iSprite != m_vSprite.end(); iSprite++)
{
playerWithEnemies((*iSprite), (*iSprite));
}
}




what i don;t understand is that player and enemy are both derived from character the class so what am i doing wrong, sorry for being a newbie.

Share this post


Link to post
Share on other sites
Listen to your compiler. It tells you that it can't convert a Character* into a Player* because a Character* could also be an Enemy*. Either make playerWithEnemies() accept Character * or use dynamic_cast to downcast a Character* into Player*.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this