Object Location Problem

Hey folks,



i started using jme for an student projekt a few weeks ago, so don't be too hard :).



I've the following problem:



If have a game where some objects ( Worms )  should shoot straight into the camera ( the player), the shoot works, i mean i use jme physics and i create a dynamicPhysicNode with a sphere in it and set a force, so that the sphere fly through the scene.



No the Problem, i want to get the position of an object so that i can shoot from that position and i want to shoot at the camara, so i need the camera position. i think the folowing code should do that but it doesn't work.


// get local translation of the node with the object ( the worm) in it
posShoot=dynPhyNode.getLocalTranslation();
// create an physics node with an sphere in it
      DynamicPhysicsNode node = scenes.tools.ObjectFactory.get().createObject();
      node.setLocalScale(5);
      node.setName("physics node");
      node.getLocalTranslation().set(posShoot.x,posShoot.y+30,posShoot.z);
      direct.set(posShoot);
      direct.add(gameModel.rootNode.getLocalTranslation());
      direct = direct.subtract(gameModel.cam.getDirection());
      node.addForce(f.mult(direct));
      gameModel.rootNode.attachChild(node);
      gameModel.rootNode.updateRenderState();
      gameModel.rootNode.updateGeometricState(0, false);



So maybe someone can tell me what i do wrong?

Sometimes this code let my app crash and eclipse say it's and java runtime error or something like that, but don't tell me any usefull information.

Hope somebody can help!

Thanks and greetings
Tobias

Hey, great seems that it works now :), but i don't know why?



i just added this

gameModel.physicsSpace.addToUpdateCallbacks(new PhysicsUpdateCallback()
        {
            public void beforeStep( PhysicsSpace space, float time )
            {
               
            }
           
            public void afterStep( PhysicsSpace space, float time )
            {
               
            }
        } );



Can somebody explain ?

Thanks for that tipp :), but i wanted to know why :)?

Not sure what scenes.tools.ObjectFactory is?  It might be from jme physics 1.0 in which case you really should use jme physics 2.0 as a lot of improvements have been made in the new version.  It can be downloaded from here: https://jmephysics.dev.java.net/.  You should check out the tutorials there.    Actually, it’s best to checkout the latest versions from cvs into eclipse to get the latest fixes but I can’t remember the exact paths as I am away from my computer where my projects are located.  If a Java runtime crash occurs a file like hs_err_pidxxx.log is written out to the eclipse workspace directory. 



In general, when working with many objects, its best to work in world coordinates because each object has its own local coordinate system that may differ from each other unless every object is created relative to the world origin.  So you should be using Spatial.getWorldTranslation when trying to find out how to shoot at the camera.  If you update an object’s local position, you should call updateWorldVectors() and updateWorldBound() to update the world coordinates with the changed position before querying the world info.



To get a vector to the camera’s postion you can do something like this:


Vector3f cameraPos = DisplaySyatem.getDisplaySystem().getRenderer().getCamera().getLocation();
Vector3f playerPos = node.getWorldTranslation();
Vector3f cameraDir = cameraPos.subtract(playerPos);

HEy thanks a lot, the problem was that i don't use the world cordinates :(, very stupid :).



But a bigger Problem is that, the app crashes and i don't really know why. I've downloaded the newes physics engine from the adress above.



Here is what eclipse says:

