- Home /
How to get the cross direction for 3 points?
How would I get the normal/cross from the three points marked in red? Assuming the points go in order 1, 2, 3 - Left to right.
By cross, do you mean the vector cross product? If so, it's pretty straightforward for that situation:
// We'll rename 1,2,3 to a,b,c
Vector3 a = new Vector3(-1, 0, 0);
Vector3 b = new Vector3(0, 0, 1);
Vector3 c = new Vector3(1, 0, 0);
Vector3 cross = Vector3.Cross(b-a, c-b).normalized;
Although going by your picture, I think you mean to take the (b-a) X (c-a)
, so you'd change the first term to a-b
, which will give you (0, -1, 0)
as it looks like you expect.
Answer by Bunny83 · May 09, 2018 at 11:31 AM
Since this is a 2d problem you can simply calculate the normal in 2d of each line, normalize both, combine them and normalize the result again. It might be useful to create those two extension methods for Vector3:
public static class Vector3ExtRotate
{
// rotate vector clockwise 90° around y
public static Vector3 XZ_CW (this Vector3 aVec)
{
return new Vector3(aVec.z, aVec.y, -aVec.x);
}
// rotate vector counter clockwise 90° around y
public static Vector3 XZ_CCW (this Vector3 aVec)
{
return new Vector3(-aVec.z, aVec.y, aVec.x);
}
}
Now when you have the points A, B and C you can do:
Vector3 n1 = (B-A).XZ_CCW().normalized;
Vector3 n2 = (C-B).XZ_CCW().normalized;
Vector3 n = (n1 + n2).normalized;
Note it's also possible to just do
Vector3 n1 = (A-B)..normalized;
Vector3 n2 = (C-B)..normalized;
Vector3 n = (n1 + n2).normalized;
However this would result in an invalid direction if the points A, B and C are on the same line (180° angle). My first solution only gives trouble when you have "0°". So when A and C are equal.
My solution gives you the combined normal on the "right" side of the line. If you want the "left" side just use "XZ_CW" instead of "XZ_CCW" or just invert the result ^^.
bunny -
just curious, why not this ?
Vector3 n1 = (B-A).normalized;
Vector3 n2 = (C-B).normalized;
Vector3 n = (n1 + n2).normalized. XZ_CCW();
Right, this gives the same result and is a bit shorter. Though i would rename n1 and n2 as they are not "normals" in this case. They are just directions. After combining them you actually get the tangent at point B and then we finally calculate the normal of that tangent.
Answer by Cursor9 · May 09, 2018 at 09:54 AM
if you know the Positions of the Points you could just calculate the Directions from Point 2 to the Points 1 and 3 and than calculate the cross product with 2to1 and 2to3.
Direction from Point A to B := B - A.
That's what I've been doing however the direction is on the wrong plane. Should I just use the xz values ins$$anonymous$$d? Here is my code for calculating the direction vector. var offsetDirection2 = Vector3.Cross((anchor2.position - anchor1.position).normalized, (anchor2.position - anchor3.position).normalized).normalized;
Am I doing something wrong?
The points are all level on the Y plane, however differ on the X&Z axis.
This code seems fine to me. But you dont need to normalize the direction. the result wil be the same but with less calculations. I would also change your direction claculation, you calculate diretion pointing to anchor2 ins$$anonymous$$d of from anchor2. This makes also no difference the direction of the normal will be the same, but to me it makes more sense to use direktion pointing away ins$$anonymous$$d of pointing towards anchor2.
As i sad the code seems fine. $$anonymous$$aybe the Position arent the correct ones? You could use Debug.DrawRay(...) to let the Editor draw the Normals for debugging.
Your answer
Follow this Question
Related Questions
Cannot create mesh from code,Cannot use Script to Generate 2d mesh 0 Answers
define touch area 2 Answers
Can you trigger/run code from a unspawned objcts, that isnt in the scene yet? 1 Answer
How can I access another script in my prefab to set a variable after my network player setup runs? 1 Answer
FPS Movement help 0 Answers