I'm working on a 3D flight game and I'd like to implement a simple "navigation arrow" pointing at the next waypoint in world which the player has to fly to.
I have position of both airplane (=camera) and the target and also orientation of the airplane (in form of quaternion). But sadly I can't figure out correct way to use them to get the arrow pointing at the target regardless the airplane yaw/pitch/roll.
The arrow was already working once, but only when the airplane was in horizontal orientation. But when the airplane was tilted, the tilt was not reflected in the arrow transform.
Here's my code (it might not be accurate, as I'm desperately trying anything that could help) to get the arrow rotation
Vector3 target = ...; target.subtract(airplane.position); target.normalize(); Vector3 dir = new Vector3(0, 0, -1); // initial airplane direction dir.rotateBy(airplaneQuat); // current airplane direction double angle = Math.acos(target.dot(dir)); Vector3 axis = target.cross(dir);
I think my problem is the fact that the airplane can fly upside down and in other riddiculous orientations, and I just cannot visualize all the transforms in my head. I'll be happy for any help.
UPDATE: Based on Cong's answer I came up with working solution. Here's the pseudocode. It could be improved, but the arrow behaves sufficiently accurate by now, just the way I indended.
Vector3 dir = target.position; dir.subtract(airplane.position); dir.normalize(); Quaternion q = airplane.orientation; q = q.conjugate(); dir.rotateBy(q); Vector3 up = Vector3(0, 1, 0); Matrix4 mat = Matrix4.createLookAt(dir, up); Matrix4 transform = Matrix4(); // identity transform.translate(0, -0.85, 0); // move to the bottom of the screen transform.mul(mat); transform.scale(0.2); // just scaling the model