Notifications
Clear all

[Closed] Rotate a vector by x degrees in any direction?

I’ve been trying to figure out how to rotate a vector by a specific angle in a random direction.
I don’t know if I’m over-complicating this, but I have got this far.

To create a random rotation in x degrees I’ve used the following:

 (eulerangles 0 x (random 0.0 360)) as matrix3

So I figure if I can convert the vector that I want to randomise into a matrix3, I can then just multiply the above result by that matrix3 for my result. But I don’ t know how to do that. Or am I just trying to do this the wrong way?

Cheers,
Cg.

5 Replies
1 Reply
(@polytools3d)
Joined: 11 months ago

Posts: 0

As you say, you just need to multiply the matrix by another matrix from the axis vector:

(
	fn RandomAngleVector axis angle:0 =
	(
		tm = (eulerAngles 0 angle (random 0.0 360.0)) as matrix3
		return (tm * matrixfromnormal axis).row3
	)
)

This is what I figured out. Probably a bit convoluted. But I can’t think of another way to do it right now.


		fn rotateVector theAngle p1 p2 =
		(
			-- rotates a vector in a random direction given a specific angle
			local theVec = p2 - p1
			local vecLength = length(theVec)
			local normVec = normalize theVec
			
			local randAngleXform = (eulerangles 0 theAngle (random 0.0 360)) as matrix3

			local z = normVec
			local x = normalize(randAngleXform.row1)
			local y = normalize (cross z x)
			x = normalize (cross y z)
			local tm = matrix3 x y z p1
			
			local tm2 = (randAngleXform * tm)
			local rotatedP2 = (tm2.row3 * vecLength) + p1
			rotatedP2
		)

in what space (coordinate system) do you want to rotate the vector?

(
	delete objects
	
	vect = (random -[10,10,01] [10,10,01])
	ang = 45
	tm = matrixfromnormal (normalize vect)
	-- spin it first
	prerotateZ tm (random -180 180)
	-- rotate to specified angle about any of X or Y axis
	prerotateX tm ang
	-- new vector
	new = (tm[3] * length vect)

	cylinder radius:0.1 height:(length vect) dir:vect wirecolor:orange  
	cylinder radius:0.1 height:(length new) dir:new wirecolor:green 
	
	format "angle:%
" (acos (dot (normalize vect) (normalize new)))
)

I guess I want to rotate it in the vector’s coordinate system. In a random direction by a defined angle. Thanks for the reply Denis. I’ll check your solution out when I get on the desktop later. I can only assume it’s going to be more economical.

Thanks again,
Cg.