Nodes are being emptied (3 posts)

  • Profile picture of ancel ancel4p said 3 months, 2 weeks ago:

    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

    
    /*
     * 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<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

    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("P_Items " + P_Items.getNItems().getChildren());
            for (int i = 0; i < results.size(); i++) {
              System.out.println("* Collision #" + i);
            }
          }else if(binding.equals("PlaceItem") && !value)
          {
            CollisionResults results = new CollisionResults();
            Ray ray = new Ray(cam.getLocation(), cam.getDirection());
    
           Spatial teapot = P_Items.getItem("teapot");
           terrain.collideWith(ray, results);
           if (results.size() > 0)
           {
            System.out.println("Results size for place item:" + results.size());
            System.out.println("results.getCollision(0).getContactPoint():" + results.getCollision(0).getContactPoint());
    
            teapot.setLocalTranslation(results.getCollision(0).getContactPoint().getX(),results.getCollision(0).getContactPoint().getY(),results.getCollision(0).getContactPoint().getZ());
    
            rootNode.attachChild(teapot);
           }
          }
    

    Thanks for any help you can give :)

  • Profile picture of pspeed pspeed815p said 3 months, 2 weeks ago:

    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.

  • Profile picture of ancel ancel4p said 3 months, 2 weeks ago:

    Ah, now it makes sense. Thank you.