Custom Control Program Design & Structure Problems

Hey everyone. I am having a software development issue with custom controls.



I am making a simple game where the units will have models with one or more spatials.

Specifically, I presently have a legoman, and a cow. The cow has only one spatial, while the legoman has several spatials (arms, legs, etc)

arranged in a scene-graph, and procedurally articulated.



I then wish to be able to click on a unit in the game, and tell the object that I clicked on that it was clicked on. I have code that will figure out the spatial that was clicked on, but I then need to determine the unit object that is associated with that spatial.



This seemed like the perfect place for custom controls. If the model I am using only has one spatial, I only need to have my unit’s class extend custom control, and attach my unit’s object (the one that I wrote that has all the cool stuff specific to my game) to the spatial. I can then easily retrieve the correct control.



The problem is that my models aren’t only one spatial. I am presently choosing some canonical “center spatial” and forcing the player to click that to get any response, but it seems like this shouldn’t be so hard.



It also seems like I am fundamentally abusing the whole construct of custom controls.



I decided that I wanted to be able to have a model object and a logic object associated with each unit. That is, my player will have his stats and skills and whatnot managed in one object, and then he will have another object which manages the spatials used, animation, etc.

(this means that both the animation and the logic classes extend abstract control)



This made sense to me because it shouldn’t be my player’s responsibility to know exactly how it walks, it should just know it needs to walk. How it walks might depend on various factors (e.g. perhaps the player is polymorphed? That would be the best example)



As a result of this, I have two controls attached the my center spatial object which need to communicate with each other using rather horrid syntax such as ((PlayerModel)this.spatial.getControl(Model.class)).walk(arg,direction);. Is this the way this was supposed to work?

I feel this must be a horrid misuse.



Then another problem is the method public Control cloneForSpatial(Spatial spatial)

The argument of spatial doesn’t make much sense for my example when my control could be a model & animation class which manages a large number of spatials.



I feel like custom control is really well designed for models that have only one spatial, and one simple logic class.



Could anyone cast light on these issues I have been having?



I have gotten the code to run just fine, but it’s not very easy to work with because despite the apparent organization and object oriented design, it doesn’t really make any sense.



Thanks in advance!

Um… why is your lego man made up of more than one spatial?



Most models in games are a single spatial, consisting of many polygons.



Anyway, as for the cow and the lego man, that may be a valid reason for having a game object made up of more than one spatial. Remember the tree structure of Nodes - each node has a parent node, which has a parent node, so on and so forth up until the root node.



So there are multiple ways to go about what you want to do. First, you could have the legoman and cow parented to a single node that has your custom control on it. So, when you find the spatial that the ray from the mouse intersected, you do:

[java]

Node current = GetSpatialFromMouseIntersection(cam);



while(current.Parent != null)

{

if(current.Parent.getControl(MyCustomControl.class) != null)

//do something

else

current = current.Parent;

}



[/java]



So you work your way up the tree while looking for the Node that has your custom control attached to it. Make sense?



Oh and another thing. Apparently you should not share controls between spatials. So dont do that. One spatial, one or more controls.



Another possibility is to make an invisible bounding box structure. Your mouse ray will then intersect with this box, even if the player cannot see it. This bounding box can then have your custom control attached to it.

You can attach custom data objects to spatial s, not just controls. In fact controls are probably overkill unless you want then updated every frame for some reason.



I have a clickable interface defined. Whenever a click is detected it does as suggested above looking for the first parent with a clickable attached and then that clickable gets called.

I only got half way through the original post… is your main issue really about picking? It is very common to loop up the parent hierarchy to find a “main” node… either by looking for a piece of user data or a control or something.

Awesome. I spent almost a week on this problem and you all solved it within an hour of me posting. Thanks guys!



I think I will use the idea of transversing my scenegraph to look for the main node of the object, and then have my user data objects hung to that. That solves the issue of the control methods not applying to what I’m doing, as well as the issue of clicking the spatial.