I’m looking to find the value of X as shown in the diagram below. I have no idea where to start from code, any help would be greatly appreciated.
You could use Vector3:Angle to get the angle between the second part and the first, then use the formula cos(angle) = x/(distance/2) and rearrange to get x
Thank you for your reply, it has gotten me on the right track. Right now it’s returning an innacurate number. The distance length between the two parts is 44 studs in the example, so the ideal length of the center would be 22, however the function returns 23.058. I’m not sure what i’m doing wrong here but this is the code:
local RayA = part.CFrame.LookVector
local RayB = (part.Position - centerPosition).Unit
local Angle = math.rad(math.acos(RayA:Dot(RayB)))
local DistanceFromCenter = (NodeAPosition - NodeBPosition).Magnitude/2
local APart = Instance.new("Part", nodeGroup)
APart.Anchored = true
APart.Size = Vector3.new(6,1,math.cos(Angle)*DistanceFromCenter)
Any ideas?
I tried to use Cos, to get the pseudo-horizontal distance between the two points, but it overshoots… I am not sure as to why it does this.
since you just want to get x which is the distance to the center, can’t you just subtract the x value of one from the other, make it positive and divide by 2:
local x = Math.abs(part1.Position.X-part2.Position.X)/2
X is not the distance to the center, the distance to the center is the Hypotenuse
the hypotenuse would account for changes in y though wouldn’t it?
I’m not sure as to what you mean, but side X and the hypotenues will never be the same number, if that was the case you couldn’t have a triangle. I might be mistaken on this but I’m pretty sure that isn’t geometrically possible.
since you mentioned the parts LookVectors im assuming that you want it in the parts local space, you also said you want it
from surface-to-surface and not center-to-center so i subtracted half the size of the part from the distance
could this be what youre looking for?
x = ((-part1.CFrame:PointToObjectSpace(part2.Position + (part2.CFrame.LookVector * (part2.Size.Z / 2))).Z) - (part1.Size.Z / 2)) / 2
Would this work instead:
local distanceVector = part1.Position-part2.Position
local xChange = math.abs(distanceVector.X)/2
local yChange = math.abs(distanceVector.Y)/2
local xSide = math.pow(xChange,2) - math.pow(yChange,2)
xSide = math.sqrt(xSide)
This worked perfectly, thank you so much. I’m not sure I understand the math but I can probably break it down and find out.
Thank you for your help but that gave me a number less than 22 (20.503… and so on).
i dont understand it either i just typed random stuff until it worked
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.