Cloning object using SharedMesh gets borked by findPick()

I'm making a tower defense game and have two modes so far: one for placing towers and one for selecting towers that are already placed. Switching between the two modes works fine as long as I don't actually select a tower while in select mode. If I do select a tower (which uses findPick()), then when I switch back to place tower mode the new tower's coordinates are screwed up.  Here's a dump of the objects in the scene:



[pre]

rootNode: trans [0.0,0.0,0.0]

  terrain: trans [0.0,0.0,0.0]

  Tower Node: trans [0.0,0.0,0.0]

    Debug Obj 2 mesh: trans [73.8,2.6,74.5]

      Cone01: trans [0.0,0.0,0.0]

        Cone01##0: trans [0.0,0.0,0.0]

    Debug Obj 3 mesh: trans [48.7,0.4,61.2]

      Cone01: trans [0.0,0.0,0.0]

        Cone01##0: trans [0.0,0.0,0.0]

    Debug Obj 5 mesh: trans [55.1,1.9,73.1]

      Cone01: trans [0.0,0.0,0.0]

        Cone01##0: trans [73.8,2.6,74.5]

  cursor: trans [153.0,619.0,0.0][/pre]



The numbers given are the x,y,z coordinates. Objects 2 and 3 were before using select mode. I then clicked on one of them (findPick() called on it) and the next tower I create has one of it's inner components assigned a translation that makes it not align with terrain. It's definitely caused by calling findPick() on "Tower Node", if I comment out that line tower placement works fine. The towers are SharedMesh objects… is it possible that findPick() is altering the local translation data on the original mesh? (I can see how that would cause all subsequent objects to be created with weird coordinates). This bug is driving me crazy… why would findpick change the translation and rotation of objects being picked? I would expect it to be read-only… :stuck_out_tongue:



Thanks in advance for any help!

Check your getLocalTranslation() method calls.

node.getLocalTranslation() returns a reference not a copy of the Vector3f.


Node a;
Node b.setLocalTranslation(a.getLocalTranslation());



Both nodes now point to the same Vector3f which will often lead to problems.
Its more safe to use b.getLocalTranslation().set(a.getLocalTranslation()); or b.setLocalTranslation(a.getLocalTranslation().clone());

This is probably a bug and I got it too when making my game ( a tower defense game too actually :wink: )

Read more here:

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

Thanks for the help! Your patch in SharedMesh.java works for me. I agree that this is a bug. If I just call findPick() and do nothing with the results, my towers get messed up. If I comment out that line, they don't. So findPick is making changes to the mesh that's used by SharedMesh, which is then screwing up other things that share that mesh.