- Home /
how to calculate vector perpendicular to another and apply this to object direction?
This relates to the train problem I have. It is frustrating I know it is about vectors but I just can't get it.
So I have a simulation a cube with translation along forward direction I have a ray cast to the right (90 degrees to the direction of travel I have a rail track to the right not perpendicular to the translating box I have calculated the hit.point & hit.normal The hit.normal is being shown with the draw ray it is perpendicular to the track
To make sure the cube follows the track all I need to do is find the vector perpendicular to the hit.normal and apply this normal to the cubes transform.direction
Any help would appreciated.
Answer by duck · Apr 08, 2010 at 07:21 PM
In 3D, there are an infinite number of vectors which are perpendicular to a single Vector in 3d space. This is because the perpendicular vector could point in any direction around the axis of the first vector.
What is generally more useful is to calculate a vector which is perpendicular to two other vectors. Those first two vectors essentially define a plane, and then it is possible to have a vector perpendicular to this plane.
For example, if you wanted to find a vector which is perpendicular sideways (along the groud) from your train track direction, you would use as the two input vectors:
- The track forward direction vector
- The world "up" direction vector
In addition, there can in fact be two vectors which are perpendicular to this plane, one pointing "up" relative to the plane, and one pointing "down". (or in the case of your train track, this would either be to the left or right of your track).
To get these perpendicular vector, you need to calculate the "Cross Product" of the initial two vectors. As it happens, Unity's Vector3 Class has a built-in Cross function which saves you from having to implement this yourself.
The order in which you pass the Vectors as the arguments to Cross (i.e. which is the "left hand side" and which is the "right hand side" vector) will determine whether you end up with the "up" or "down" version of the perpendicular vector.
Ok thanks So I already have calculated a hit.normal from the train against the track section ,I need the the direction of the train to be perpendicular to this? This then becomes the trains new direction
I have a test unity project on the forum http://forum.unity3d.com/viewtopic.php?t=48762 I have had a response but I cannot figure where I add the last vector to alter train direction
so far with your help and help from stickystudios on forum I have it going round track but with a jitter see the result so far at http://forum.unity3d.com/viewtopic.php?p=296986#296986
Answer by Wyclife · Oct 31, 2011 at 11:42 AM
please help.(a) Find the equation for the plane perpendicular to the vector A=2¡+3j+6k and passingthrough terminal point of the vector B=¡+5J+3K
(b)From (a) find the distance from the origin to the plane