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

Finding midpoint between two Vector3 values?

Asked by
KoreanBBQ 301 Moderation Voter
8 years ago

Context: I'm making a pathfinding script and I want it to create a path. To get the size of the path brick I'm doing (Size1-Size2).magnitude But I dont know for the position, because if the values are (1,1,7) and (5,1,5) than it's going to end up negative although the mid point is positive.

Help?

3 answers

Log in to vote
1
Answered by
funyun 958 Moderation Voter
8 years ago

There's another way you could do this, and it's called "linear interpolation", or lerping.

pointA = Vector3.new(234, 3456, 1)
pointB = Vector3.new(34, 867, 1123)
midpoint = pointA:Lerp(pointB, .5)
print(midpoint)
Ad
Log in to vote
3
Answered by 8 years ago

The midpoint of 2 or more vectors is essentially the "average point" of the vectors.

Finding the midpoint of 2 or more vectors is exactly like finding the average of 2 or more numbers: you add all the numbers up, and divide them by the number of numbers there are.

So to find the midpoint of the vectors, it's just (Vector1 + Vector2 + ... + Vectorn) / n

In your case, it would be (Vector3.new(1, 1, 7) + Vector3.new(5, 1, 5)) / 2

Hope this helped!

0
Didn't see your answer, sorry. Upvoted anyway. Spongocardo 1991 — 8y
0
Ohhhh average haha I'm so stupid. Thanks! KoreanBBQ 301 — 8y
0
And also, how do I make it so the part is rotateded in a way so it connects the two points? KoreanBBQ 301 — 8y
Log in to vote
1
Answered by 8 years ago

If you want to perform pathfinding quickly, just use ROBLOX's PathfindingService, it's much easier.

However, if you insist on getting the midpoint of two vectors, the correct way of doing so is to add each vector up and divide by two.

Say if I had two variables, x and y. x has a value of Vector3.new(2,2,2) and y has a value of Vector3.new(1,2,3).

To get the midpoint of these two vector values, I add x and y up and then divide by two, like so:

local midpoint = (x + y) / 2 --Add up both vectors first because division comes before addition in BODMAS/BIDMAS/PEDMAS.

This is equivalent to the following:

local midpoint = (Vector3.new(2,2,2) + Vector3.new(1,2,3)) / 2

If you print out midpoint, it should print out 1.5, 2, 2.5.

I hope my answer helped you. If it did, be sure to accept.

Answer this question