Collision with Spatials

@kevin.shen18 said: @pspeed

The error is on this line: TowerControl tower = rootNode.getChild(“tower”+selected).getControl(TowerControl.class);

The variable “selected” holds the tower index. I gave each tower an index in a generateTower method in Gameplayappstate class.

…but why do you bother searching for the tower by index when you already had the actual tower where you set the index?

getIndex() is a method I made in the TowerControl class.

In the generateTower() method when each tower is generated it has an index.

The ray casting gets the tower, identifies the index and then load the charge into that specific tower.

I’m not sure what you mean by when I have the actual tower where I set the index?

A tower is clicked on. You have the actual tower. You get it’s index. Then you try to use its index to look up the exact same tower later.

If your get call worked then those would be the exact same objects, yes?

Except in the second one with TowerControl I’m trying to get the control of the selected tower. In the first line with selected after getControl(TowerControl.class) there is a get index.

Also I put your loop in here:
if(selected != -1 && state.getBudget() > 0 && !keyDown){
for(Spatial s = results.getClosestCollision().getGeometry(); s != null ; s = s.getParent()){
//TowerControl tower = rootNode.getChild(“tower”+selected).getControl(TowerControl.class);
TowerControl tower = s.getControl(TowerControl.class);
System.out.println(s); //print the geometry
if(s != null){
if(tower.getChargeNum() < 5){
//tower not full. load a charge
System.out.println(“Check 4”);

                }//if loop
             }//for loop

Apparently the geometry is called mesh1.

However it gives me an Nullpointerexception on the line if(tower.getChargeNum() < 5){

I am unsure what to do at this point. Is there anything you would suggest me to do?

@pspeed I’m looking at your picture above. Are you saying the problem lies in getChild(“tower”+selected)?

if(s != null){

I think you meant tower != null.

Change selected from int to Spatial. Set selected to s when you find one without a null TowerControl.

…then don’t even bother looking it up. It’s like you found the book you were looking for on the shelf but then somehow want to go back to the card catalog to look up the book. But you already had the book right in your hand… so just hold onto it.

It works now! Thanks. the field variable selected isn’t really necessary then if I’m using the loop. I managed to do it without changing it from int to spatial.

If I wanted the index I can just add this: s.getControl(TowerControl.class).getIndex();

EDIT: s.getControl(TowerControl.class).getIndex() doesn’t work. However the game still runs so nevermind. I forgot that s is a spatial not a geometry.

My last question is: Given a spatial say for example:

Spatial sp = assetManager.loadModel(“somerandommodel”);

Is there a way to get the Geometry of that spatial. Say I figured out the geometry is called “geom” what do I have to do to access geom. I know for sure casting doesn’t work because you can’t cast a node to a geometry.

Geometry geom = (Geometry) sp; //doesnt work

Is it with getChild()? If so, how?


I read both links.

Are you sure sp.getChild(“geom”); would work?

In the code I had problem with the hierarchy was: rootnode -> tower node -> tower -> mesh1

I tried rootnode.getChild(“tower node”).getChild(“tower”) and it gave me a red underline so I couldn’t get to mesh1

Thats because getChild returns a spatial and you have to cast it to node again…

1 Like


YES! That’s was what I was looking for. I forgot to cast. Thanks!


Thank you to you too!