Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Sappharos

Member Since 29 Jun 2008
Offline Last Active Oct 04 2013 05:08 AM

#4858011 Managing Game Entities

Posted by Sappharos on 05 September 2011 - 06:17 PM

I created classes for physics objects, but I think it could use some more optimization and cleaning up. One thing I don't like is the GetType() method and the types string comparison stuff. The code for updating, input, drawing, and other stuff for collisions is:

void World::OnContactBegin(b2Contact* pContact) {
	void* userDataA = pContact->GetFixtureA()->GetUserData();
	void* userDataB = pContact->GetFixtureB()->GetUserData();
	PhysicsEntity* entityA = static_cast<PhysicsEntity*>(userDataA);
	PhysicsEntity* entityB = static_cast<PhysicsEntity*>(userDataB);
	if(entityA->GetType() == "PhysicsEntity" && entityB->GetType() == "PhysicsEntity") {
		if(a)
			entA->OnContactBegin(entB);
		if(b)
			entB->OnContactBegin(entA);
	}
}

I have a class called Entity which contains the positions and sizes of the objects, a class called PhysicsEntity which inherits Entity and contains the box2d physics stuff, and a class World which contains the world simulation. This method calls all of the OnContactBegin() functions inside PhysicsEntity. If you want the full code for all of these classes to help me then I'd be happy to share it.

The thing I don't like is calling GetType() == "PhysicsEntity", each entity has a string type assigned to it and I assign it through the constructor of Entity(), and Entity has a GetType() method to return the string containing its class type, and it feels hacky to me and I'm wondering if there is a better way to do it.

Anyone willing to help me further optimize my entity management system please respond! Thanks :)


At the very least you'll want to use numerical constants instead of strings. So use const int PHYSICS_ENTITY = 5 (or whatever); change the return type of GetType() and then test if(entityA->GetType() == PHYSICS_ENTITY... That way you're not doing array comparisons for each entity.


#4834243 Demise of the "game entity".

Posted by Sappharos on 12 July 2011 - 05:27 AM

Imagine if all the standard-library objects had a common base type -- i.e. std::string, std::iostream and std::vector were all "entities"...
Why? Why would you do such a thing! They're completely different types that can be composed to make more complex types. There's no requirement for having an Entity* which might point to either a string or an iostream.


I don't know much so I may be missing the point of the conversation here, but surely the purpose of an entity object is to serve as a base class so that all objects in the game can be held in the same container? Otherwise you have a list of trees, a list of monsters, a list of players, a list of particle systems etc., all with similar interfaces, but which need to be accessed seperately...


#4829240 Data Coordination Between Server & Client

Posted by Sappharos on 29 June 2011 - 02:51 PM

I can help with one question.

When comparing two distances simply to see which is the larger, you don't need the square root. Use
if (x1*x1 + y1*y1 + z1*z1 > x2*x2 + y2*y2 + z2*z2)
instead of
if (sqrt(x1*x1 + y1*y1 + z1*z1) > sqrt(x2*x2 + y2*y2 + z2*z2))

It may speed up some calculations - in theory. :)

As for forged packets, never trust the client. That is all.


#4828584 Managing A Large Tilemap [ ][ ][ ][ ][ ]

Posted by Sappharos on 28 June 2011 - 03:12 AM

Don't give up on the thread. I will post again later on when I have time. :)


PARTNERS