Get point above object that player is looking at and find the vector between the object and point

What I am trying to do is find the vector of a object to a point using the position of the object and the position of the player coupled with the rotation of the camera. How would you recommend finding this vector? (I have done this using the player to object vectors length in the vector from camera rotation but this results in the vector I wish to find not being perpendicular to the player to object vector).

In 2D it looks like this:

I’m not sure how the camera’s rotation plays in to this. What does it matter what the camera’s rotation is if you just want a point perpendicular to the player->object vector? It sounds like it’d be a fixed world position (based on the player and object locations.)

Must the position be the centre of the screen? You want to find the vector offset from the centre screen position, such that it is perpendicular to the player->object vector?

The camera rotation gives the length of the vector I wish to find such that if I press left mouse down on the object and hold, move the mouse above the object and walk around, the vector I wish to find would be perpendicular to the actors position with a length given by the line made using the cameras rotation.

I can post some code I already have if that helps?

Something like this?

FRotator CameraAngle; //Camera Angle Rotator
FVector Player; //Player Postion
FVector Object; //Object Position

FVector DistVector = Object - Player; //Let's get player->object distance vector
//Let's get angle diffrence between camera rotation and distance to object
FRotator AngleDiff = CameraAngle - DistVector.Rotation(); 
//Let's rotate the distance vector by resulting angle
FVector LookAtVector = DistVector.RotateVector(AngleDiff);
//Based of new vector we can world location of it by adding it back to player location
//Having that let as get distance vector between object->look at point on same distance
FVector ResultVector = Object - (Player + LookAtVector);

Sorry i was trying to rotate AngleDiff, i meant DistVector ;p corrected

You should combine both powers of FVector and FRotator, they both have functions that let you compute positions without knowledge of trigonometry

You can find the length of the camera vector using acos and the angle (camera->point, actor->object.) I’m assuming the camera is on the player. Or, at least, you are comparing camera->object and camera->point. Once you have the length, you can multiply it by the camera’s rotation vector to get your point. Then use that minus the object’s location to get your vector.

angle = 90 * ( 1 - ( ( object.location - camera.location ) dot ( camera.rotation.vector ) ) );

cos(angle) = (camera->object) / camera->point)

Therefore camera->point (length) = (camera->object) / cos(angle);

multiply camera->point by the camera’s rotation vector to get the point vector.

Take away the object’s location form point to get your offset vector. If that first line checks out, I’m not sure. It sounds about right, though. Assuming you want something like this: http://puu.sh/md9t6/dc85d99d0e.jpg

Edit: You’ll need to normalise ( object.location - camera.location )

Anyone doing anything in 3d should know some basic trig. This is high school stuff.

True enough. But, even so, you need some basic understanding of how it all works, even if you can’t remember the specifics, or you’d never know which functions to use!

Well he clearly dosn’t and UE4 let you skip that :stuck_out_tongue:

I have tried the above solution but it did not work, the blue arrow goes in the wrong direction (representing the resulting vector (see picture). Also there is an error with:

 FVector LookAtVector = DistVector.RotateVector(AngleDiff);

As an FVector has no method for RotateVector but a FRotator does so I made it AngleDiff.RotateVector(DistVector), which is what I think you were getting at in the first comment.

I’m on mobile but check if this works and let me know what issues arise:

FVector ObjToPlrN = (PlrPos - ObjPos).Normalize();
FVector LineEnd = PlrPos + (CameraForwardvector * 1000000.0f);
FVector IntersectPnt = FMath::LinePlaneIntersection(PlrPos, LineEnd, ObjPos, ObjToPlrN);
FVector DesiredVec = IntersectPnt - ObjPos;

I hope I understood your problem correctly. My code will work as long as you’re not looking straight at the object. You can check that the dot product between the camera forward vector and the vector from the player to the object is not zero (greater than 0.0001f would be safer).

Thanks. I did have a bit of an issue with that code but this enabled me to figure out the problem using this:

FVector IntersectPnt = FMath::LinePlaneIntersection(CamLoc, CamRot.Vector() * 1000.0f, Object->GetActorLocation(), (CamLoc - Object->GetActorLocation()));
	FVector ForceVector = IntersectPnt - Object->GetActorLocation();

I can’t get the arrow to work correctly but the vector appears to be working which is the good thing.