- Home /

# Relation between two 3D vectors that are in the same plane, to translate to 2D

Hello,

I've never asked a question on here or any other answering site but this has been bugging me for days and I'm sure there must be an easy answer to it.

I have two 3D unit (normalised) direction vectors D1 and D2 (so they go from point 0,0,0 in some direction).

Both D1 and D2 lie in the same plane (so there is a particular view in which if you look at the direction vectors from this point they will appear to look like one vector).

I have another unit direction vector, this time in 2D called T1.

I want the 2D unit direction vector T2 that is some rotation round from T1.

Going back to the 3D direction vectors, they share a normal because they are in the same plane. If I look at the 3D vectors from a point along this normal then in 2D T2 should be at a rotation from T1 that is the same as from D2 to D1.

Thank you!

I've drawn this too:

Could you say what you are doing with this, might make it easier to help you.

I'm trying to make it so I can draw a 2D line (on the part of the texture that represents the triangle) in the direction of a 3D direction vector that is in the same plane as the triangle. I was thinking initially that I could use a quaternion for this but I couldn't find anywhere that explained how to get the quaternion between two vectors that was relative to an arbitrary plane in 3D space so that I could apply this quaternion to a 2D vector and get the direction I wanted relative to the side of a triangle. I hope this helps :-)

Does this help: http://stackoverflow.com/questions/8942950/how-do-i-find-the-orthogonal-projection-of-a-point-onto-a-plane

You basically want to project the vector onto the plane right? that way you'd be able to get the equation of the line...

Hmmm, I already have the points on the plane of the triangle. An interesting idea might be to get the orthogonal projection of my vectors on the ground (plane with position 0,0,0 and normal UP) and then get the quaternion between them...On second thought I don't think this would work well if the triangle plane was completely vertical resulting in the two projected vectors being just one solid line. I thought I'd post this anyway incase 1)I'm wrong OR 2)It gives someone else some inspiration somehow. Thank you :-)

Ok I must be missing something. If you have the points on the 2d plane then the direction is just the normalised difference between them isn't it?

**Answer** by scarecrow77
·
Jan 23, 2013 at 10:25 PM

The real answer to my question and indeed a much much better explanation can be found here: http://answers.unity3d.com/questions/383804/calculate-uv-coordinates-of-3d-point-on-plane-of-m.html :-)

**Answer** by aldonaletto
·
Jan 17, 2013 at 10:51 PM

Well, if you just want to copy the angle between 3D unit vectors to 2D vectors, the job is easier. A possible solution is to calculate sin and cos from the 3D vectors, and use them to position the 2D vector. Since the vectors D1 and D2 are normalized (unit length), the ubiquitous Dot and Cross products can do the job: Dot(D1, D2) returns the cosine, and Cross(D1, D2) return a vector normal to D1 and D2 whose length is the sine of the angle. Putting all stuff together:

```
...
var sin: float = Vector3.Cross(D1, D2).magnitude; // calculate sine...
var cos: float = Vector3.Dot(D1, D2); // and cosine of the angle between D1,D2
// create a copy of T1 rotated 90 degrees clockwise:
var T1Right: Vector2 = Vector2(T1.y, -T1.x);
// now calculate T2 = T1 rotated by the same angle as D2:
T2 = T1 * cos + T1Right * sin;
```

This works fine if D1 and D2 are unit vectors, and T2 results the same size as T1.

NOTE: From your original question, I thought you were trying to map some point inside a mesh triangle to its equivalent in uv coordinates - if so, this may only work for plane surfaces: the mesh triangles in curved areas frequently have shapes different from the ones found in the uv map. If this is the case, you should instead interpolate the uv values according to the desired point - this article gives some hints about triangle interpolation.

Hey yeah I just tried this out and it's definitely a lot like what I want however it doesn't seem to work with all angles. The directions of D1 and D2 are arbitrary and so could be the other way round (D1 where D2 is and D2 where D1 is) I think this is where it is going wrong? Thanks a lot for helping! :-)!

