Part's Object Space Position changes when I rotate it

I set a Parts CFrame at

CFrame.new(-243,6,3):ToObjectSpace(origin.CFrame)

So the relative Position is (0,-6,0)

But then if I rotate the part, the relative Position changes. Even though in world space the parts Position doesn’t change when you change the rotation.

I put one part at Worldspace Position (-243,6,3) unrotated and
one part rotated.
output

The Y and the Z axis are swapped

I assume the reason for this is when multiplying the CFrames it takes the parts rotation into account.

How can I get a Parts correct Position and Rotation relative to an Object even if it’s rotated?

1 Like

CFrame.new(0,-6,0):ToWorldSpace(origin.CFrame) gives origin.CFrame relative to CFrame.new(0,-6,0)

You would almost never have to do anything like this
A proper implementation of object to world space would be:

origin.CFrame:ToWorldSpace(CFrame.new(0,-6,0))

I just realized I wrote the wrong thing I meant to write :ToObjectSpace

I fixed it now

what I meant was

CFrame.new(-243,6,3):ToObjectSpace(origin.CFrame)

if the part is not rotated it would be (0,-6,0)

but if the part is rotated I would get the same numbers but they would be on different axis (in this case (0,0,-6)

I mean thats just the nature of object space
Its based on both orientation and position

Do you want it to ignore the orientation?

No I don’t want to ignore the Orientation I just don’t want it to affect the Object Space Position

As in I want it to act like a part in world space, if a part is at coordinates (0,8,0) then I rotate it, it doesn’t then make the Position (0,0,8). It keeps the Position (0,8,0) and adds to the Orientation.

Are you rotating origin.CFrame?

If you are it shouldnt cause issues as long as you set it up correctly

An example of it being rotated correctly would be like

CFrame.new(-243,6,3):ToObjectSpace(origin.CFrame) --world space to local
CFrame.new(-243,6,3):ToWorldSpace(Vector3.new(0,-6,0)) --local space to world

no Origin.CFrame has no Rotation.

I’m think I’m confused with the :ToWorldSpace function can you replace the CFrame.new and Vector3.new with “ObjectSpace” or “WorldSpace” or “origin.CFrame”?

Also doesn’t it take a CFrame as a parameter not a Vector3?

Let me give some context to what I am doing.

So I take Mouse.Hit which is in Worldspace and place a Part there, then I convert Mouse.Hit into ObjectSpace relative to origin.CFrame, then I convert the ObjectSpace into world space and it’s in a completely different Position

local cframe = Mouse.Hit

local part1 = Instance.new("Part")
part1.CFrame = cframe

local objectspace = cframe:ToObjectSpace(origin.CFrame)

local part2 = Instance.new("Part")

part2.CFrame = objectspace:ToWorldSpace(origin.CFrame)

I think I am getting ToObjectSpace wrong or ToWorldSpace wrong, but I don’t think that solves the original question from the OP about why the object space position changes on rotate.

Okay I’ve figured out the solution and it fixed both problems!

When I did :ToObjectSpace it was inverted so all I did was do Cframe:Inverse() and it corrected it.

c = a:ToObjectSpace(b)

b = a:ToWorldSpace(c)

This would be more obvious if roblox didnt use the weird ToObjectSpace and ToWorldSpace functions

c = a:ToObjectSpace(b) is the same as c = a:Inverse() * b
b = a:ToWorldSpace(c) is the same as b = a * c

When you do
c = a:Inverse() * b to get object space relative to a, its quite obvious that all you have to do is multiply a by the whole thing to extract b

Multiply both sides by a
a(c) = a(a:Inverse() * b)
The as cancel out
a*c = b

This isnt self explanatory at all when roblox creates these “easy to use” cover functions instead of the actual math

So in your case, when youre doing:

objectspace = cframe:ToObjectSpace(origin.CFrame)

Youre actually doing:
objectspace = cframe:Inverse() * origin.CFrame
You can then infer from this the formula you would use to get the origin.CFrame, or the original CFrame back by itself
You would obviously just multiply both sides by cframe
cframe*(objectspace) = cframe*(cframe:Inverse() * origin.CFrame
cframe then cancels out and youre left with the formula you need
cframe * objectspace = origin.CFrame
Or, in terms of robloxs bad functions
originCFrame = cframe:ToWorldSpace(objectspace)

2 Likes

Thank you for explaining this really helps I’ve been trying to figure out :ToObjectSpace and :ToWorldSpace for 3 days now!

1 Like