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.
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.
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).
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).
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).