Set rotation problem with quaternions

Hello, i'm trying to extend physics BallThrower to a more general form firing missiles of in the direction of a given spatial of which i hold a reference ( CameraNode or gun barrel …)



my shoot - method is as follows:


   /**
    * Shoots a missile in the direction of a Spatial (Gun).
    */
   private void shoot() {
      // Move to the next ball index.
      missileIndex++;
      if (missileIndex >= pMissiles.length) missileIndex = 0;
      
      // Enable and show it in case it wasn't before.
      pMissiles[missileIndex].setEnabled(true);
      pMissiles[missileIndex].getSpatial().setForceCull(false);
      
      // Set the missiles position and direction to the barrels
        pMissiles[missileIndex].getSpatial().getLocalRotation().set(barrel.getLocalRotation());
      pMissiles[missileIndex].getSpatial().getLocalTranslation().set(barrel.getLocalTranslation());
        pMissiles[missileIndex].getSpatial().updateGeometricState(-1f, true);
                      
      pMissiles[missileIndex].syncWithGraphical();
      pMissiles[missileIndex].resetForces();
      
      // Shoot it in the direction of the camera with the selected force.
        pMissiles[missileIndex].addForce(barrel.getLocalRotation().getRotationColumn(2).mult(shootForce, forceVec));
      
      // Reset the timer.
      elapsedTime = 0;
   }



my problem with this is that the missile does not point in the direction i face but in the direction i started of (default CameraNode orientation)
so if i fire the first msl it works as expected, but when turning my camNode and firing the next it points in the direction the first did.

if i change the line setting the rotation to:


pMissiles[missileIndex].getSpatial().setLocalRotation(barrel.getLocalRotation());


then the shootoff works but as the missiles Quaternion is a reference to the CamNodes ... the missile turns as i turn the camNode.

I think the first method should work but am stuck finding out why it doesn't, what do i miss here ???
winkman said:



pMissiles[missileIndex].getSpatial().setLocalRotation(barrel.getLocalRotation());


then the shootoff works but as the missiles Quaternion is a reference to the CamNodes ... the missile turns as i turn the camNode.

I think the first method should work but am stuck finding out why it doesn't, what do i miss here ???


This piece of code means the missle and the barrel share the same Quatrnion. So if you change that Qautrnion both the missle and barrel will be effect.

Try something like this:


pMissiles[missileIndex].getSpatial().getLocalRotation().set(barrel.getLocalRotation());


Thanks for the fast answer llama, but i tried that first (see first piece of code) and it didn't work as i expected, but gave me a missile facing the same direction independent of where my camNode faced.

I tried the second one just to verify that the local rotation from barrel is actually a reference to the real one and not a snapshot copy.


Well, does it actually have a local rotation? (the barrel) or is attachted to a Node or something?

Well in the test case "barrel" is my CameraNode which drives my camera and the LocalRotation Quaternion changes as the direction of the missile changes according to the cam when i use code piece 2 to set the direction, but i think i have to do some more test to verify the rotation really matches the one of the camNode.

Ok solved it :slight_smile: , it was the call to DynamicPhysicsObject.resetForces()  after setting the rotation.

resetForces() resets all Physics Forces AND replaces the local rotation with an empty one  :-o

I simply had to put the resetForces() before setting the direction of the missile.

This was not noticed in the original BallThrower because the rotation is not noticeable there.

winkman said:

resetForces() resets all Physics Forces AND replaces the local rotation with an empty one  :-o

Which is quite odd, isn't it? Should we change that, Per?

Mhm :slight_smile:



Good someone spotted it.

ok, resetForces does not set rotation any more