Face Spacial same direction as camera

Hi All,

I have been trying to use a reworked version of flagrush lession 9 as a sample to start playing with jME. I have replaced the bike with a model of a tank which has a child node turret (Set in blender as the whole tank is one model). I have loaded it in as a jme xml file no problem

I can even rotate the turret in the direction of the camera BUT… if I turn the tank the turret moves also and no longer points in the same direction as the camera (using chase camera if that makes a difference)

In the update method of Lession9 I have added the line

player.updateTurret(chaser.getCamera().getLocation(), chaser.getCamera().getDirection());

and in vehicle I have added

public void updateTurret(Vector3f camLoc,Vector3f camDirection){
Vector3f v = new Vector3f(
model.getWorldTranslation().z );
v.addLocal( camDirection.x, 0f ,camDirection.z );
Turret.lookAt( v , Vector3f.UNIT_Y );

I was under the impression that it should just pull the details off the camera and still set the turret in the correct direction even though the tank has rotated

Not sure what I am missing


It sounds to me like an issue of world vs local coordinates.  I'm not sure about your scenegraph, but you can try using:


I'm not convinced that will fix this, however.  From what you said, I think you want to move the tank and keep the turret pointing in the same position.  If that is the case, than the easiest thing to do would be to 'save' the position of the turret before you start moving both the camera and the rest of the tank.


// rest of your class above
private Vector3f turretDirection;

public void turnTurret(Vector3f camDirection){
Turrent.lookAt(turretDirection, Vector3f.UNIT_Y);

What you want to do is update the turret to the 'saved' location every time you turn the tank itself.  What's happening in your scene is that the tank is turning, which in turn makes the turret turn as well since the turret is attached to the tank.

In other words, your scene graph looks like this:


Moving the parent moves the child, same for rotations.

Thanks for your quick reply

Seems that if I change




the turret doesnt move at all with my code

I have created the value Vector3f turretDirection which updates when the turret is updated

and have created the method

public void SetTurretPos(){
Turret.lookAt(turretDirection, Vector3f.UNIT_Y);

to reset the turrent to its last position and is called after the tank gets turned
Still seems to have the same issue  :?

Try putting a cube off to the side of the scene and have the turret look at it on every update.  The desired functionality would be that you drive the tank around and the turret automatically 'aims' itself at this cube.

This really depends on how things are attached in your scene.  It sounds like your manipulating the model too high in the hierarchy.  Can you confirm how the various Nodes, TriMesh's, etc are structured?  There's a big difference in whether the turret is attached directly to the rest of the tank or to a Node that directly parents them both

Im pretty new at this so I will try my best to explain

The tank is attached to the rootNode

The Turret is created via the code

Turret = ((Node)model).getChild("Turret");

This nodes was created in blender so they are loaded from the same file but the 2 parts body and turret are loaded as different spacials in the code

I have created a target box, which would be better if I could get it working because I will use it as my target cross

Box TargetBox;
TargetBox = new Box("Target", new Vector3f(100, 9, 110), 2, 2, 2);
TargetBox.setModelBound(new BoundingSphere());

and in the update

player.Turret.lookAt(TargetBox.getWorldTranslation(), Vector3f.UNIT_Y);

unfortunatley it doesn't really point in the right direction either, which I guess means Im doing it wrong or something isnt set right.

If I can get it to point to an object that would be better then I can attach the chase camera to the turret which may then look in the right direction, I will just then need to move the box with the mouse