How to get the position of a rotated object?

Hello, I’m trying to get the position of the end of the part event when its rotated.


lets inspect how this might work:

image

As you can see, normally I would do +Size/2 to get the end of a part. However, look at how this equation would work on a rotated part:


image

As you can see on a rotated part, this position would not be the correct one. So how do I ensure that the position I am getting is correct, even when the part is rotated, so it would look like this?
image


This is basic trig, but luckily CFrames can do this calculation for you.

Use part.CFrame * CFrame.new(part.Size.X/2, 0, 0)

so lets say I wanted to check if a rotated part was beyond the position 10 studs on the X axis. I could do:

local cf = part.CFrame * CFrame.new(part.Size.X/2, 0, 0)
local posX = cf.Position.X

if posX > 10 then
print("Rotated part past 10 studs")
else
print("Rotated part within bounds")
end

Could you check the magnitude?

local cf = part.CFrame
local newPos = part.CFrame * CFrame.new(part.Size.X / 2, 0, 0)
print((cf.p - newPos.p).magnitude)

the magnitude appears to be

2.000002145767212

which differs from when it has a rotation of 0 which is

2

If you’re concerned with your equation, I tested it with this code

while true do 
   local part = workspace.Part 
   local newPos = part.CFrame * CFrame.new(part.Size.X / 2, 0, 0)
   part.CFrame = newPos 
   task.wait(.5) 
end

and it appears to be fine. It started moving diagonally.

Does that matter? You can just math.floor it

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