# Rotating pitch and yaw causes roll

So I'm new to working with a 3D space and not sure how to fix my problem, I basically have a turret that rotates along its Z axis, and pitches along it's Y but after rotating 180 on the Z axis the turret points down as I completely understand despite being unexpected.

I have a fiddle with what I'm working on here https://jsfiddle.net/2v4ezmaz/2/

So the question is, how do I make it rotate like it does in blender where the barrel end stays pointing up while rotating the z? After lots of reading it's all to do with cross vectors since the .rotation property is euler angles which I think is my problem. I need to use quaternions but I can't logically see what needs to happen.

My brain is thinking of it as needing to reset the rotation back to its default then apply Z rotation and apply the Y rotation, but that can't be right.

After reading http://blog.wolfire.com/2010/07/Linear-algebra-for-game-developers-part-4 I'm sure my answer is there somewhere but I cant think what the code needs to be to do what i want.

Any pointers would be super helpful since my brain is fried reading/trying vector and matrix rotations.

• That fiddle displays some stuff that doesn't seem to be related to your question. I can't see where on the ship this problematic turret is supposed to be. – user253751 Sep 26 '16 at 23:56
• Strange, should be the left most turret from the default view. I know theres texture errors going on but shows up fine for me with chrome :/ – Julian Sep 27 '16 at 0:05
• No, I mean that I can't tell where the problematic turret is because you didn't tell me where to look. – user253751 Sep 27 '16 at 0:07

It appears the two rotations are being applied in the wrong order.

turret.eulerOrder = 'ZYX';


somewhere, and three.js will now apply the Z rotation first.

• Holy Cow! There's me getting all caught up in way complicated stuff just for that! Thank you so much! – Julian Sep 27 '16 at 0:22