How to find the middle point between two vectors

Hi, I’m trying to understand exactly how the trace by box works. I can make it work when I just input the values.

I assume it works by taking the center of the box, start and end and then adding the size (half size values) to build the box. Because I’m trying to make it work as a drag box (think any top down game/rts). I start by setting the vector location of the left mouse button when pressed and then setting the vector location when the left mouse button is released. However I can’t get the mid vector location between these. When I do a line trace from the camera (floating above the scene) to the mid point (2nd point - 1st point) it shows me a random location.

I believe its because if the locations contain a minus in any of the x, y or z axis it gives a incorrect result.

No I know there is the rama series of add-ons that will no doubt be mentioned, I’m really trying to understand how to do it without it. No sense in just accepting I can’t do it and skipping it.

Any help or even an full example of a drag box in blueprints would be a great.

2nd point - 1st point provides you with the directional vector 1st → 2nd. If you add this one to your 1st vector you will again get your second vector.

To get the middle point you have to do this ((2nd - 1st) / 2 + 1st). 2nd - 1st to get the vector between those two vectors / 2 to get the middle and + 1st to add it to the location of your first vector. Otherwise it will just start at 0, 0, 0 and therefore give a wrong result.

You might want to take a look at these:

Vector math is everything but UE related even though it’s very important for quite a large amount of stuff you want to do. Kahn academy has quite a few pretty good videos about different vector operations and you can even train it on their website!

Cheers! :slight_smile:

6 Likes

Brilliant answer, thank you so much. First answer that was simple and short but made perfect sense. Plus thank you for the link.

Can’t you just do (v1 + v2) / 2 ???

4 Likes

Indeed. I totally didn’t think of that. This is why thinking in patters is not always great.

My immediate trail of through was to get the way and half it because of how the question was asked but this is actually the straight up better way!

1 Like

You can also lerp between two vectors giving you a result somewhere between the two vectors :slight_smile:

3 Likes

For anyone looking for the middle point between more than two vectors, beware: The solution above provides only the mean (average) point, not the median (centroid). For the median point, I followed this post here: http://stackoverflow.com/questions/12934213/how-to-find-out-geometric-median

you could also just do the following…

http://puu.sh/v8ffZ/96bfeb8732.png

GetActorArrayAverageLocation / GetVectorArrayAverage ?

1 Like

MidPoint by alpha
I think this is the best way
when Alpha is 0.5 we have midpoint

7 Likes

Best for what? Lerp is for linear interpolation and it’s used for interpolating a value over a period of time. So this method is best to interpolate a value over a period of time between two vectors. If I just need to find a center point between those two, (v1 + v2) / 2 is much better: simpler algorithm, faster, easier to understand.