# 3D and 4D vector rotations [Solved]

Maybe a little farfetched asking this here, but it’s worth a try…

I’ve written a code to rotate vectors in 3D as well as 4D space, given two vectors e1 and e2 defining the plane of rotation:

``````function metaVec4.vRotate(v, angle, e1, e2)
-- e1 and e2 define the plane in which v should be rotated,
-- since rotation always occurs in a 2d plane.
e1 = e1:normalize()
e2 = e2:normalize()

-- Projection of v on plane
local v_par = vec2(v:dot(e1), v:dot(e2))
-- Portion of v not in-plane
local v_ortho = v - v:dot(e1)*e1 - v:dot(e2)*e2
-- Rotate the in-plane part using vec2 functions
v_par = v_par:rotate(angle)
-- Construct a 4D rotated vector by adding the rotated in-plane
-- part to the non-rotated not in-plane portion of v.
v_rot = v_ortho + v_par.x*e1 + v_par.y*e2
return v_rot
end
``````

Unfortunately, this method only works when e1 and e2 are orthogonal. I’d would like this to work with any two non-orthogonal vectors defining the plane of rotation. Does anyone knows how to do this perhaps?

I know this would be much easier with cross products in 3D, but there aren’t any cross products in 4D afaik.

Why don’t you rotate with `rotate(x, y, z)`? You will also need `translate` at the point where you want to rotate

@TokOut This function is about rotating vectors, not the canvas.

@TokOut - please stay out of things you don’t understand

@Kjell - maybe you could ask @LoopSpace nicely if he can help

Won’t work if e1 and e2 are in the same direction (but then they don’t define a plane so the rotation is not defined):

``````e1 = e1:normalize()
e2 = e2 - e2:dot(e1)*e1
e2 = e2:normalize()
``````

I’m mildly curious as to why you’d be rotating a vector in 4d. The 4d vectors in OpenGL are not really 4d vectors but really represent 3d vectors in an advanced way. So rotating them in true 4d space is rarely wanted.

@LoopSpace Thank you so much, that makes a lot of sense. I’m a little ashamed I didn’t come up with that myself.

And to explain why I need this: I’m busy making a small app in which you can rotate 4D geometries. Just out of curiosity. A couple of years ago I saw this video about a 4-dimensional puzzle game called Miegakure, and I was intrigued. Now I’m trying to comprehense the wonders of 4D spatial worlds through programming one myself.

http://miegakure.com/

Their demo video just broke my brain :s

it is not 4D displayed on 2D, bit only 3D diplayed on 2D. The 4rth dimension is managed via the change of scenery. And it is a simple isometric rendering, so there is no real 4d rotation involved. You can rotate a 4d object and project it in 2D, but the result is quite different.