03.07.2008 19:49:51 com.jmex.physics.impl.ode.OdePhysicsSpace addNode
INFO: PhysicsNode (null) has been added
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (Worm) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (WormNode) attached to this node (OwnRootNode)
03.07.2008 19:49:51 com.jme.scene.Node <init>
INFO: Node created.
03.07.2008 19:49:51 com.jme.scene.Node <init>
INFO: Node created.
03.07.2008 19:49:51 com.jmex.physics.impl.ode.OdePhysicsSpace addNode
INFO: PhysicsNode (null) has been added
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (Worm) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (WormNode) attached to this node (OwnRootNode)
03.07.2008 19:49:51 com.jme.scene.Node <init>
INFO: Node created.
03.07.2008 19:49:51 com.jme.scene.Node <init>
INFO: Node created.
03.07.2008 19:49:51 com.jmex.physics.impl.ode.OdePhysicsSpace addNode
INFO: PhysicsNode (null) has been added
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (Worm) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (WormNode) attached to this node (OwnRootNode)
03.07.2008 19:49:51 com.jme.scene.Node <init>
INFO: Node created.
03.07.2008 19:49:51 com.jme.scene.Node <init>
INFO: Node created.
03.07.2008 19:49:51 com.jmex.physics.impl.ode.OdePhysicsSpace addNode
INFO: PhysicsNode (null) has been added
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (Worm) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (WormNode) attached to this node (OwnRootNode)
03.07.2008 19:49:51 com.jme.scene.Node <init>
INFO: Node created.
03.07.2008 19:49:51 com.jme.scene.Node <init>
INFO: Node created.
03.07.2008 19:49:51 com.jmex.physics.impl.ode.OdePhysicsSpace addNode
INFO: PhysicsNode (null) has been added
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (Worm) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (WormNode)
03.07.2008 19:49:51 com.jme.scene.Node attachChild
INFO: Child (WormNode) attached to this node (OwnRootNode)
03.07.2008 19:49:54 com.jme.scene.Node <init>
INFO: Node created.
03.07.2008 19:49:54 com.jmex.physics.impl.ode.OdePhysicsSpace addNode
INFO: PhysicsNode (null) has been added
2
03.07.2008 19:49:54 com.jme.scene.Node attachChild
INFO: Child (sphere) attached to this node (null)
03.07.2008 19:49:54 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (physics SPHERE)
03.07.2008 19:49:54 com.jme.scene.Node attachChild
INFO: Child (physics node) attached to this node (OwnRootNode)
2
03.07.2008 19:49:56 com.jme.scene.Node <init>
INFO: Node created.
03.07.2008 19:49:56 com.jmex.physics.impl.ode.OdePhysicsSpace addNode
INFO: PhysicsNode (null) has been added
03.07.2008 19:49:56 com.jme.scene.Node attachChild
INFO: Child (sphere) attached to this node (null)
03.07.2008 19:49:56 com.jme.scene.Node attachChild
INFO: Child (null) attached to this node (physics SPHERE)
03.07.2008 19:49:56 com.jme.scene.Node attachChild
INFO: Child (physics node) attached to this node (OwnRootNode)
#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x76f28516, pid=4516, tid=6120
#
# Java VM: Java HotSpot(TM) Client VM (10.0-b19 mixed mode windows-x86)
# Problematic frame:
# C  [ntdll.dll+0x68516]
#
# An error report file with more information is saved as:
# C:UserstobiasProjekteJavaSimpleTerrainhs_err_pid4516.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

[error occurred during error reporting , id 0xc0000005]



The error was thrown after a few spheres werde shootet in the world, maybe i have to remove the old ones after they are out of view, but i don't know i can do that.

You have added a Null node to the PhysicsSpace…

Oh, ok that could be the reason, but i can't find the problem, here is the code which creates the DynamicPhysicNode


public DynamicPhysicsNode createObject( ObjectType objType) {
                // space a public var in the class and is set by the contructer with the PhysicSpace
      DynamicPhysicsNode node = space.createDynamicNode();
      TriMesh visual = null;
      boolean isBasicShape = false;
      
      switch (objType) {
      case BOX:
         visual = new Box("box", new Vector3f(), 0.5f, 0.5f, 0.5f);
         visual.setModelBound(new BoundingBox());
         isBasicShape = true;
         break;
      case ROUNDEDBOX:
           visual = new RoundedBox("box", new Vector3f(0.5f, 0.5f, 0.5f), new Vector3f(0.07f, 0.07f, 0.07f), new Vector3f(0.03f, 0.03f, 0.03f));
           visual.setModelBound(new BoundingBox());
//          visual = new RoundedBox("box", new Vector3f(0.5f, 0.5f, 0.5f));
           break;
      case SPHERE:
         visual = new Sphere("sphere", 30, 30, 1f );
         visual.setModelBound(new BoundingSphere());
         isBasicShape = true;
         break;
      case CAPSULE:
         visual = new Capsule("capsule", 15, 10, 10, 0.5f, 2f);
         visual.setModelBound(new BoundingCapsule());
         isBasicShape = true;
         break;
      case CYLINDER:
          visual = new Cylinder("cylinder", 15, 10, 0.5f, 2f, true);
          visual.setModelBound(new BoundingSphere());
          break;
      case TORUS:
          visual = new Torus("torus", 15, 10, 0.3f, 1);
          visual.setModelBound(new BoundingSphere());
          break;
      case TEAPOT:
          visual = new Teapot("tea pot");
          visual.setModelBound(new BoundingSphere());
          break;
      default:
          Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).severe("OOps:" +objectType +" unknown");
          return null;
      }

      visual.updateModelBound();
      
      node.attachChild(visual);
      node.setLocalScale(scale);
      node.setName("physics " +objectType.toString());
      node.setMaterial(material.getMaterial());
      applyRenderStates(visual, material);
      
      if (isBasicShape) {
          // create geometry automatically
          node.generatePhysicsGeometry(true);
      } else {
          // create a physical copy of the existing mesh
          PhysicsMesh mesh = node.createMesh("physics mesh");
          mesh.copyFrom(visual);
          node.attachChild(mesh);
      }
      
      node.computeMass();
      node.updateRenderState();
      return node;
      
   }



