Quaternion.negate() vs Quaternion.opposite()


#1

I’m trying to get the opposite rotation of a node. In my head using Euler angles, if I rotated a node 90 degrees on the X axis, the opposite would be -90 degrees - or negated. I was using negate(), but then came across the opposite method. What’s the fundamental difference? Is my assumption wrong that a negated Quaternion is not what I think it is? If it’s not the opposite - what is it?


#2

I’m not sure but now that I looked, there’s also an inverse() method that I would also think does the same as negate and opposite, so now I’m interested as well.

The code behind all 3 methods looks different

public Quaternion opposite(Quaternion store) {
    if (store == null) {
        store = new Quaternion();
    }

    Vector3f axis = new Vector3f();
    float angle = toAngleAxis(axis);

    store.fromAngleAxis(FastMath.PI + angle, axis);
    return store;
}
 public Quaternion inverse() {
    float norm = norm();
    if (norm > 0.0) {
        float invNorm = 1.0f / norm;
        return new Quaternion(-x * invNorm, -y * invNorm, -z * invNorm, w
                * invNorm);
    }
    // return an invalid result to flag the error
    return null;
}

public void negate() {
    x *= -1;
    y *= -1;
    z *= -1;
    w *= -1;
}

#3

Well they all kinda seem to make sense in that they return the opposite but they all do it in different ways.

.negate() multiplies by -1
.opposite() adds 180 degrees
.inverse() uses a direction and negates it.

They all do the same thing but in a different way. I’d be interested for my own education if anything as to what each use-case is warranted.


#4

Hmm that negate one seems odd. I’ve always seen the quaternion conjugation/negation listed as only x y and z components inverted while w remained the same. Could be that there’s no difference I guess.

I had to look that up once when working on an older version of the glMatrix library where some moron defined it as everything times 1 which did nothing at all and I wasted hours of debugging because of it lol.


#5

inverse() is what you want.

opposite() is weird. While I can’t imagine why it won’t work in all cases I feel like it won’t work in all cases. :slight_smile:

I think negate() doesn’t actually change the rotation since a q = -q in quaternion-land. I always imagined it was to flip them in case there was some reason some piece of code wanted to normalize the sign.