Nodes are being emptied

I’ve two classes(Items and HelloTerrainCollision). The Items class loads models and provides methods to access them.

In my items class I save my objects in a Node called “P_items”.



If I press “e” (Action) it will print out : P_Items [teapot (Geometry)]

Showing that the Node has teapot attached.

As soon as I place the teapot in the scene (‘p’) and press ‘e’ again i get: P_Items []

I can’t see how I’ve remove the spatial from my node by just placing it in the scene?

Here’s everything: http://www.2shared.com/file/osxjO25P/Game.html (2.5MB)



I’m using Windows 7 version 6.1 running on amd64; Cp1252; en_US

Code:
/* * This class tracks the items(pickable) in the world. * -keeps information about objects/items * --Location,name and other stuff */ package mygame;

import com.bulletphysics.collision.shapes.CollisionShape;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.material.Material;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import java.util.ArrayList;

public class Items{
static int size = 0;
static ArrayList <Spatial> objects = new <Spatial>ArrayList();
static Node P_items = new Node("pickables");
public Items(AssetManager assetManager,AppStateManager stateManager)
{
//teapot
Spatial teapot = assetManager.loadModel("Models/tea/tea.j3o");
teapot.setName("teapot");
Material teapot_mat= new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
teapot.setMaterial(teapot_mat);
objects.add(teapot);
P_items.attachChild(teapot);
System.out.println("P_items.getChildren() : " + P_items.getChildren());
}

public Spatial getItem(String name)
{
    for(int i = 0;i&lt;objects.size();i++)
    {
        if (objects.get(i).getName().equals(name)){
        return objects.get(i);
        }
    }
    //If Notting has been found
    return null;
}
public int getSize()
{
    return size;
}
public Node getNItems()
{
    return P_items;
}

}


Code in HelloTerrainCollision that deals with Items
Code:
else if(binding.equals("Action") && !value) { CollisionResults results = new CollisionResults(); Ray ray = new Ray(cam.getLocation(), cam.getDirection()); P_Items.getNItems().collideWith(ray, results);
    System.out.println(&quot;P_Items &quot; + P_Items.getNItems().getChildren());
    for (int i = 0; i &lt; results.size(); i++) {
      System.out.println(&quot;* Collision #&quot; + i);
    }
  }else if(binding.equals(&quot;PlaceItem&quot;) &amp;&amp; !value)
  {
    CollisionResults results = new CollisionResults();
    Ray ray = new Ray(cam.getLocation(), cam.getDirection());

   Spatial teapot = P_Items.getItem(&quot;teapot&quot;);
   terrain.collideWith(ray, results);
   if (results.size() &gt; 0)
   {
    System.out.println(&quot;Results size for place item:&quot; + results.size());
    System.out.println(&quot;results.getCollision(0).getContactPoint():&quot; + results.getCollision(0).getContactPoint());
    
    teapot.setLocalTranslation(results.getCollision(0).getContactPoint().getX(),results.getCollision(0).getContactPoint().getY(),results.getCollision(0).getContactPoint().getZ());
    
    rootNode.attachChild(teapot);
   }
  }</div>
Thanks for any help you can give :)

Spatials can only have one parent at a time. If you attach a spatial to Node 1 and then attach it to Node 2, it will no longer be a child of Node 1.



There is something you aren’t understanding about how scene graphs work or you are trying to do something strange, I guess.

1 Like

Ah, now it makes sense. Thank you.