How to get the Magnitude of two CFrames?

The speed of the tween shouldn’t change. It will always complete in the time you passed to your TweenInfo object.

If you have a part at (0,10,0) and you want to tween it to (0,0,0), and the tween should take 5 seconds.

Now the speed is going to be (distance/time) = (10/5) = 2 studs per second.

But if the distance is lower, for example 5 studs, the speed is going to be (distance/time) = (5/5) = 1 stud per second. But I want to keep it at 2 studs per second, and TweenService doesn’t accept any Speed argument.

Now you might say, “Just do (distance/speed) = (5/2) and pass that number into the Time argument”.

Yes, I know it will be like that. But how do I do the same with rotation? I thought of doing (Vector3.new(cf:ToEulerAnglesYXZ()) - Vector3.new(cf2:ToEulerAnglesYXZ())).Magnitude to calculate the “distance” but that returns inaccurate results.

I can’t guarantee how well it will work, but I think this is a pretty good attempt at getting this thread back on track.

local DistX, DistY, DistZ = CF1:toObjectSpace(CF2):toEulerAnglesXYZ()

I can’t vouch for the results because this is Euler rotations and there are better ways of handling rotation, but I’m not the guy to ask.

I tried that. But it does not seem accurate.

It wouldn’t be. Not only is XYZ not useable in many circumstances, but it also (I think) will take the quickest rotation between the two points, even if that is not what you want.
I gave it as an example for others to better understand you.

Orientation is a vector. Why don’t you just do (orientation1 - orientation2).magnitude?

Then how does TweenService does it? Like if you tween a CFrame to another CFrame and the difference between them is only in the rotation matrix and not any of the X,Y,Z locations, it will still Tween it perfectly.

I also tried that. Its also inaccurate.

You’re trying to calculate a value that doesn’t have a definition (at least according to all the people in this thread so far, including yourself). If you don’t know what you’re looking for, how can you judge what’s accurate?

Its because I am testing it in Roblox Studio. So I do know if its accurate or not.

We cannot comprehend what exactly do you want, try to clarify.

So I am making a Ladder, that rotates from a “pivot” from the ground when you hold E, and when you release E it tweens back to the original CFrame.

It takes 8 seconds for the ladder to fully be placed on the wall, but if the player release the E key when, for example the ladder is half-way in its tween, it starts to tween back to the original position but with half the speed, I want it to keep the speed and not decrease/increase when the distance is smaller or bigger.

function a(b,c)
    return c*b
end
local p = workspace.Part
local d = (a(p.CFrame:ToAxisAngle()) - a(originalCFrame:ToAxisAngle())).magnitude/math.pi
local len = 5 --tween length in seconds for maximum (180°) difference
local tweenInfo = TweenInfo.new(len*d, Enum.EasingStyle.Linear)
8 Likes

How am I the only one that understands this?

When you interpolate something, it is ten studs away and the interpolation lasts two seconds.
That’s 10/2 = 5 studs per second for interpolation.
Now say you have something thirty studs away, but want to use the same speed rather than the same duration. That’s 30/5, or 6 seconds of interpolation. Without knowing the distance, either ten or thirty studs, you can’t figure exactly how long the duration should be.

He wants to apply the same to rotation, and needs to know how many degrees one angle is from another so that he can specify a duration based on degrees/second.

StrategicPlayZ, it sounds like in this instance you may want to look into constraints rather than CFrames, if just to solve this issue.

1 Like

I figured it out, thanks to emojipasta.

local function GetDistanceOfCFrames(cf,cf2) --
    local axis,theta = cf:ToAxisAngle()
    local axis2,theta2 = cf2:ToAxisAngle()
    return (cf.Position-cf2.Position).Magnitude,((axis*theta)-(axis2*theta2)).Magnitude
end
3 Likes

You could of just did this xD

local distance = (target1.CFrame.p - targer2.CFrame.p).Magnitude

Not quite what he wanted though.

@JarodOfOrbiter @527366

This post is old. I have already found a way to do what I was trying to do when I made the post, Lerping.

I thought TweenService animates object using specific increments, but I was wrong, it uses Lerping, so I applied the same concept later on and it worked better than this post.

1 Like

Coulden`t (Cf1.LookVector - Cf2.LookVector).Magnitude work?

That would result in a 0 for directions pointing opposite.
This still isn’t a solution by itself, but you can get the angle between two vectors like this.

math.acos(Cf1.LookVector:Dot(Cf2.LookVector))

Seriously though, this post is over a year old and it’s already solved.

2 Likes