Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with getting Y Rotation of brick rotated on all axes?

So I have a brick thats rotated on all three axes (X, Y, Z). I want to get rid of the X and Z rotations of the brick but get the Y rotation from that brick, all through a code. I noticed that if I apply this fix to a brick manually, the Y rotation ends up different than before you apply the fix. (The fix is removing the X and Z rotations.) I have tried a variety of things, but am so far coming up with nothing. I was thinking of using Euler Angles, but when I tried working with them nothing seemed to work. I never have used Euler Angles before, however, so I would appreciate any help you guys have for me if the solution does end up needing them. Thank you.

==PICS== What I want to happen: Click

What your code does (gray bricks with studs): Click

What I want the brick to end up as: Click

Red=What current version does, Green=What the brick with just Y would look like: Click

1 answer

✓ Accepted answer

Euler Angles is a way of describing 3D rotation using three different angles, what we would call x, y, and z. However, this isn't the way Roblox stores rotational information. They instead use a Transformation Matrix.

In order to figure out what the Y-Axis rotation is, we will need to extract the Euler Angles from the transformation matrix.

###Solution to your problem

local x, y, z, m00, m01, m02, _, _, m12, _, _, m22 = part.CFrame:components()
local rx = math.atan2(-m12, m22)
local ry = math.asin(m02)
local rz = math.atan2(-m01, m00)

part.CFrame = CFrame.new(x, y, z) * CFrame.Angles(0, ry, 0)

Edit

Sorry I had a bit of a mixup, these do calculate the Euler angles but that isn't actually what we want. We want the heading, attitude, and bank of the block. Doing this is very similar and can be done as so:

local part = game.Workspace.Part

local x, y, z, _, _, m02, m10, m11, m12, _, _, m22 = part.CFrame:components()
local heading = math.atan2(m02, m22)
local attitude = math.asin(-m12)
local bank = math.atan2(m10, m11)

part.CFrame = CFrame.new(x, y, z) * CFrame.Angles(0, heading, 0)

This should work how you want it to.

Be careful however, you can't just say CFrame.Angles(rx, ry, rz) as you have to do them all separately or else it won't build the transformation matrix correctly. You can however simplify to: CFrame.Angles(0, ry, 0)*CFrame.Angles(rx, 0, rz).

9 comments

0

Look at the pics I just edited into my post. The code does something weird when I use it, as shown in pic 2

0

What exactly are you doing to rotate it? When I test it, it gets the heading just fine.

0

Just creating a new part and replacing part.CFrame = CFrame.New(x, y...etc. with the new instance

0
0

Did you set the CFrame of the new part to its own CFrame multipled by the angle? If so try this: newPart.CFrame = CFrame.new(x, y, z) * CFrame.Angles(0, ry, 0)

0

It flattens the brick but the Y rotation comes out incorrectly. https://gyazo.com/85dd0cdf41c63a5d36422748c0175e0e

0

The bricks start rotating on the 7 axis in ways that aren't even close to the original brick

0

Sorry about that see my edit

0

Thanks so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.