I need to rotate my model,
Every time I press the w key the model turns to the direction of the camera.
private Vector3f viewDirUP = new Vector3f(0,0,1);
targetQua.set((simpleApp.getRootNode().getChild("model")).getWorldRotation()).nlerp(cam.getRotation(), 0.1f);
viewDirection.set(targetQua.mult(viewDirUP));
viewDirection.setY(0);
I don’t know much about the concept of quaternions,So I don’t know if I’m doing the right thing.
Because minie CharacterControl was used,So the end result is going to be a vector.
The above code completes the interpolation rotation conversion to vectors,
I don’t know if I have any mathematical problems with this piece of code,The results look good.
I hit the “s” key, The character model should face the camera,
I got an error when I multiplied targetQua by viewDirUP.negate() and you can see the result in the video
It’s obvious that the model should be facing the camera when the back button S is pressed instead of spinning around(Here I guess I must have used the wrong parameters to cause this,But I’m not sure what).
But I found a function in javadoc Quaternion.opposite(), And then I changed the code for the S key
private Vector3f viewDirUP = new Vector3f(0,0,1);
targetQua.set((simpleApp.getRootNode().getChild("model")).getWorldRotation().opposite()).nlerp(cam.getRotation(), 0.1f);
viewDirection.set(targetQua.mult(viewDirUP.negate()));
viewDirection.setY(0);
So now the S bond works pretty well,But I don’t know why…
Quaternion rotation = spatial.getLocalRotation();
Vector3f direction = camera.getLocation().subtractLocal(spatial.getWorldTranslation());
direction.setY(0f).normalizeLocal();
Quaternion target = new Quaternion().lookAt(direction, Vector3f.UNIT_Y);
rotation.nlerp(target, .1f);
spatial.setLocalRotation(rotation);
Bonus: the model will face the camera regardless of the camera’s direction.
The reason your code does not work as expected is because fromAngleAxissets the quaternion. So in essence, you are completely overwriting the camera rotation and replacing it with 90 degrees around the y axis. So I would expect targetQua to always turn to the same direction.