Problem rotating an object around a specific axis

How do you rotate an object about an axis that it is not lined up with?

Here’s an example:
Lets take this cube. Its axes are represented by the red, green and blue lines. I want to rotate the cube about the yellow line! this is an easy task as the red axis happens to be lined up with the yellow line.

5a6f196d342d89293ec75f2315ceb8af

If I wanted to do this via code, all I would have to do is add a few degrees to the X argument of the part’s orientation:

07ceccb33cbd0ca3802261b6be2c3f23

.
.
.

But what if the part is already rotated a certain way? I want all of those rotations to remain the same, but I still want to rotate it around the yellow line like so:

bd2d75b56152dbaad68fe50e134519d2

.
.
.

None of the axes line up with the yellow line however. When i try to rotate it with any of the three axes, it will not rotate around the yellow line.

9fba9717c98643e80e9f72442474747b

What i need is a collection of changes for each axis that will produce the desired rotation:

d38e1da68b7ca0d3cb50c7d77246e07c

How would one go about making this happen?

2 Likes

You could use CFrame.fromAxisAngle. its takes two parameters:

  • Vector (a vector that describes the axis of rotation)
  • Angle (angle in radians of which to rotate)

This should allow you to rotate a CFrame relative to world space

4 Likes

I might be able to make that work, ill get back to this thread if i find an answer

Nevermind. I misunderstood what CFrame.fromAxisAngle() does, this ended up being exactly what I was looking for. Thank you.

For anyone who happens upon this thread, this piece of code achieved what I needed:

local function update(deg)
	--convert to radians
	deg = math.rad(deg)
	
	--Direction of the modified rotation
	local rotCF = CFrame.fromAxisAngle( Vector3.xAxis, deg) --+ Motor.C0.Position
	
	--Update Motor
	Motor.C0 = rotCF * OriginalCF
end

There’s a model set up with a motor6d connected to a rotated part. This function accepts an angle given in degrees, and rotates that model along the designated axis.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.