• Advertisement
Sign in to follow this  

Need some help with code structure design

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

I am writing a small game using the XNA Framework. It is a 3D game with some 2D objects in it. I would like to use a base class and inheritance since there are a few attributes that all objects will share, as well as dynamic binding etc, but I have some problems setting this up in a nice way. All objects have the following attributes: Position Rotation Scale Active The 2D objects have the following attribute: Texture The 3D objects have the following attribute: Model The movable objects have the following attribute: Velocity Right now I have it set up in the following way: GameObject Statid2D : GameObject (texture) Movable2D : Statid2D (velocity) Statid3D : GameObject (model) Movable3D : Static3D (velocity) This structure feels a bit awkward though. Suggestions on improvements? I would also like to do a base Projectile class that inherets from one of the classes above. A problem here is if I want to be able to create both 2D and 3D projectiles. I am not sure how to best solve this, but problem might be automatically solved with a better structure of my GameObjects hiearchy above.

Share this post


Link to post
Share on other sites
Advertisement
So, your base class looks like this (in pseudo-c++):

class BaseObject
{
protected:
Vector position;
Vector rotation;
Vector scale;
bool active;

public:
BaseObject() { };
}


Then, to create your 2D and 3D objects:


class Object2D : public BaseObject
{
protected:
Texture texture;
}

class Object3D : public BaseObject
{
protected:
Model model;
}


Then, to solve your problems of velocity, just use a const velocity of 0 on static objects. Or, just have a "static" boolean property of the base object.

[Edited by - m0nkfish on January 12, 2009 11:16:01 AM]

Share this post


Link to post
Share on other sites
But putting a velocity attribute into all objects will cause memory to be allocated for objects that will not use it. That will probably not be a problem for my game, but it feels like non-good programming practice?

Share this post


Link to post
Share on other sites
Perhaps you could implement velocity as a pointer to heap-allocated memory and assign it to NULL when static (as opposed to 0.0,0.0,0.0 when dynamic but not moving)

Share this post


Link to post
Share on other sites
Which, in C#, would be

class BaseObject {
public Vector3? Velocity;
}


Notice the ?. You can assign it to 'null' or to a Vector3.

However, using heap allocated memory in this way is not a good idea in XNA because it keeps the garbage collector busy. The 4 saved bytes (8 bytes versus 12 bytes since the reference will have at least a pointer to the boxed structure plus a mutex handle for C#'s lock statement) are not worth the effort.

You will save a few bytes of memory but the indirection will hurt performance slightly and produce a small amount of garbage when you release objects. Since you mentioned projectiles, I'm going to assume you'll potentially have a lot of these objects being created and destroyed.



My recommendation: Put your effort in a clean design, where clean is defined as working, readable and maintainable. Don't waste memory, but don't micro-optimize (either memory or performance) until you're seeing a problem.

Your might create a class hierarchy like this:


public abstract class Actor {
protected Matrix Transform; // <- Position, Orientation + Scale
}
public abstract class DynamicActor : Actor {
protected Vector3 Velocity;
protected Vector3 RotationalVelocity;
}


But use your own judgement.

Share this post


Link to post
Share on other sites
Thanks for the replies.

I will simplify the hiearchy a bit, "wasting" a bit of memory but making things a lot easier. Was just hoping there was something I had missed that could make it look sexy. I do not think memory will be an issue, was just looking to get a better structure of it all if there was one.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sabo
Thanks for the replies.

I will simplify the hiearchy a bit, "wasting" a bit of memory but making things a lot easier. Was just hoping there was something I had missed that could make it look sexy. I do not think memory will be an issue, was just looking to get a better structure of it all if there was one.


You aren't wasting space. Even objects that don't move have a velocity... it just doesn't change. You never know when you might need to read that velocity (even though it doesn't change). It doesn't cost much of anything to keep it.

You could also make a distinction between "entities" and "terrain" or some other semantic distinction between things that don't move and are, therefore, part of the environment.

Share this post


Link to post
Share on other sites
No velocity????? oh really????


Right now you and your computer are blazing through space at thousands of miles per hour in relation to the sun. But In relation to the floor your velocity is 0.

Velocity is in relation to something else.

I'm not sure if your game will capture this level of realism though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement