Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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:

alt text

Product Version: UE 4.10
vector 2d.png (8.0 kB)
more ▼

asked Dec 29 '15 at 10:52 PM in C++ Programming

avatar image

32 7 7 8

avatar image TTaM Dec 29 '15 at 11:02 PM

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?

avatar image hydroflax Dec 30 '15 at 01:28 AM

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?

avatar image TTaM Dec 30 '15 at 01:54 AM

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 )

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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

more ▼

answered Dec 31 '15 at 05:57 AM

avatar image

1.6k 49 48 218

avatar image hydroflax Dec 31 '15 at 07:27 AM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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);
more ▼

answered Dec 30 '15 at 02:27 AM

avatar image

37.8k 937 172 1123

avatar image Shadowriver Dec 30 '15 at 02:33 AM

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

avatar image Shadowriver Dec 30 '15 at 02:35 AM

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

https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Math/FVector/index.html https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Math/FRotator/index.html

avatar image TTaM Dec 30 '15 at 02:50 AM

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

avatar image Shadowriver Dec 30 '15 at 02:53 AM

Well he clearly dosn't and UE4 let you skip that :p

avatar image TTaM Dec 30 '15 at 02:55 AM

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!

avatar image hydroflax Dec 30 '15 at 07:46 PM

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.

alt text

2015-12-30 (1).png (313.8 kB)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question