Robin Theilade

Hello good people,

I'm doing my own definition of IDrawable and IUpdatable mainly because none of them has setters in their properties.

While doing an implementation of the IDrawable interface I thought that it was weird that it did not have any position property. Usually when drawing something you'd like to know where to draw it.

My first thought was that the difference between the coordinate sets in 2D and 3D graphics, but now that we got generics I could go ahead and implement it like this:

public interface IDrawable<TPos>
{
TPos Position
{
get;
}

// ... code left out
}

Can anyone give me a good reason for not adding a position property to my IDrawable interface

Best regards,
Robin Theilade


Re: XNA Framework IDrawable interface and a position

qrli

It all depends on what the interface is intended for. Your interface seems designed for individual objects. But the interface in XNA is for components, that means one component may render a whole sprite layer or the whole UI.




Re: XNA Framework IDrawable interface and a position

S.

Personally i think something like IDrawable should do exactly what the name says, Draw() and nothing more.

interface IDrawable
{
void Draw();
}

Because not everything that can be drawn has a position. Also why just Position, why not Rotation and Scale Starts to get off topic of the interface i think.

The class that implements IDrawable is the one that will decide whether it needs a Position, Rotation, Scale etc.




Re: XNA Framework IDrawable interface and a position

Robin Theilade

qrli

You're actually right about my intentions about what I planned to use the interface for and I have thought about it and come to the conclusion that the interfaces shouldn't be the same for scene objects and game components.

Although the the difference in definition exists, I still see the two kinds as one outside the XNA framework. But inside the XNA framework there is a difference and therefore I should define two different interfaces or at leat the one for the scene objects.

But let's say that you have that game component that renders whole sprite or UI layers, the position could still exist as a position
offset like location on a System.Windows.Forms.Label is relative to its parent. My point is that it is only in rare cases where you actually have no position for a game component that implements IDrawable, but maybe I'm wrong

S.

I see where you're going but in that case I'd say that the interface name should be IDraw :) Visible and DrawOrder can prove to be very usefull for drawing logic and I would hate to define additional interfaces for the properties like IVisible, IDrawOrder and IPosition. Also DrawOrder and Visible is thought of being checked before the Draw method might be called by some kind of parent/owner object. So the object would need a way to make sure that the properties are available, and the interface is a good way to solve that.

Moving on, I think that you are right about rotation and scaling. They should also be there if position is.

Best regards,
Robin Theilade




Re: XNA Framework IDrawable interface and a position

S.

"My point is that it is only in rare cases where you actually have no position for a game component that implements IDrawable, but maybe I'm wrong "

Yer true, 99% of stuff that needs to be rendered will probably have a position :)


"
I would hate to define additional interfaces for the properties like IVisible, IDrawOrder and IPosition"

Hm, i've done that in my engine, i've got things like: IPosition, IRotation, IScale, IVisible, IFrustumCullable, ISelectable. I guess both ways have there good points and bad points :)




Re: XNA Framework IDrawable interface and a position

Robin Theilade

S.

Well it isn't necessarily a bad design, I just meant that I wouldn't specialize the interfaces to such an extent, my post was actually more about generalizing the interface a bit more.

I guess there is two things in this: flexibility and lazyness. I'm probably more in the lazy end of the scale :)

Just out of curiosity, does these interfaces of yours define getters and setters or only setters

Best regards,
Robin Theilade





Re: XNA Framework IDrawable interface and a position

qrli

Interface is generally more difficult to design because we always want them to fit in more situations, and that's why it is called interface. So my rule is that don't design an interface unless absolutely necessary. Yes, you can design a IDrawable with a position and used for either case, but is there enough benefit to access position through IDrawable Will there be many functions operate on both a game component and a game unit I don't think so.

And for the generic question, I don't think it's a good idea. If it is used for 2D, then a generic parameter Vector2 may be enough. But for 3D, as S. said, you'll need Scale and Rotation soon.




Re: XNA Framework IDrawable interface and a position

Robin Theilade

qrli

Yes, it is difficult to think about every possible aspect of an interface that you might need in the future.

I'd think that having the position, rotation and scaling in the interface is good on at least a game object since it would be easier to make relative position, rotation and scaling on game objects located in a hierarchy like this:

Car
- Wheel #1
- Wheel #2
- Wheel #3
- Wheel #4
- Left front door
- Right front door
- Left front seat
- Character
- Right front seat


But yes, maybe the game component wont need those properties. So the solution might be to leave the XNA IDrawable for game components and use my own IDrawable for game objects.

To solve 2D/3D problem with the interface I could do it like this

public interface IDrawable<T>
{
T Position { get; set; }
T Rotation { get; set; }
T Scaling { get; set; }

bool Visible { get; set; }
int DrawOrder { get; set; }

void Draw();
}

Best regards,
Robin Theilade





Re: XNA Framework IDrawable interface and a position

PJ. van de Sande

It is better to create an master interface implements others. For example:


public interface IGraphicsObject<T> : IDrawable<T>, IPositionable<T>, IScalable<T>
{
}






Re: XNA Framework IDrawable interface and a position

Robin Theilade

PJ. van de Sande

That's not a bad idea, thanks!

Best regards,
Robin Theilade




Re: XNA Framework IDrawable interface and a position

S.

PJ. van de Sande wrote:

It is better to create an master interface implements others. For example:


public interface IGraphicsObject<T> : IDrawable<T>, IPositionable<T>, IScalable<T>
{
}



yer that is a good way. that way you can keep the fine tooth abstraction, but bundle common types :)