Lets say I have to Vector3
’s: Vector3.new(-10, 0, 0)
, and Vector3.new(10, 0, 0)
. How would I position a part in between those to positions? Like I could simply just position them at Vector3(0, 0, 0)
(Which would directly in between them, and would be correct), but the positions could be different every time. I remember a topic like this a while back, but I could not find it, so thats why I am posting a topic on this.
To figure out the vector in between I believe you would have to average the two to together.
For example, NewVector3 = (Vector 3.new(-10,0,0) + Vector 3.new(10,0,0)) / Vector3.new(2,2,2)
I’m not sure if this would work since I am on mobile and can not test it, but I assume this would work.
You don’t need to divide the sum of the positions by another Vector3, you could just do:
local MiddlePosition = (PartA.Position + PartB.Position) / 2
However, it’s just a small consideration for simplicity
I use Lerp for this and place it at 0.5 alpha:
local middlePos = Part1.Position:Lerp(Part2.Position, 0.5)
Adding the positions and dividing them by two will be a little bit faster because this is lerp:
function lerp(a, b, t)
return a + (b - a) * t
end
and this is the faster solution
function getPositionBetween(a, b)
return (a + b) / 2
end
As you can see there’s only one addition and division whereas lerp has addition, subtraction and multiplication. Also division by two is probably optimized because dividing by two is often compiled to using a binary shift operator on a number. For example in C++:
int a = 200;
int b = a / 2; -- 100
int c = a >> 1; -- 100
Are you sure you’ve tested it? It doesn’t seem to work.
You’re calculating a number in your
find_midpoint
and making a new Vector3 with that number as its X component only, you’re not constructing a full Vector3 from the original positions.
The output shows that, as I mentioned it , will have to be modified to your liking
People are overcomplicating things
You can simply position a part in-between two positions by doing this.
Imagine two positions A and B as the points that dictate where the middle is.
local A = Vector3.new(-10,0,0)
local B = Vector3.new(10,0,0)
local middlePart
middle.CFrame = CFrame.new(A, B) * CFrame.new(0, 0, -(A-B).Magnitude / 2)
Vector3.new((Part1.Position.X+Part2.Position.X)/2,(Part1.Position.Y+Part2.Position.Y)/2,(Part1.Position.Z+Part2.Position.Z)/2)
This is the simplest I can say.
Using the math that teaches in junior high school.
How is it simpler than (Part1.Position + Part2.Position) / 2? Your solution is definitely not the simplest.
It would help to actually explain why you think this rather than trail off after leaving a comment like this. The answer may be obvious but explaining to people the why helps contribute to discussion, show potential other readers what the issue with this code is and serves as a way to direct users to a better way of doing things and why it’s better.
Your answer should be the solution because of its flexibility
(PartA.Position + PartB.Position) * .5 -- this works
(PartA.Position + PartB.Position) * .25 -- this doesn't
Part1.Position:Lerp(Part2.Position, n) -- this always works, if n is -math.huge or math.huge