Hello fellow forum members, I’m very curious on how I should make a formula for this.
I’m trying to make it so that B (Bar) can get lined up with 2 points. (P) ( []------[] )
I have calculated and have made a formula(S = (P.Size.Z + P2.Size.Z)2) to calculate the size, but I’m curious on how I should calculate and create a formula to find the position and rotation that B will be.
My best guess for me so far is to get the Magnitude between P1 and P2, other than that, I’m not very sure what to do.
Here is the Module Script (M) and regular script (S).
M
local module = {}
function module:CreateTracks(trackVar, point1, point2)
-- Size
trackVar = Instance.new("Part")
trackVar.BrickColor = point1.BrickColor
trackVar.Size = Vector3.new(1, 1, 2) -- Z = length up
local totalzVal = (point1.Size.Z + point2.Size.Z) + 2
-- Position
trackVar.Size = Vector3.new(1, 1, totalzVal)
trackVar.Position = ????
end
return module
S
local TrackModule = game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("Tracks")
local p1 = workspace:WaitForChild("Point1")
local p2 = workspace:WaitForChild("Point2")
local track = nil
tracks = TrackModule:CreateTracks(track, p1, p2)
local LongPart = --long part
local p1, p2 = workspace.Point1, workspace.Point2
local dist = (p2.Position - p1.Position).Magnitude --get distance between points with vector subtraction
LongPart.Size = Vector3.new(0.5, 0.5, dist) --make part as long as the distance
LongPart:PivotTo(CFrame.lookAt(p1.Position, p2.Position) * CFrame.new(0, 0, -(dist / 2))) --1. make part positioned at p1 and look at p2, 2. move part forward by half the distance because we made the part as long as the distance itself
I really don’t get CFrame.lookAt(p1, p2) and a bit of * CFrame.new(0, 0, -(dist / 2))'s math.
What is CFrame.lookAt(), why did you use CFrame.lookAt() on p1 and p2, Does PivotTo move the CFrame’s origin and also the part? Why did you Multiply CFrame.lookAt()'s product by the distance / 2? Why do we need the distance / to?
In the documentation, CFrame.lookAt takes three arguments
1st: Where the cframe is located at with a vector3 (in this case, p1’s position)
2nd: What vector3 should the cframe should be pointing towards (in this case, p2’s position)
3rd: Optional argument for upvector to control where it’s upvector will be
PivotTo() Moves the cframe of the part to the cframe in it’s arguments so yes the part moves along.
I multiplied the cframe from cframe.lookAt by the negative distance/2 because if i didn’t the bar would just stay in p1’s position, and since the bar’s length takes up the entire distance between the two points it should be moved by half the distance from the lookAt cframe which points to p2 so that it perfectly fits the gap.
I’m guessing the harder way to do this would be to find the midpoint of the line segment formed by those 2 points’ coordinates which is very easy a quick google search then move the part there and adjust its’ size based on the distance between the points once again easy pyth theorem and rotate the part based on the slope of that line tan^-1(slope). But roblox has a lot of functions that would probably make this much easier.
The magnitude retrieves a single numerical value for the shortest distance between 2 vectors. If you left out the magnitude, you would be inserting a vector for Z component which would be incorrect
The magnitude provides a size in studs which he used to size your Z component