2 Replies - 3456 Views - Last Post: 26 December 2008 - 11:20 AM Rate Topic: -----

#1 flocsy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 2
  • Joined: 26-November 08

Collision, C++, polymorphism, function overloading, coding style

Post icon  Posted 25 December 2008 - 02:40 AM

I'm writing my first game in C++. I'm trying to build some reusable classes that I'll be able to use in other games as well (some kind of "Mini Game Engine")

I have a manager object that keeps a pointer to every Sprite, and a pointer to the Sprite that has the focus (the player). It checks in a loop if the player collides to any other sprite, and if it does the I would like to collide them.

The player, and the other objects inherit from class Sprite. The problem is that I have both polymorphism and function overloading (maybe I shouldn't?), but I can't make it work.

I tried to make Collision a friend function, make it a virtual member function, but no matter what every time when Player collides let's say a GoodObject, Sprite::Collision(Sprite&) is called and I'd like Player::Collision(GoodObject&) to be called.

I understand that the problem is that polymorphism and function overloading in the child class don't fit together, because in the derived class the Collide function(s) don't have the same footprint as in the base Sprite class.

What techniques could I use to make the collisions? Should I use dynamic_cast? Is there nicer way to do it?

The code is something like this:

class Sprite // basic sprite
{
...
public:
	bool IsCollision(const Sprite &other) const;
	virtual Collide(Sprite &other); // for the polymorhism
};

class GoodObject : public Sprite
{ // colliding this would give the player extra points
...
};

class BadObject : public Sprite
{ // colliding this would kill the player
...
};

class Player : public Sprite
{
public:
	virtual Collide(GoodObject &other); // function overloading
	virtual Collide(BadObject &other);
};

class Manager
{
public:
	void Loop(void);
private:
	std::list<Sprite*> sprites;
	Sprite *focus;
};

void Manager::Loop(void)
{
	while(loop)
	{
		for (std::list<Sprite*>::iterator i = sprites.begin(); i != sprites.end(); ++i)
		{
			Sprite &sprite = **i;
			sprite.Step(); // move the sprite, change animation frames
			if (&sprite != focus && focus->IsCollision(sprite))
				focus->Collide(sprite); // this is the PROBLEM
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Collision, C++, polymorphism, function overloading, coding style

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,684
  • Joined: 16-October 07

Re: Collision, C++, polymorphism, function overloading, coding style

Posted 25 December 2008 - 05:43 AM

Even though you're implementing:
virtual void Collide(BadObject &other);



Doesn't mean you're overriding
virtual void Collide(Sprite &other);



You need to have the matching call to override. So match the call and then treat different instances as needed.

Strangely, there is no "typeof" method in C++ by default. But you can fake it, particularly if you're already useing a base class. All you need to do is give the class an id of some sort.

e.g.
class Sprite {
public:
	bool IsCollision(const Sprite &other) const;
	// needs a return type
	virtual void Collide(Sprite &other); // for the polymorhism
	virtual string getTypeName() const { return "Sprite"; }
};

class GoodObject : public Sprite {
public:
	virtual string getTypeName() const { return "GoodObject"; }
};

class BadObject : public Sprite {
public:
	virtual string getTypeName() const { return "BadObject"; }
};

class Player : public Sprite {
private:
	void ProcessCollide(GoodObject &other);
	void ProcessCollide(BadObject &other);
	
public:
	void Collide(Sprite &other);
	virtual string getTypeName() const { return "Player"; }
};

void Player::Collide(Sprite &other) {
	string otherName = other.getTypeName();
	if (otherName=="GoodObject") {
		this->ProcessCollide((GoodObject &)other);
	} else if (otherName=="BadObject") {
		this->ProcessCollide((BadObject &)other);
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Re: Collision, C++, polymorphism, function overloading, coding style

Posted 26 December 2008 - 11:20 AM

Also, there's many different kinds of hurt and good things out there. For example, I have an Entity object which is ANYTHING that has a physical manifestation in the game world. Then, I have an Actor object which is anything that walks like a character and gets animated that way. Then, I would have an Enemy object (not all enemies that touch you can kill you) that has it's event check in case the player impacted it.

There's many different ways the player could get hurt, but the player will get hurt so it's great to have a player.takeDamage(amt) function to make the player get knocked back, take off some HP, ect.

That way, in your game engine you're just checking all the objects to see which are colliding with each other, and every object has an event function. This makes it easy to seamlessly add all sorts of different kinds of objects that do many things without you having to check a list of object names for that purpose.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1