You're right: I forgot that Cross(D1,D2).magnitude has no polarity - this works fine only for 180 degrees, and mirror the T2 vector in the other 180 degrees. Actually, we don't have any reference to tell us when D2 is "to the left" of D1: the plane they form flips 180 degrees when they cross each other. If there's another fixed vector D3, D1 and D3 can define a fixed plane, and the correct angle D1,D2 can be calculated.

Oh hmmm ok right I'm thinking about it with a fresh brain now and you've made me think about my problem differently with your 'NOTE'. I hadn't takn into consideration how much the shape of a triangle can change when going from mesh to uv coordinates. I think you're right about some kind of interpolation? I don't really know much about that subject so I'm going to have a look through the link you've given. Basically I have a line that goes along the plane of a triangle on my mesh (3D) and I want to translate this line to uv coordinates so I can draw a line on the triangle's texture representing the line along the triangle in 3D space :-) I understand a bit more where senad was co$$anonymous$$g from now as well

I've asked another question to help me work out my entire problem here: http://answers.unity3d.com/questions/383804/calculate-uv-coordinates-of-3d-point-on-plane-of-m.html After I've got the answer to that I'll sort out this question so it isn't left hanging. It's my first time using sites like this so I've made a little bit of a mess of it but hopefully I can sort it out :-)

**Answer** by senad
·
Jan 17, 2013 at 08:49 AM

So let us say that you have AB, which consists of the vectors/points A and B. And you have F which consists of the vectors/points F1 and F2.

What you have is A, B, At, Bt, F1 and F2. What you need to find ist F1t and F2t. Let us ignore that the t vectors are in 2D, we could just set one coordinate to zero.

Then there is a transformation matrix that will transform A -> At, B -> Bt, F1 -> F1t, F2 -> F2t. If you can derive the matrix from AB and AtBt, then you could easily transform F??

Are the tex coords chosen manually by you or an artist? Then maybe you can find a way to formalize the transformation in to the matrix??

Maybe there is a much easier way to think about it. :D

I think this is close to something I want, I thought about using matrices but I found very little information about using them with Unity and so thought quaternions were the preferred way of representing rotations. I think it would be possible to get the rotation matrix from AB to AtBt and then apply that matrix to F to get Ft??? That sounds right in my head (please correct me if I'm wrong and you have to do it with each individual point i.e A -> At) but I have absolutely no idea how to get the rotation matrix between two direction vectors in Unity and I have even less of an idea of how to apply that matrix to F in order to get an Ft. I have been looking around since you posted this but like I said I cannot find much info on Unity matrices. Thank you for your time, I think this is definitely on the right track :-)

Oh and I know the tex coords. As in they're made by the artist but I retrieve them from my mesh using $$anonymous$$esh.uv. Which gives me the uv coord of each vertex. Hope this helps :-)

On second thought, i think I am beginning to really understand your problem. :D

What you want to do to get the tex coords for F is bilinear interpolation inside your triangle. :) It might be a little hard to do in 3D space, so maybe you want to project everything to a 2D plane, to not break your brain. :D

I think it should also work, if F is outside of the triangle.

But what I don't understand is that if I projected everything to a 2D plane let's say the ground with position 0,0,0 and normal UP then if the triangle was vertically aligned and so the normal of the triangle is perpendicular to the "ground" then the projection of the two vector lines won't project very well because the lines will be perpendicular to the ground. Surely there must be some way in Unity to get the rotation matrix between two vectors and then some other way of creating a vector by rotating a currently existing vector by the rotation matrix? That way I'd have the rotation matrix $$anonymous$$ which rotated from AB to AtBt and then I could apply $$anonymous$$ to F to get Ft??

Oooo my brain just melted. Better mop that up and go back to drawing random lines on bits of paper!

**Answer** by kru
·
Jan 22, 2013 at 08:46 AM

Judging by your picture and the description, it seems that you just need the angle between D1 and D2. float alpha = Vector3.angle(D1, D2);

Then you take your T1 and rotate it by alpha. Vector2 T2 = new Vector2(T1.x * Mathf.Cos(alpha), T1.y * Mathf.Sin(alpha));

No?

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

How can I animate the surface of a 3D object? 0 Answers

Making sprites just rotate and not tilt. 3 Answers

2D eyes on 3D character? 2 Answers

How do I create a vector that always points down locally 1 Answer