- Home /
How to get axially symmetric vector3?
Hello,
i have simple question. I have some two Vectors3 ( first is pattern, second is axis) and need to get Vector3, which is axially symmetric.
We did it on high school, but i forgot it. (I only know that i had to calc normal vector from point (pattern vector) to axis and calculate distance to it and then make it "inverse") Everything was in 2D
But i am here to ask you, if is here the easier way, how to get vector in 3D.
Example:
Answer by Bunny83 · Oct 01, 2017 at 01:57 PM
You can simply use Vector3.ProjectOnPlane to get a vector that represents the displacement orthogonally from "u" to v. Just subtract this vector two times from "v" and you get "w"
Vector2 a = Vector3.ProjectOnPlane(v, u);
Vector2 w = v - 2*a;
If you want to know the raw math behind this, that's what ProjectOnPlane does:
public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal)
{
return vector - Vector3.Project(vector, planeNormal);
}
and that's what Project does:
public static Vector3 Project(Vector3 vector, Vector3 onNormal)
{
float num = Vector3.Dot(onNormal, onNormal);
if (num < Mathf.Epsilon)
return Vector3.zero;
return onNormal * Vector3.Dot(vector, onNormal) / num;
}
and Vector3.Dot is just the dot product:
public static float Dot(Vector3 lhs, Vector3 rhs)
{
return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
}
If you have further questions how it actually works, just add a comment.
Answer by ImpOfThePerverse · Oct 01, 2017 at 04:47 PM
Another way would be to use Quaternion.FromToRotation:
Quaternion halfRotation = Quaternion.FromToRotation(startVector, midpointVector);
Vector3 resultVector = halfRotation * midpointVector;
edit: Or, I guess you're looking to rotate pattern, so the code would look like:
Quaternion halfRotation = Quaternion.FromToRotation(pattern, axis);
Vector3 resultVector = hafRotation * halfRotation * pattern;
Answer by NMS_TomiBary · Oct 01, 2017 at 05:45 PM
Thank you both. While i was reading Vector3.ProjectOnPlane i found that Unity has function called Vector3.Reflect. And it solved my problem.
Again thank you very much.
Your answer
Follow this Question
Related Questions
I need help with moving an object on only the Y axis with mouse input 1 Answer
Get angle around a specified axis. 1 Answer
Problem with lerping Y axis 0 Answers
Use yAxis as a Vector3(0,0,yAxis) paramenter? 3 Answers
three second timer. 1 Answer