Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Calculating the "distance" between two CFrame values?

Asked by 9 years ago

I'm using this piece of code to have a model point towards a part:

ship:SetPrimaryPartCFrame(CFrame.new(ship.Pointer.Position, planet.Base.Position))

But I want to have the CFrame slowly change so that the ship looks like it's turning, and not just snapping to the angle. However, I have no clue how to go about doing this. I figure that if I can somehow get the "distance" between the old CFrame orientation and the new one (when it points to planet.Base), then I could use a for loop to create a smooth animation. If someone could help me get started in the right direction, that would be great.

2 answers

Log in to vote
0
Answered by 9 years ago

To get the distance between two positions, you can use .Magnitude

http://wiki.roblox.com/index.php/Magnitude

local magnitude = (part1.Position - part2.Position).magnitude

To get the Vector3 Value of a CFrame just use CFrame.new().p

Ad
Log in to vote
0
Answered by
Diitto 230 Moderation Voter
9 years ago

It's not distance you need to calculate, but the difference between the angle measurements. And interpolation, also known as lerp. However, CFrame is different than Vector3 in that it has angles as well. Stravant made a nice CFrameInterpolation module that returns the theta, which is the angle difference, and you can use it to lerp CFrame with the angles.

Example usage:

local CFrameInterpolate=require(Workspace.CFrameInterpolator);
local OriginalPosition=ship.Pointer.CFrame;
local MovedPosition=CFrame.new(OriginalPosition.p, planet.Base.Position);
local Interpolator,Theta=CFrameInterpolate(OriginalPosition,MovedPosition);
print('Theta: '..Theta);
for Increment=0,1,.1 do
    ship:SetPrimaryPartCFrame(Interpolate(Increment));
    Game:service'RunService'.Stepped:wait();
end;

Answer this question