Atrus
March 17, 2009, 2:56am
1
How would you go about moving all the children of a node relative to the translation on the parent?
For example if I have:
root.setLocalTranslation(new Vector3f(1, 0, 0));
It doesn't seem to effect the children, unless I am not calling an update correctly...
if you're player is attached to the scene's rootNode you dont need to update anything yourself, you can remove:
root.updateWorldVectors();
Also the players location and the Cameras location point both to the same instance of Vector3f, because of the following code:
Vector3f loc = cam.getLocation();
root.setLocalTranslation(loc);
better use:
root.getLocalTranslation.set(loc);
Atrus
March 17, 2009, 12:55pm
3
the root.updateWorldVectors() was just something left over from me trying to get everything to move together.
Whenever I run what I have, both hand and spheredo not change position, even when I move the camera around.
When you call "setLocalTranslation" on any object, it moves it to those coordinates relative to it's parent node. So, if all of the things you are trying to move are connected to that node, you should be doing it correctly.
In SimpleGame this is already called, but if you're not using it you may need to add it to your update method:
root.updateGeometricState(tpf, false);
Atrus
March 17, 2009, 3:43am
5
See, I thought it would but it doesn't seem to be doing that, which probably means I'm missing some detail. Anyways here is the entire class…
package player;
import java.io.IOException;
import java.net.URL;
import jmetest.flagrushtut.lesson8.Lesson8;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingVolume;
import com.jme.input.FirstPersonHandler;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Sphere;
import com.jme.system.DisplaySystem;
import com.jme.util.export.binary.BinaryImporter;
public class Player extends Node
{
private Camera cam;
private Sphere s;
private FirstPersonHandler fp;
private Node root = this;
private Node hand;
public Player(int width, int height)
{
setupCamera(width, height);
setupFirstPerson();
setupHand();
s = new Sphere("Temp Player", cam.getLocation(), 10, 10, 30);
s.setModelBound(new BoundingBox());
s.updateModelBound();
root.attachChild(s);
}
private void setupCamera(int height, int width)
{
cam = DisplaySystem.getDisplaySystem().getRenderer().createCamera(width, height);
cam.setFrustumPerspective(45.0f, (float)width / (float)height, 1, 1000);
DisplaySystem.getDisplaySystem().getRenderer().setCamera(cam);
}
private void setupFirstPerson()
{
fp = new FirstPersonHandler(cam);
fp.getKeyboardLookHandler().setActionSpeed(50f);
fp.getMouseLookHandler().setActionSpeed(1f);
}
private void setupHand()
{
hand = loadHand();
hand.setLocalTranslation(new Vector3f(209, 29, 208));
root.attachChild(hand);
}
private Node loadHand()
{
Node model = null;
try
{
URL modelFile = Player.class.getResource("/resources/models/hand.jme");
BinaryImporter importer = new BinaryImporter();
model = (Node)importer.load(modelFile.openStream());
model.setModelBound(new BoundingBox());
}
catch (IOException e)
{
e.printStackTrace();
}
return model;
}
public void update(float inter)
{
fp.update(inter);
Vector3f loc = cam.getLocation();
System.out.println("Hand: " + hand.getLocalTranslation());
System.out.println("S: " + s.getLocalTranslation());
System.out.println("Root: " + root.getLocalTranslation());
root.setLocalTranslation(loc);
root.updateWorldVectors();
}
public BoundingVolume getPlayerBounds()
{
return s.getModelBound();
}
}
And the player is attached to the scene's root node right?
What kind of Game implementation do you use? Base -Simple- StandardGame?
Can you also show your games update() method?