How to rotate a Mesh to Match a direction Vector

I have two arbitrary points A,B and I want to use it to rotate a mesh

currently I get awkward rotation as in the right picture, Imagine I draw a debug line between A and B, i want the mesh to replace the debug line

I am using SetWorldTransform() to apply the transform

A two world space points to calculate the vector
the code is something like that
FVector directionVector = A-B;
FTransform transform =FTransform(directionVector .Rotation(), A-B/2);
Originally when I draw the debug line between A and B and the Mesh I get the left Image,
When I rotate the mesh using transform I get the right picture
The mesh is the green line and the black line is the debugline

Is this a direct copy of your source? What is your expected math here?
Set the transform to 1/2 the distance between A && B? (A-B)/2?
Or change B’s position B/2 and then take the distance between them A-B, and apply that to your transform? A-B/2

First, you’re getting location wrong. It’s (A + B) / 2, not (A - B) / 2;
Second, for Rotation you need to calculate B - A, not A - B; the former may give a ‘correct’ result with a symmetrical shape, but it’s actually backwards.

FVector LocA = PointA->GetActorLocation();
FVector LocB = PointB->GetActorLocation();

FVector NewLocation = (LocA + LocB) / 2;
FRotator NewRotation = (LocB - LocA).Rotation();

FTransform NewTransform {NewRotation, NewLocation};


Be gentle, vector math is not the easiest concept to grasp =)

1 Like

Thanks for help,
True vector math is pain…, Actually The math I used on my machine is correct, , it was a mistake here when I was copying the code, I have been trying to figure out why for hours, and ofc this is not an actual code, no one name stuff A and B :slight_smile:
and it was rotation problem.
I found out that the issue I had was Mesh Related and all the meshes I have been using was modified so they weren’t center and that was the cause of all the weird rotations