Extending Geometry

This is more of a curiosity than an actual problem, but I made a class that extends Geometry and I noticed 2 things.

  1. Normally the Geometry constructor assigns a ‘name’ to the geometry. What is the purpose of the name? And if it is important to assign one, than how do I assign one in the constructor of my class that extends Geometry?

  2. In my extended class I created a variable called mesh. The editor complained that mesh was hiding a field. Does that mean that the class Geometry already uses a variable called mesh? Will using my own variable mesh cause unexpected behavior?

    Thanks for reading ^.^
  1. You should not extend Geometry, rather create a Control class that you attach to it. Otherwise, you can call super(name); in the constructor to call the super constructor.
  2. No, you just hide it, its two different variables and a possible source for errors on the user side because its kinda hard discerning the two.

Thank you for clearing that up. Still, one last thing. What’s the purpose of the ‘name’ on a shape or geometry object? Or a mesh for that matter? Most of these constructors ask for a name. What are the names used for?

Its… their… name… :slight_smile: You can find sub-items in node for example by that name, debug your scene, organize it…


[snippet id=“13”]

Ah alright. I’m still rather new to jMonkey and haven’t yet used getChild. Suddenly it’s really obvious why a name would be useful. So far I’ve just been sticking to reference variables, often stored in arrays. Thank you : )

Oh, why is it ‘bad’ to extend Geometry? Is it just a general programming precaution? Because the code I created from doing it is rather elegant and clean. But if it’s going to get me in trouble down the road than I’d rather avoid it.

That would depend on why you extended it. Though the fact that you had your own mesh field when geometry already manages a mesh is a little suspicious.

In general, one should prefer composition over inheritance when possible.

You can extend Geometry however the logic of the scenegraph and jme would suggest only putting things that are solely visual into geometry and put logic code inside a control.

I can understand that, makes sense. And my mesh field was used to point to a mesh that will be attached to the extended Geometry node but hasn’t been attached yet. The reason for that being is so that the mesh may be modified before rendering by method calls from external sources.

But as I am going to be adding larger control structures into the code I’ll switch over to composition. Thanks for the tips ^.^