SpaceWars SceneItem implemenation

I've been going through the code files for the SpaceWars sample game. I was somewhat confused by the SceneItem class, because it inherits from List<SceneItem>. This throws me off because I wouldn't expect a "scene item" "is a" list of scene items. Does anyone have any insight into the logic here?

[340 byte] By [jgillin] at [2007-12-24]
# 1

SceneItems is a hierarchy - and can have child items below it. So it makes sense that a scen item is indeed a collection of other SceneItems

Yes this could have been handled buy having a member which contains the child items but its just a design choice I made.

TheZMan at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...
# 2
When I went through the code I did recognize it but I didn't think much about it
thx for your post........

I think it's a very elegant aproach to describe an object of one type containing a list of objects of the same type.
Beeing organized in a graph-like structure an object has only to know to update itself and to draw itself and tell it's children of his own type to do the same

Instead of objects defining a List-property the object itself inherits all typed list functionality
and everything else is added on top of that.
It's typesafe and good to traverse, the graph can be sorted easily and the virtual functions
make inheritance very easy.......

think that's the way c# and .Net is meant to be....

very cool.....

Shellacc at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...
# 3

Thanks for the reply.
Yeah, it's pretty neat.
For some reason, I just found it a little hard to get my head around.
I've just never come across this type of recursive type definition before.

jgillin at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...
# 4

Hi Zman,

This is a nice way of handling a SceneItem, one thing I was wondering though is the best way to propagate transformations through the heirarchy.

I can only think of simple approaches such as

public void Position
{
get
{
return Parent.Position + _Position;
}
set
{
_Position = value - Parent.Position;
}
}

This is a simple example and not even tried it however this is one way I thought about it by storing only the local position and returning the parent + local position as the absolute position

Any thoughts on this?

regards

Fluxtah

Fluxtah at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...
# 5
Seems like that would work. THe only problem I see is that by putting the calculation inside a property you force the calculation to be done avery time you access it. Normaly the hierarchical scene items such as position are updated once per frame with a walk of the tree. Then when you read the value there is no extra calculation.
TheZMan at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...
# 6

I see, that would be a problem, I need to think up an elegant way to do it once per frame.

Fluxtah at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...
# 7
I've added function to update the position like:

public void updatePostion(Vector2 deltaP)...(it's 2D stuff I'm working on)

I calculate the delta every frame and call the function, then the Position-Property is accessible
without side-effects...there could be the place to calculate once but I guess you have to add a
member more to store all data; parent postion, local offset and one for the sum of both...
this frees property-access from calculating each time....

maybe this idea helps,

cheers

ShellaccOverdrive at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...
# 8
Position + parent.Position isn't going to work if the parent is allowed to rotate (and the children are supposed to rotate with it).

In general, in hierarchical scene graph, you will end up implementing a hierarchical "dirtying" system, where all children of a node being "dirtied" will be dirtied as well (recursively). You only need to dirty down until you find a child that's already dirtied, because it's transitive.

When a node is asked about its world transform, and it is dirty, it re-calculates it by multiplying the parent transform with its local transform, and clearing its dirty bit.

For culling, you probably want to propagate bounding information from the children, up through the parent -- again, if you want to create a traditional, hierarchical scene graph.

JonWatte at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...
# 9

I almost understand what you are saying but one thing that confuses matters is how to set the position or rotation of a child in relation to the parent object.

If I had a SceneItem s and set s.Position = 50.0f; should that be the position in the world or relative to the parent?

Would it be an idea to have two ways to set / get the position, maybe WorldPosition and RelativePosition? I am not sure, maybe I need to experiment rather than ponder.

My first game is a 3d arkanoid clone so it does not need to have this kind of relationship between scene graph nodes, although when I move on to a more adventurous game I would want a more robust scene graph.

For culling I would probably use an octree as I would like to make a space game for my next game and that would also serve as a good collision system, although I would probably only start with a space dog fight first :)

Fluxtah at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...
# 10
In a minimal scene graph, you always set the position relative to the parent, so if your parent rotates, you will revolve around the parent. Think of a hand holding a sword; when the elbow is moved, the sword hilt rotates around the elbow.

In slightly fuller-featured scene graphs, you can set (and get) the position either relative to the parent, or relative to the world. When setting the position relative to the world, you either set a flag saying "don't inherit parent position/rotation," or you get the parent transform matrix, and transforms the incoming world position by the inverse of that matrix, to get your new local position. The difference is in what happens if the parent moves/rotates after the child has been set in world space.

JonWatte at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...