# Simple quaternion question

Why are the x,y,z,w members of the quaternion class protected?

-Ike

Because if you think you need to set them directly then you are confused about what a quaternion is.

I understand what they areā¦ Iāve run into a case where I need to do this. And it got me curious as to why you didnāt make them public. It doesnāt hurt.

Well, you can do it with the

set(float x, float y, float z, float w)

right?

99.9999999999999999999999999999999999999999999% of the time someone wants to do this, they think quaternions are an angle-axis. The other 0.00000000000000000000000001% of the time, the experts know they can just call set() since setting one value of a quaternion at a time makes no sense at all. 0.

And here I was thinking the opposite, donāt make internal class members public, please, it ONLY HURTS.

Hah you should see the official oracle JavaFX library though. EVERYTHING PRIVATE.

I need it for my animation format. All my frame data is packed and I pull out the data with masks.

For rotations:

``````     jointPose.rotation.x = channelOffset[3];
if ((mask & 0x08) != 0) {
jointPose.rotation.x += frameData[frameD++] * channelScale[3];
}
``````

At the moment Iām just putting them all into floats then creating the quaternion.

And no Iām pretty sure it will not hurt. I think of them as essentially being structs. They just hold data and do some maths.

Random thought:
Wouldnāt it be better to have vecs,quats and matrices as float arrays and their respective classes to be static

You can think of them being unicorns if you like, doesnāt make it a good future proof design though

1 Like

There is no future for such a class. It is simply a utility. A storage bin.

I also think, that using stupid get/set to encapsulate a private field is a āwrongā habit of java. A Legacy of OO and āhow do I manage evolution of a field that move from āanniversary dateā to ābirth dateāā. In the fact, 99.99999% of the time, when you change the private, you also change the public. (get/set are just boilerplate)

Now, Quaternion are as is in jME and 99.9999% of java dev like to wrap/encapsulate field with get/set. So itās easier to have 0.0001% that ask āwhy not a struct/array/data type + functions ?ā vs 99.9999% that ask 'why not a POJO ?"

Then if itās just a storage bin for 4 floats then use Vector4f.

Quaternion values are all interdependent so it never makes any sense to set one without the other and is almost always a mistake. And by āalmost alwaysā, Iāve never once seen it not be a mistake if your intent is to actually have valid quaternions.

No, because there is a pretty measurable performance benefit for accessing fields directly, especially through final methods that might get inlined.

This is NEVER a valid operation for a quaternion. So itās good that we donāt let you make invalid ones here.

Okay, I give up.

(it is a valid operation in this case. Since Iām unpacking dataā¦)

Also not my pointā¦

Still there

What remembers meā¦

https://en.wikipedia.org/wiki/Technology_in_The_Hitchhiker's_Guide_to_the_Galaxy

A whole thread to get here. If you include your use-case up front next time then it will save a lot of trouble.

For unpacking data, keep four floats. Call set() when done. It will be faster and doesnāt expose users to problems.

ā¦and there WILL be problems because there are already a large number of posts from people who have messed up their Quaternions and they canāt even set the values directly.

While it sounds like you would really enjoy procedural languages (it was really nice in its own way), sometimes itās ok to let object oriented programming do what itās really good at: encapsulation.

2 Likes