And in this Method the Method above will be called


public void shoot( ){
      //dynamic node is the object who shoot the dynamicNode
      DynamicPhysicsNode node = null;
      
      
      posShoot=dynPhyNode.getChild(3).getWorldTranslation();
      posShoot.add(0,0,20);
      
                /**
                *** Here i call the Method above
                */
      node = scenes.tools.ObjectFactory.get().createObject(ObjectType.SPHERE);
      
      node.setLocalScale(3);
      node.setName("physics node");
      node.getLocalTranslation().set(posShoot.x,posShoot.y,posShoot.z);

      
      direct = gameModel.cam.getLocation().subtract(posShoot);
      node.clearForce();
      node.addForce(direct.mult(20));
      
      gameModel.rootNode.attachChild(node);
      gameModel.rootNode.updateRenderState();
      gameModel.rootNode.updateGeometricState(0, false);
      
   }



Hope somebody can see what i'm not :(, it's so disapointing.

Thanks a lot

I don't see the null node: Try posting the info in the  hs_err_pidxxx.log written out to the eclipse workspace directory when a JVM crash occurs.  Maybe we can trace where the call generating the crash.

Oh wait, my fault for (confused by the null name)



you may be having a ODE physics issue.  If there are too many collisions at once (or a collision that cannot be resolved for some reason) its possible to get that error.  Try putting in a physics callback to ignore all collisions and see if the problem persists…


