Getting the magnitude of two vectors is easy:
(v1-v2).Magnitude
But how to get the Magnitude of two CFrames? A magnitude that also takes into account the rotation.
For example I have a CFrame at 0,0,0 facing upwards, and another CFrame at 0,0,0 facing downwards.
If I do (cf1.Position-cf2.Position).Magnitude
, it will return 0 because it doesn’t care about the rotation. How do I make it care about the rotation too?
2 Likes
This does not make sense because the “Distance” from a CFrame and a Vector3 are the exact same concept.
If A
is a CFrame pointing some direction, and B
is a CFrame pointing some other direction that is not A
's direction, then d
is just (A.Position - B.Position).Magnitude
. The direction of the CFrame does not change the distance between them.
Yeah it does not make sense, I know. I am not good in maths so I am saying it like that.
Like the “distance” between two orientations.
1 Like
I think the first one. “Distance” between two orientations.
Does this mean the distance on a sphere then?
For example I have these two squares.
The one on the left is rotated 45 degrees. And on the right is 0 degrees.
If I want to find out the “distance” of their rotations, it should come out 45 degrees since the left one is rotated 45 degrees and the other one is not. How do I do that? I don’t know the exact term for this so I refer to it as “distance between rotations”.
Are these objects always rotated in one dimension? (Can only turn Left or Right, can’t rotate Sideways into Z dimension)
In the picture they are rotated in 1 dimension. But in Roblox they can be rotated in any dimension, either X,Y,Z or all of them.
No, that’s not what I am looking for. I will try to give a better example.
You know TweenService?
If I play a tween on a part’s CFrame whose Position is (0,0,0) and the orientation is also (0,0,0) to a part’s CFrame whose position is the same as the other part, BUT its orientation is (0,90,0).
If TweenService used .Magnitude to calculate the distance, it would come 0 and the object would not Tween at all since its Position is same as the target, but it uses something else to calculate the “distance” because the object still tweens, but only the orientation changes. That means its also calculating the “distance between two orientations: (0,0,0) and (0,90,0)”.
I’m not sure what you want. You can’t represent the “distance between two orientations” for a 3D vector with just one number because you need two numbers to describe the rotation of a 3D vector.
A better question might be, what are you trying to use this information for? What are you trying to do?
1 Like
I am trying to use this information to make a part’s CFrame tween to a LookAt CFrame, but regardless of the “distance”.
Tween service accepts time values in it, but the time value I am going to put is only for how much time it should take for the FULL “DISTANCE” between two CFrames, so that means if the “distance” is smaller or bigger, the speed of the tween will change, I want to keep it at a constant speed.
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.