Download

XY Distance calculation?

I have a shedload of X, Y character location coordinates that I need to calculate a single distance figure from a specific location (also an X,Y coordinate).

Can i ask what is the calculation to do this, I need to do these in an excel spreadsheet so need the calculation rather than the BP.

I’d appreciate some help!

You can use the distance blueprint node, one is for vector3 (x,y,z) and one is the same but for two actors.

If you want to manually do it it would be the vector length (another blueprint node) of one vector subtracted by the other.

If you wanted to further manually do it it would be: (source: https://stackoverflow.com/questions/…een-two-points https://stackoverflow.com/questions/…points/5230380) I recommend stackoverflow as a place to find alot of answers to common formulas used in games, with the added benefit of many examples being broken down to not take into account specific APIs that make things easier (so you can learn what the blueprint function nodes are actually doing behind the scenes and make it yourself if need be for a different purpose). You can even look at questions answered for Unity as an example, and find that Unreal Engine usually has an equivalent equal to whatever functions they use as generally the math for all these algorithms at the root are the same.
dist = sqrt( (x2 - x1)[SUP]2[/SUP] + (y2 - y1)[SUP]2[/SUP] )

Not sure why you’d need a spreadsheet for a bunch of different character location coordinates (maybe you do need it for your purposes), but if you didn’t want to do it in excel I’d recommend using two arrays of vector locations (one for the first coordinate, second for the other) and using a for loop that you’d potentially store in a third array that you’d return, depending on your needs.

Cheers for that, very much appreciated

Hi DeathStick,

I was wondering if I could pick your brains a bit, I’m clearly doing something wrong:

I’ve got my initial target that I’m trying to calculate distance from lets call this Location 1, its X,Y is 220, -208 respectively

now in trying to calculate its distance to Location 2, which has an X, Y of -321.163, -408.634

Following the logic above:
=SQRT((220–321.163)*2+(-208- -408.634)*2)

I’m getting 38.51, IMVHO this doesn’t look correct (I’m pants at maths and have clearly messed something up).

Can you please help point out where I’m going wrong, I’m pulling my hair out!

I’d really appreciate your help

You dont need to use pythagoras equation to do it, because lenght function for vectors is basically that.
so substract one location from another (does not matter which order) and use length node for result.

Hi Nawrot can you please show me how this is done using the values and data above ie;
Location 1, X= 220, Y= -208
Location 2, X= -321.163, Y= -408.634

Would MASSIVELY appreciate some help, I have a few different X,Y values that I need to calculate distance from a specific point, this data is being recorded into an excel spreadsheet so don’t need it to work in game.

ahh excel.

in your equation: “=SQRT((220–321.163)*2+(-208- -408.634)*2)” you multiplied by 2 here. it is not multiply it is power of two.

first get vector beetwen two points, i call it dist_vector. simply substract x of location1 and location2 , then y of both locations.

dist_vector = Location1 - Location2 . or dist_vector.x = location1.x - location2.x dist_Vector.y = location1.y - location2.y

now you can create vector length function, or just write this as next formula

distance = sqrt(dist_vector.x^2 + dist_vector.y^2) this is POWER of two not multiply by two.

Buty spreadshets are for calculating things, look into documentation of whatever program you are using, it should have build in function for vector math.

Cheers for that.

If done in ue4 bp am i right in thinking it’d simply be two vector 2ds subtracted from each other?

Location is also vector, just from 0,0,0 to where object is. If you substract 2 vectors that are not location, there may be different results depending which you substract from which. Just read something about vector math adding them and substracting.

Yes everything works same with 2d vectors and 3d vectors. If you do not have finctions for 2d vector you can always change 2d vector into 3d vector, just copy x and y and set z to 0.

in unreal BP just do this:

Awesome!

What I’m trying to do is compare actual values using specific X,Y coords versus a BP I’ve developed that shows the value onscreen:

For the example above this gives me 689.481

From Position A;
X Value:
220
Y Value:
-208

To Position B;
X Value: [TABLE=“border: 0, cellpadding: 0, cellspacing: 0”]

-321.163

Y Value: [TABLE=“border: 0, cellpadding: 0, cellspacing: 0”]

-408.634

Does this seem correct?

I’m VERY SORRY for being a pain but I’d really appreciate it if you could please run through the calculation with these values to determine whether I’m doing this correctly or not?

Uh let me guess, just in case.
You placed some objects on level, wrote down locations, and you want to know how far player is?
Can you write what you want to do for game, not what you want to do with vectors.

Because just for that there is easier method.

  • create dummy blueprint actor, lets call it marker. You may add to it some glowing ball or arrow, something that you set to be invisible in game, but visible in editor, so you can see them all in level.
  • place those actors where you want,
  • then use “Get all actors of class” and set it to your dummy blueprint actor class
  • then with result of that array use “for each”, you will get for loop that goes trough all dummy actors.

But there are probably multiple solutions, and i am guessing here what exactly you want do.

Hi Nawrot,

It supposed to display and record distance between goal (trigger) and last player position, if the player can’t get to the goal, it simply displays you were X cm away.

I think the code works and that’s kind of not the problem, I’m using the game for some player research and have a set of recorded values from the game, but I need to double check these values are correct by manually calculating each one against the values provided by the game, to absolutely ensure they’re accurate.

It’s for that reason I need someone who knows what they’re doing to look at the values I’ve got and determine if they’re correct.
I’d really appreciate your help.

D.

Will my bp provide the same results?

Yes if you put same “code” in your blueprint. Just connect in and outs correctly.

This is driving me mad, I’ve used the following set up just to provide values, but it is giving me 577?

Shouldn’t this be 689.481???

its the same if I set Z values to 0?

Please help Nawrot I’m going bald at the hair I’m pulling out with this!

Length of that vector for numbers you wrote in is indeed 577.
And yes you can set both Z to zeroes, it will not change length of vector.
Because when you subtracting 2 vectors you doing [vector_A.x - vector_B.x, vector_A.y - vector_B.y, vector_A.z - vector_B.z]
So same number minus same number is always zero.

Massively appreciated thankyou Nawrot