[SOLVED] Quaternions Math problem

I one more stumble with quaternions a little, and hope someone here knows the missing part :slight_smile:

(note that i say substract in logical not mathematical terms, as I know that a real substractions make no sense here)

Basically I have two quaternions (overly simplified example)
The rotation of a PlayerObject
The Cameras Rotation.

Now I want to get the difference on the PlayerObjects local yaw.

So basically
the PlayerObject Rotation from the CameraRotation -> giving me a PlayerLocal rotation to the viewrotation
converting that to angles
-> do my stuff with the angles[1] part

Now I cant figure out that part that "subtract"s the rotations to be correct.
For a simple test case I have to identical valid rotations, and just “substract” them , my expectation is that I should get the identity Quaternion out of this.
-> Eg there is no local rotation if both are equal.

However I fail to get the calculations right or even nearly right.
Any help would be really nice.

My current TestCase is:
package de.visiongamestudios.server.system;

import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;

public class QTest {
public static void main(final String[] args) {
final float[] initValue = new float[3];
initValue[1] = 90 * FastMath.DEG_TO_RAD;

	final Quaternion player = new Quaternion();

	final Quaternion cam = new Quaternion();

	final Quaternion res = cam.mult(player);
	System.out.println("Player to Cam Rotation " + res);// expectiong 0,0,0,1 here as both 
	//quaternions are the same, so no further rotation should be required
	//However ouput0 is "Player to Cam Rotation (0.0, -0.99999994, 0.0, 0.0)"


I suspect you want to divide the quaternions (multiply by an inverse), not multiply by the negative.

1 Like

Negative quaterion is the same rotation as the quaternion… so negate is not what you want.

If you are trying to do the equivalent of “world to local” for rotations:

Quaternion someWorldRotation…
Quaternion local = spatial.getWorldRotation().inverse().mult(someWorldRotation);

…depending on what you are actually trying to do in a given case, sometimes you need spatial.getParent().getWorldRotation()

1 Like

Thanks, that really helped, it works now :slight_smile:

Seems like I misunderstoood the multiplicative inverse part :slight_smile:

Since you helped me getting it to work, here the real use case:

After playing around with the buggy in my game, I was somewhat unsatisfied with a pure digital keyboard based steering, as it is far to easy to over-steer that way, loose grip and start flying somewhere down. So I searched for viable optional alternative control methods, that are doable with a mouse and feel intuitive (meaning after max 1 minute you are able to use them at least as well as the classical input (wasd)).

Basically my server knows the view direction of the client(since this is necessary for ray-traces for bullets, object useages ect anyway) , and of course the orientation of the Vehicle entity,
-> based on this a turning degree is calculated, and then applied to the vehicle.

The result is, if you want to stay on the road, you basically only need to make sure you look at the road now, and due to more differentiated steering (max steering is if you look 90° away from the forward direction) it steers way more smooths, and it rarely happens that you loose grip accidentially.

Looks cool.