- Home /
Find the tangent or maybe the perpendicular line?
Hello everyone,
I would like to simulate a graphic perspective work in Unity. Problem is that I'm really not comfortable with Mathematics.
So basically, I want to find the 2 tangents of the sphere, where (in the picture) I use just the top and the bottom.
From the researches I've done, I know the tangent is findable I use my first point to the center of the sphere, and then use Vector3.Cross to find it's perpendicular line and then re use again. But I can't find a way to make it work properly because I don't understand all the mathematics notions included.
Can anyone explain me a bit how to reach that?
Thanks in advance!
Links visited: http://answers.unity3d.com/questions/133680/how-do-you-find-the-tangent-from-a-given-normal.html
http://answers.unity3d.com/questions/338537/great-circle-trajectories-around-a-sphere.html
"I would like to simulate a graphic perspective work in Unity. Problem is that I'm really not comfortable with $$anonymous$$athematics."
it's probably fair to say you will struggle. You will really need to learn a lot of junior school geometry and arithmetic to do this, so, treat the whole thing as an exercise in learning schoolbook geometry and arithmetic!
Enjoy !
Answer by robertbu · Mar 19, 2013 at 07:42 AM
Grab three pencils out of your pencil cup. Hold the eraser ends together and allow the points to splay out. You have two rays. Both of those rays are on a plane. If I rotate them around, the plane changes, but as long as they are pointing in separate directions, they the two rays always define a plane. Now take a third pencil place its eraser at the same position as the other two, but perpendicular to the plane defined by the first two. That is the cross product. Whether the third pencil points "up" or "down" will depend on the order you crossed the first two pencils.
From your diagram, you have one of the two "pencils" defined...the ray from the center of the sphere to the point. But that still leaves you with the need to have a second ray. It could be from the center of the sphere to the camera for example. So let's say we have the position of the camera and the position of your point as the two rays. Your code might look like.
ray1 = point1 - shpere.transform.position;
ray2 = Camera.main.transform.position - sphere.transform.position;
axisRay = Vector3.Cross(ray1, ray2);
tangentPoint1 = axisRay.normalized * sphere_radius + sphere.transform.position;
tangentPoint2 = -axisRay.normalized * sphere_radius + sphere.transform.position;;
Well, that's really near from what I wanted. First, am I correct if I say that I could have used a simple Vector3.Up ins$$anonymous$$d of the Camera.main.transform.position? Can you just explain a little bit about the .normalized ? I don't get it. Exept that, it works really good until a certain angle ( where the sphere is aprox 90°deg of the original point. Thanks for the very quick and helpfull answer!
Any vector will work as long as it is not the same vector (or facing opposite of that vector) as the one from the sphere to the point. You need to different vectors to define the plane. Vector3.normalized returns a vector where the magnitude is 1, so multiplying that by the radius gives a vector from the sphere center to the edge of the sphere. Add the position of the object, and you have a world position.
normalized just means you take a vector, and make it length one.
normalized takes a vector and gives it a magnitude of 1.
magnitude is the length of the line
basically it makes it fit into the unit circle more specifically and this should make it clearer
a normalized vector IS THE SLOPE of the vector. its how much it moves in the x y and z direction every single movement.
its not working well because you can't cross 2 identical lines.
if you imagine a 2d space the tangent is just a line 90 degrees.
however in 3d space it gets wierder.
If you place a pencil through the whole in a cd and draw a line from the pencil to the edge of the cd any line you can draw is tangent.
In 3D space it doesn't make sense to speak of the tangent of a line. Because every line has an infinite amount.
Every direction a clock hand points is always tangent to the screw holding it in.
Ins$$anonymous$$d in 3D space you speak of the tangent OF A PLAN$$anonymous$$
thats an line that is 90 degrees to the face of the plane in any direction.
A plane however cannot be define by a line.
Vector3.cross basicalyl returns the tangent of a plane. (the normal) However it takes 2 vectors only because any two non paralle vectors do in fact define a plane. It really wants 2 planes but in the same sense that you can take two points and connect them and as long as there not the same point get a line. you can take take 2 directions expand them and get a plane. so long as there not the same line. But these are at 90degrees. thats why what you get back is wierd. there are an infinite amount of answers cause your asking for the tangent to a line.
Answer by Tarlius · Mar 19, 2013 at 08:28 AM
For a distant circle, that would be a good approximation, but for a close circle, it will fall apart. If you use a perpendicular from the centre point, the tangent will be parallel to your first vector (or it won't be a tangent). The right-angle needs to be between your point, the point the tangent touches the circle and the centre.
(Assuming: A = your point; B = Perpendicular point; C = centre) Given that CBA is a right-angle, you have a right angled triangle in ABC. I assume you know how big the circle is, and you said you have the vector from the centre to the point.
From there you can use some simple trig to get the angle ACB. BCA = invCos(BC/CA) You can then multiply the vector AC by quaternions to rotate it (you'll need to do it for + and - the angle), normalise it and multiply by the radius of the circle to get your points.
Normalising makes the "distance" = 1 (ie x^2 + y^2 = 1)
Answer by Diablo404 · Mar 19, 2013 at 09:19 AM
That's a lot for an answer. Thanks to all the contributors. Of course like Fattie said, I've to learn the basics to get a better understanding of all of this. But the fact is all the answers of this questions are Unity oriented, so it will simplify the practice. Thanks!
You don't need an in-depth understanding, the libraries already do all the complex number-crunching for you. You just need a basic idea and the motivation to mess around, and pick it up as you go. Just hit/hack it until it works, like any engineer :)
yea just understand that cross means the vector of a plane.
Plane requires 2 lines not parallel
normalize is the slope of a line.
You don't need to know how to normalize just what it is.
or how to cross just the result.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094702im_/https://answers.unity.com/themes/thub/images/avi.jpg)