Find Two 3D Points Relative Position (Left or Right)

Hey guys!

I’ve been playing around with the engine for a while now, and I’ve been trying to find out how to find the relative position of a point (preferably, with a good accuracy, so angles would be the best way I guess), but I haven’t been very successful so far.

FRotationMatrix::MakeFromX(Point1 - Point2).Rotator();

Any hints?

Hi,

A relative position to which reference coordinate system :slight_smile: ?
You need a position and an orientation in order to transform a point into another space.

You can transform P2 into endless spaces, because P1 has no orientation!

You get P2 in “P1’s space”, (assuming P1 is in identity-world-space, thus having the same orientation as the common world-axes) if you calculate:

FTransform trafoP1 = FTransform(FVector(1,0,0), FVector(0,1,0), FVector(0,0,1) , P1);

FVector P2inP1 = trafoP1.InverseTransformPosition(P2);

You could also write:

FVector P2inP1 = P2 * trafoP1.SafeInverse();

There are also other ways to do this.

It depends on your reference coordinate system.

What are you trying to achieve ?

Well I want to know if the point is on the left or right of my character. So yeah, I guess it needs to be in world-system-coordinate.

Cross product and then Dot product is a really quick and cheap way to know if something is to the left or right of something else.
From the old scripture… Legacy:UnrealScript Vector Maths - Unreal Wiki

The UE4 way of doing dot product is - FVector::DotProduct | Unreal Engine Documentation

Yes I had considered that too, the problem is, it works for Actors, you need a direction basically, and with two points, you have none of those.

That should be easy.

You get the object-to-world transform of your char with ACharacter::GetTransform().
Inverse-transform your point with it. If the resulting transformed position has a negative Y-Component, the point is left of your character.
If it is positive, it is right of your character.

Code:

void ACharacter::Foo()
{

FTransform trafoCharToWorld = GetTransform();
FVector vecTest(...);


if ( trafoCharToWorld.InverseTranformPosition(vecTest).Y < 0 )  
{
    // left side of character
}
else if ( trafoCharToWorld.InverseTranformPosition(vecTest).Y > 0 )  
{
    // right side of character
}

}

Without comfort-methods:

FVector vecTestWorldToChar = vecTest * trafoCharToWorld.InverseSafe();

if ( vecTestWorldToChar.Y < 0 )
{
// left side of character
}
else if ( vecTestWorldToChar.Y > 0 )
{
// right side of character
}

Hope this helps

@djchase- I’m so sorry, maybe I wasn’t clear, but I actually need the angle, well I could use the angle, because there will be points which aren’t at 180º/- 180º of the character, and those are the ones I need, I have to ignore all others… Sorry for not being clear the first time!

No problem :slight_smile:

Angle of two vectors can be calculated with

float fAngle = FMath::RadiansToDegress(acosf( FVEctor::DotProduct(vec1, vec2) ) );

You can use the previous method additionaly if you want to know if it is a positive or negative angle.
(Although there should be a faster way without having to use linear algebra)

Perhaps googling for azimuth calculations would also help…

For example this site:

http://tchester.org/sgm/analysis/peaks/how_to_get_view_params.html

Let me know if you need additional code

It gives me wierd HUGE numbers, like 2147483648. Actually it always gives me that number, either in positive or negative form