Saving rotation as Vector3 or Quaternion in database? Best practice.

Hey guys,

I’m currently in the setup of my database schemata. I would like to know if I should save the rotation as Vector3 or Quaternion.

Are there any advantages to have the rotation as Quaternion in the database?

to do what? context tends to be important when you ask “which way is best?”

1 Like

I would like to save an items rotation to make it available again after a player reconnects.

ah so it’s an online persistent world kind of thing?

Exactly. Currently I think it would be better to save it as Vector3, so it would be easier to modify the rotation through the database, as I don’t have to care about rotation.W.

I’d always recommend Quaternions, if only just because of fear for the dreaded gimbal lock.

That’s only an issue when changing rotation.

If I say “rotate by” some rotation, and that rotation is euler, it can have gimbal lock.

But the current state of the rotation… that’s no big deal. When you read it from the database, a quaternion is going to be created from said values, and set to the Transform. No gimbal lock.

OP should only be concerned about that if their database code plans to update the rotation in the database arithmetically… which I highly doubt will be happening.

Really, you could store the rotation as euler (or even in a matrix). Convert to quaternion when performing arithmetic and back when complete. And you would avoid gimbal lock just as well. The only reason this is a bad idea is that it adds that ‘conversion’ step EVERY time… incurring that 4 extra bytes of memory makes more sense than performing that complicated conversion every time.

SO, with all that said. At OP:

Databases are all about having easily queryable data that is compact. You want to take up as little space as possible when storing data.

Quaternions require 4 numeric values that are cryptic when viewed.
Euler requires 3 numeric values that are human readable.

I think the answer is clear which I’d go with.

Indeed, that’s the answer that Unity itself goes with for the exact same problem - rotations are always stored as Euler angles.

Looks around I don’t know why you guys give old gimbal such a hard time… guys?.. guys?.. where did you go? Crap my head is stuck…

1 Like

I’m not sure how serious the inquiry is, but here’s a good video explaining gimbal lock and why it sucks:

I think it was quite obvious it was a joke.

I know I giggled at it.

1 Like