3
\$\begingroup\$

I'm using unreal engine 4. Currently I ray trace to a surface in game and I'm trying to find the location of the point relative to the surface1 instead of the world.

I have the world position of the origin of the surface and the world position of the impact point. I also have the X,Y,Z bounds of the object What I would like to find out is how to get X',Y' position of the hitpoint relative to the upper left corner. I've tried rotating the hitpoint around the origin to calculate the hitpoint based on refernce Roll, Pitch, and Yaw like so:

FVector RotatePointAroundPivot(FVector hitpoint, FVector pivot, FRotator angles) {
FVector dir = hitpoint - pivot;
FQuat quat(angles);

dir = quat*dir;
hitpoint = dir + pivot;

return hitpoint;
}

But it changes based on the rotation of the plane no matter what I do.

I also tried a very limited technique like so which just assumes it's in one of the correct orientations:

if (actorRotation.Yaw >= 0) {
    xpos = (actorBounds.Y - clickPosition.Y) / (actorBounds.Y * 2) * compscale.X * 1000;
    ypos = (actorBounds.Z + clickPosition.Z) / (actorBounds.Z * 2) * compscale.Y * 1000;
}
else {
    xpos = (actorBounds.Y + clickPosition.Y) / (actorBounds.Y * 2) * compscale.X * 1000;
    ypos = (actorBounds.Z + clickPosition.Z) / (actorBounds.Z * 2) * compscale.Y * 1000;
}

But it's flawed. All I want is to be able to use the surface like a screen.

Example surface

Any help would be appreciated.

\$\endgroup\$
1
\$\begingroup\$

Don't work so hard. Just take the top-left model-space vertex {-1, 0, -1} (or whatever) and project it into world-space.

Subtract. Done. :D

Alternatively, you could take the raycast one step further by un-projecting the world-space hit into the object's model-space and doing the math there. The hit would be relative to the object's model-space origin at the original, model-space scale.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.