How can I get a look at rotation looking at center?

I have an object that I’m spawning many of. It’s a flat rectangle. The long edges point down the X+ axis (forward). I need this rectangles face to always be pointing towards the center. They should billboard always towards FVector(0,0,0).

Here’s what I have at my disposal right now. I have a locations where the pivot point of this object should sit. I have another FVector that this long rectangle should point at. and I have the FVector(0,0,0) that the rectangles face should point towards.

Hi Thumper,

What you are describing is called a Look At matrix, and it’s actually pretty easy to construct:

FVector const objectPosition(100.0f, 100.0f, 0.0f); // Get the transform of your object and set it to this.
FVector const lookAtPosition(0.0f, 0.0f, 0.0f); // Where you want to look at.
FVector const worldUp(0.0f, 0.0f, 1.0f); // World Z Axis.

FMatrix lookAt = FLookAtMatrix(objectPosition, lookAtPosition, worldUp);

// From here you can create a Quat and set it to your actor's rotation, or whatever you need.

Good luck!

Actually I need two axis to look at. So as a rectangle the pivot is located at one of the ends. The other end point towards one specific vector. Finally if you were looking down on this rectangle seeing it that axis (it’s up) would point towards (0,0,0).

This is how it looks so far.  

//Find FVector between A and B

tempEdge.edgeMiddle = ((tempEdge.edgeCornerB - tempEdge.edgeCornerA) / 2) + tempEdge.edgeCornerA;

//Find Middle of triangle

tempEdge.cellCenter = FVector(((tempVerts.CellCornerA.X + tempVerts.CellCornerB.X + tempVerts.CellCornerC.X) / 3), 
						((tempVerts.CellCornerA.Y + tempVerts.CellCornerB.Y + tempVerts.CellCornerC.Y) / 3),
							((tempVerts.CellCornerA.Z + tempVerts.CellCornerB.Z + tempVerts.CellCornerC.Z) / 3));

//Create Rotator to align mesh

FMatrix lookAt = FLookAtMatrix(tempEdge.cellCenter, FVector(0, 0, 1), tempEdge.edgeMiddle);

tempEdge.cellOrientation = lookAt.Rotator();

Hmm, I’m getting confused. Can you supply a picture of what you’re trying to accomplish?

Ahh, I just switched some of them around and I think I’ve got it now. Thanks!

I need to reopen this one. I don’t think it’s working. The diagram above explains what I’m trying to accomplish in code. So far, my rectangle object’s pivot is landing on the exact spot I need it to (not much of an accomplishment). However, it still needs to point towards a vector A while keeping it’s face (it’s a single sided rectangle) pointing towards (0,0,0) It doesn’t matter where it’s at in relation, the face should billboard towards (0,0,0).

When generating your LookAt Matrix, set your UpVector to be:

const FVector3 originPt(0.0f, 0.0f, 0.0f);
const FVector3 localUp = originPt - PivotPt; // PivotPt is the position of your rectangle.

So I went through and verified the locations. The pivot location is good and so is the look at location. I spawned little spheres to each part and they are all sitting in the correct locations.

FRotator AWorldCell::getCellOrientation(int32 faceIndx, int32 edge)
{
	FVector faceCenter = getFaceCenter(faceIndx);

	return FLookAtMatrix(faceCenter, getEdgeMiddle(faceIndx, edge), faceCenter * -1).Rotator();
}

This method takes the face index and the edge identification number. getFaceCenter simply takes the three FVectors that make up the triangle and (x1 + x2 + x3) *0.333333, (y1 + y2 + y3) *0.333333, (z1 + z2 + z3) *0.333333 to find the center. getEdgeMiddle takes the same two arguments and grabs the center of the two Fvectors in question by ((b - a) / 2) + a.

When I’m getting the LookAtMatrix, I’ve plugged the face center into “Eye Position”, Edge Middle into “Look At Position” and finally I get the direction vector for the worlds center (0,0,0) by multiplying it against the face center location * -1. What I see happening is the pivot point of the line I’m trying to spawn places at the correct location (face Center) but the adjacent end points in strange directions from the Look At Position (Edge Middle). Ultimately the top face of this line does not billboard towards the world center (0,0,0).

Any ideas?

It appears that using FRotationMatrix::MakeFromZX (faceCenter * - 1, getEdgeMiddle(faceIndx, edge)).Rotator() gets it pretty close.