Sorry but i have to say that the code above doesn't realy solve my problem, i think i don't implement what you realy mean :(.



Hope you could help me. But i can say that the app crashes often after some collisions.

Hmm, well I am working on a production project and don't update the jME stuff very often.



Here's what a ignore all PhysicsCallBack would look like with the code base I am using:



    private static void setCallBack() {

        ContactCallback callBack = new ContactCallback() {

            public boolean adjustContact( PendingContact c ) {

                c.setIgnored( true );
                return true;
            }

        };

        physicsSpace.getContactCallbacks().add( callBack );
    }

hey thanks, but now i have no automatic collision detection and reaction, and i think the problem is not really solved :(.



The app crashes again if i com clother to the objects, maybe there is the problem.



At the moment i load 7 Objects into the scene, they are all the same .obj file, but i load the .obj file for every Object in the game. Maybe here is the problem? Or maybe there is a more efficient way to do this?



Hope someone could help.



Greeting Tobias

I don't think that multiple objects is your problem as 7 does not seem like a lot and I think ODE is decent enough implementation to handle many simultaneous objects.  You can avoid the data duplication by using the com.jme.scene.SharedMesh (for TriMesh root objects) and SharedNode (for Node root object) classes depending on whether the loaded model casts to a TriMesh or a Node.  You can maintain a map of file names to the original loaded Spatial and return a new SharedMesh or SharedNode that wraps around the loaded object.  The documentation of those classes says to not allow a reference to the original mesh to escape into the application code so just be sure to always wrap the loaded model.



Maybe there is something wrong with the particular physics representation like having some concave polygons that maybe cause an error in the physics calculations if you are using triangle collision accuracy? (Not sure if that could actually cause the problem but it's an idea).  Maybe try to switch to using bounding box collision accuracy or maybe try substituting the more complex torus or teapot models with boxes and see if you still have the problem.

Hey thanks, for your idea, because of that i tried to find which objects result that error and know i'm sure that the problem is my terrainBlock.



I changed it to a simple Box and all works fine, i've tried a lot and maybe someone else can help me to find my problem.



Here is my function which creates the TerrainBlock:


MidPointHeightMap heightMap = new MidPointHeightMap(64, 1.2f);
       Vector3f terrainScale = new Vector3f(4, 0.0575f, 4);
       gameModel.tb = new TerrainBlock("Terrain", heightMap.getSize(), terrainScale,
                                          heightMap.getHeightMap(),
                                          new Vector3f(0, 0, 0), false);
       gameModel.tb.setName("Terrain");
      
       gameModel.tb.setTrisPerPixel( 0.5f);
       gameModel.tb.setDistanceTolerance( 1.0f);
       gameModel.tb.setDetailTexture(1, 16);
       gameModel.tb.setModelBound(new BoundingBox());

       gameModel.tb.updateModelBound();
       gameModel.tb.setLocalTranslation(new Vector3f(0,0,0));
            ProceduralTextureGenerator pt = new ProceduralTextureGenerator(
           heightMap);
       pt.addTexture(new ImageIcon(TestTerrain.class.getClassLoader()
                                   .getResource("jmetest/data/texture/grassb.png")),
                     -128, 0, 128);
       pt.addTexture(new ImageIcon(TestTerrain.class.getClassLoader()
                                   .getResource("jmetest/data/texture/sand_0007_C.jpg")),
                     0, 128, 255);
      
       pt.addTexture(new ImageIcon(TestTerrain.class.getClassLoader()
                                   .getResource("jmetest/data/texture/highest.jpg")),
                     128, 255,
                     384);
   
       pt.createTexture(32);
      
       TextureState ts = gameModel.display.getRenderer().createTextureState();
       ts.setEnabled(true);
      
       Texture t1 = TextureManager.loadTexture(
           pt.getImageIcon().getImage(),
           Texture.MM_LINEAR_LINEAR,
           Texture.FM_LINEAR, true);
       t1.setStoreTexture(true);
      
       ts.setTexture(t1, 0);
   
       Texture t2 = TextureManager.loadTexture(
           TestTerrain.class.getClassLoader().getResource(
           "jmetest/data/texture/Detail.jpg"),
           Texture.MM_LINEAR_LINEAR,
           Texture.FM_LINEAR);
   
       ts.setTexture(t2, 1);
      
       t2.setWrap(Texture.WM_WRAP_S_WRAP_T);
   
       t1.setApply(Texture.AM_COMBINE);
       t1.setCombineFuncRGB(Texture.ACF_MODULATE);
       t1.setCombineSrc0RGB(Texture.ACS_TEXTURE);
       t1.setCombineOp0RGB(Texture.ACO_SRC_COLOR);
       t1.setCombineSrc1RGB(Texture.ACS_PRIMARY_COLOR);
       t1.setCombineOp1RGB(Texture.ACO_SRC_COLOR);
       t1.setCombineScaleRGB(1.0f);
      
       t2.setApply(Texture.AM_COMBINE);
       t2.setCombineFuncRGB(Texture.ACF_ADD_SIGNED);
       t2.setCombineSrc0RGB(Texture.ACS_TEXTURE);
       t2.setCombineOp0RGB(Texture.ACO_SRC_COLOR);
       t2.setCombineSrc1RGB(Texture.ACS_PREVIOUS);
       t2.setCombineOp1RGB(Texture.ACO_SRC_COLOR);
       t2.setCombineScaleRGB(1.0f);
      
       StaticPhysicsNode stPhyNode = gameModel.physicsSpace.createStaticNode();
       stPhyNode.setName("TerrainNode");
       stPhyNode.setRenderState(ts);
       stPhyNode.attachChild(gameModel.tb);
       stPhyNode.generatePhysicsGeometry();
      
       gameModel.rootNode.attachChild(stPhyNode);



Hope someone can help :(.

i think there is a problem with certain collisions, trimesh <-> trimesh ? i am not sure…

there are other threads about similar problems.

http://www.jmonkeyengine.com/jmeforum/index.php?topic=4020.30

Hey Thank you, but that didn't really solve my problem, i took the adviset mentioned in the thread but the problem still exist.



In worst case i have to implement some physics by myself, but i would like to use jmePhysics.



Cheers Tobias