Page 1 of 1

[SOLVED]Engine question

Posted: Fri Dec 10, 2010 3:59 pm
by like80ninjas
Hey, I have a game engine started and it's come along pretty far. They only issue I have is implementing a kind of global entity list for drawing and collision, and I was wanting to get some suggestions on how you guys handle the drawing and collision of all the entities or objects in your engines/games. Any help would be great, thanks!

Re: Engine question

Posted: Fri Dec 10, 2010 4:20 pm
by adikid89

Code: Select all

//EntityManager::Draw() .. or something similar
for(int i=0; i<entities.size();i++) {
  entities[i]->Draw();
}
For the collision... I'd recommend separating sprites from bodies(which will interact in the physics world). Look into a physics engine like box2d or something similar. I find that it's a great way to implement collision testing and physics in general.
So the bodies interact in a world, they collide, change pos accordingly etc, and all you do in the Entity class is update the pos of where you'll draw the sprite to match that of the body.

Re: Engine question

Posted: Fri Dec 10, 2010 4:45 pm
by GroundUpEngine
Agreed with above

I personally use Managers too... but its similar to this. Heres an aproach that is more simple than say.. a Entity manager (excuse the messy code)
But to turn this into a Manager class, the Manager class would handle the 'stack' and 'All()' style functions... Entity class would just handle the core functions. Making the Manager style easier and cleaner code ;)

Code: Select all

#include <iostream>
using namespace std;

#include <vector>

class Entity
{
public:
	static vector<Entity*> Entitys; // Shared stack of Entity's

	Entity()
	{
		Entitys.push_back(this); // Now on the Entitys stack...
	}
	~Entity()
	{
	}

	void Update()
	{
		// Check collision for this Entity
		cout << "Updating" << endl;
	}
	void Draw()
	{
        // Draw this Entity
        cout << "Drawing" << endl;
	}

	static void UpdateAll()
	{
        for(vector<Entity*>::iterator iter = Entitys.begin(); iter != Entitys.end(); iter++)
            (*iter)->Update();
	}
	static void DrawAll()
	{
		for(vector<Entity*>::iterator iter = Entitys.begin(); iter != Entitys.end(); iter++)
            (*iter)->Draw();
	}
};
vector<Entity*> Entity::Entitys;

int main()
{
	Entity bob; // Now on the Entitys stack...

	while(true) // Game loop
	{
		// Think

		// Update
		Entity::UpdateAll();

		// Render
		Entity::DrawAll();
	}

	cin.get();
	return 0;
}

Re: Engine question

Posted: Fri Dec 10, 2010 11:09 pm
by like80ninjas
Hey, thanks for the reply guys. I don't want to use box2d or anything like that, my collision code is written from scratch and works very well, however, you did answer my question on how to manage my entities. I like the idea of the static functions more than the extra class, I think i'll try implementing that.

Re: Engine question

Posted: Sat Dec 11, 2010 7:36 am
by GroundUpEngine
like80ninjas wrote:Hey, thanks for the reply guys. I don't want to use box2d or anything like that, my collision code is written from scratch and works very well, however, you did answer my question on how to manage my entities. I like the idea of the static functions more than the extra class, I think i'll try implementing that.
Sweet, let us know if it works ok 8-)

Re: Engine question

Posted: Sat Dec 11, 2010 9:25 am
by like80ninjas
Yeah the method groundupengine gave me was flawless. It works very well for my uses, thanks!

Re: Engine question

Posted: Sat Dec 11, 2010 3:05 pm
by GroundUpEngine
Awesome!
+1 to e-peen 8-)