• News

• Industries

• Learning & Support

• Community

• Marketplace

# Get new vector based on angle of two vectors

I feel like I am missing something simple here, I thought if I had two world space vectors I could grab the angle between them either with ‘Find Look At Rotation’ and breaking the rotator to get the yaw or by getting the degrees from normalizing the two vectors, getting the dot product, then the arccos. It does appear to give me the correct angle.

But then how do I make a vector based off that angle?

I tried to use ‘Rotate Vector Around Axis’ and give it the world space vector and axis set to 1.0 in Z.

The result I get is not something that is making sense to me, not sure how to even explain it other than to say it is definitely not what I need. My brain is too fried to think much now.

Here is an image of what I am trying to do:

I have A location vector, and I have B location vector, I get the angle between (white line) and want to make a vector off in the distance (above B / the black line).

Hello,
i remember another thread where something like this have been solved by using " get forward vector." Maybe it will work for you too.

Edit : like in this example for a line trace from : https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/UseRaycasts/Blueprints/index.html

https://docs.unrealengine.com/latest/images/Gameplay/HowTo/UseRaycasts/Blueprints/GHT2B_6.jpg

Well you have two locations A and B. To find the vector that goes from A to B and continues on into the distance, all you need to do is take B - A (subtraction). This is basically what a line trace does.

I have two vectors, that example uses an object to get a forward vector. How do I get the forward vector of a vector?

Not sure what you’re getting at… it is most certainly not what the line trace does.

Sorry - I’m not saying that’s what it does completely. What I recommend is brushing up a bit on 3D math and start with the basics like points and vectors in 3d space. Leave the game alone for a moment and think simple, then once you’re able to find the solution in a simple setting then you can then tailor that toward your liking in the game. Obviously I can’t solve your problem completely without knowing all of things you have going on underneath so I apologize if what I said is not giving you exactly what you need. Anyways good luck!

I’m not implying that’s all it does, I’m implying that subtracting vector B from vector A isn’t what a line trace does, otherwise the line in the image would point at B.

If I make a line trace with debug, it draws it from A->B.

There’s nothing really going on under the hood that’d change that. I have two world space vectors and that’s pretty much it.

A vector always points “forward” to some direction

Anyways, to answer your first post… what you want to do is subtract vector A from B then normalize the result (which is going to give you the direction with a length (aka magnitude) of 1) then you can multiply that with whatever distance you want to “extend” the line to and finally you add the result of that to either vector A or B (depending on what you want the starting point to be).

So in more programing terms the formula is: normalize(VectorB - VectorA) * DistanceFloat + VectorB

It would be a good idea to look into some basic vector math though, you can start here and also legacy unreal wiki here

Thanks for taking the time to respond. I’m not certain if I’m doing it correctly, does this look right? Changing the Distance float isn’t reducing or increasing the length, if it does anything sometimes it just changes the direction.

I will elaborate on my purpose in case of other methods…

When my character is near something, it will find each corner of the static mesh and store it as a vector array. My Update Position event will look at each vector in the array and exclude any corners that are not visible by doing a line trace from the character to the corner to see if it hits anything, if it hits something it can’t see it. However, since it’s tracing directly onto the corner it always hits something so I need to reduce the length of the vector by around 1-2% so that it wont hit the object if the corner isn’t occluded.

If there’s 3 vectors remaining, it also excludes the closest to the character. This gives me the two edges required for the last point on the mesh that the character can actually see.

I think you’re getting confused by the fact that Line Trace draws between two coordinates in world space, while this vector(B-A) is not a world location. A mathematical vector does not have an origin, it is just a direction and a magnitude, which means that when you plug in the result B-A, it will appear as if you’re drawing towards the center of the world. This is because you’re still treating it as a world location. For the trace to behave properly, you will have to do this:

Start: World location of Object
End: World location of Object + Vector(B-A)

Try the trace mentioned above, but where world location is from different objects. It will make more sense then.

Edit*
To clarify. In programming, a vector is just how to store some values. In math, a coordinate and a vector is NOT the same thing. While the values may look identical, they have different purposes. In programming it’s all a matter of how we use values, so we can cheat. So a vector can be used as a coordinate, and a coordinate can be used as a vector. Mathematicians cry, but we can have fun with it. Keep this in mind.

Nope! that’s incorrect… you should multiply the result of the Normalize node with a float number (not a vector!) then add that to vector B.

Example:

Indeed, I’ve also struggled with this at first. So to clarify (and anyone please correct me if I’m wrong):

There are two types of vectors in UE4 (and probably most other engines). One is the mathematical that has only magnitude (length) and direction. This one does not have a location. You can place that same vector anywhere in space. You can picture it as an arrow pointing somewhere. Neither starting nor end point of that arrow denotes an actual location. You could call this a direction vector.

The other kind of vector is a location vector. Technically it isn’t a vector at all, it just uses FVector as a type to store X, Y, Z values in. It’s just a point in space without direction or length.

Ah, thanks! For some reason my brain put parenthesis around the “DistanceFloat + VectorB”. This works perfectly. I’ve bookmarked the vector math pages you linked and I’ll either find time to get into them later or do it before my next attempt to calculate vectors.

Thanks a lot everyone for taking the time to help me out.

It seems most of my problems arise from the dilemma of needing to get stuff done and being brain fried from spending too much time attempting to get stuff done.

Indeed! Both types are stored in the same data type as the elements are only 2-3 floats. The magnitude is then returned by calculating the length, using sqrt(xx, yy, z*z) (Pythagoras). Square root is a relatively expensive operation, so it’s redundant to calculate and store it for all vectors, and is only calculated when needed. It’s up to us to remember what type of value we’re working with when we’re getting and setting a vector. It could be both a location and vector at the same time, depending on what we’re up to.

I hope we’re not going too off-topic Vaei. It’s just for future reference if other users end up in your thread. Good luck with your project.

Glad I could help, this vector stuff used to fry my brain too back in the day when I was starting out, but when I eventually got the basics clearly in my head it’s a piece of cake.

Not exactly! there are no two kind of vectors (well there is vector2d with just X & Y but that’s another story) every vector has a direction and a magnitude! what you are probably referring to by “direction vector” is a normalized vector (a vector with a length (magnitude) of 1)… and the “location vector” obviously has a direction and length too!

No such thing as off-topic when it comes to learning new things. It pertains to me also, since I came from 3D art and my understanding of a vector was an XYZ coordinate on a graph I was unaware of the magnitude/direction of a vector. Now things make a lot more sense!

I think my brain fries regardless of what I’m trying to solve I have spent days on the system I’m working on and its getting tedious so it definitely adds up. But with this I can move to the next phase, I am very grateful for the assistance.

For the sake of argument, isn’t a quaternion stored in a vector4? I guess that’s a dangerous question to ask here

Whenever you find yourself using arccos, arcsin, or arctangent, 99 times out of 100, you’re doing it wrong.
The only reasonably time to use it is when interpreting 2D user input. And, then, you always want arctan2 (with both dx and dy inputs) rather than asin or acos.

To find a rotation from vector A to align with vector B, you can find the axis of rotation R as normalize(cross(A, B)). If A is parallel with B, just pick any axis that’s normal to the vectors.
Then construct the third vector for the basis A by crossing A and R. Call this basis matrix Ba. Now, construct the same kind of basis for vector B, call it Bb. The matrix that rotates from A to B is transpose(Ba)*Bb.