how can I get angle of line between two pixel points?
I know there is vector3.angle… but it seems not working for 2d pixel points.
is there any funtion for it?
please help me.
Thank you
how can I get angle of line between two pixel points?
I know there is vector3.angle… but it seems not working for 2d pixel points.
is there any funtion for it?
please help me.
Thank you
Convert your vectors from Vector2 to Vector3 (just give them each a z-value of zero) and then compute the angle, easy.
NO it’s not working…
it’s just have 90 degree…
if I change Y with Z still not haveing correct value.
var fingerpoint =Vector2.zero;
var newfingerpoint=Vector3.zero;
var neworginalpoint =Vector3.zero;
var newangle=0;
function OnGUI() {
fingerpoint=Input.mousePosition;
newfingerpoint=Vector3(fingerpoint.x,fingerpoint.y,0);
neworginalpoint=Vector3(0,0,0);
newangle=Vector3.Angle(neworginalpoint,newfingerpoint);
print(newangle+" “+newfingerpoint+” "+neworginalpoint);
}
Chill out, there is zero need to use all caps.
My suggested technique is entirely valid as I’ve personally used it repeatedly, your continued problems are due to you introducing errors, not flaws in the suggested solution. Allow me to explain.
var fingerpoint =Vector2.zero;
var newfingerpoint=Vector3.zero;
var neworginalpoint =Vector3.zero;
var newangle=0;
function OnGUI() {
fingerpoint=Input.mousePosition;
newfingerpoint=Vector3(fingerpoint.x,fingerpoint.y,0);
neworginalpoint=Vector3(0,0,0);
newangle=Vector3.Angle(neworginalpoint,newfingerpoint);
print(newangle+" "+newfingerpoint+" "+neworginalpoint);
}
What exactly do you expect to get as a result when you compute the angle between some vector (newfingerpoint) and a zero vector (neworginalpoint)? In order to have an angle between two vectors you need to have two non-zero vectors otherwise the angle value returned isn’t going to make much sense anyway.
Examples:
var AVector = Vector2(5.0, 0.0);
var BVector = Vector2(0.0, 7.0);
var AOtherVector = new Vector3(AVector.x, AVector.y, 0.0);
var BOtherVector = new Vector3(BVector.x, BVector.y, 0.0);
var ABAngle = Vector3.Angle(AOtherVector, BOtherVector);
Debug.Log(ABAngle);
And the result of that? 90 degrees, as expected.
var AVector = Vector2(5.0, 5.0);
var BVector = Vector2(0.0, 2.0);
var AOtherVector = new Vector3(AVector.x, AVector.y, 0.0);
var BOtherVector = new Vector3(BVector.x, BVector.y, 0.0);
var ABAngle = Vector3.Angle(AOtherVector, BOtherVector);
Debug.Log(ABAngle);
And the result here? 45 degrees, as expected.
You can safely take any 2D vector and slap on a z value of zero and it’s all the same (your vectors both existed in the same plane before and they’ll continue to do so now). From there, as noted, computing the angle between them is trivial.
Does Vector3.Angle() give the angle between two 3d/2d points relative to x+ axis? This seems not to be the case and I am struggling to understand what I am missing.
Also…
Debug.Log(Vector3.Angle( Vector3(prvx,prvy,0), Vector3(evt.position.x,evt.position.y,0) ) );
…for pixel-to-pixel movement gives mostly 0-to-1 values! Why?
I just want to pass two pixel coordinates and get the angle of the shaped line relative to x axis.
Currently I am using: Mathf.Atan2(evt.position.y - prvy, evt.position.x - prvx) * Mathf.Rad2Deg);
which works, but I would like to understand what Vector3.Angle returns…
Thanks
I’m sorry but that question just doesn’t make sense to me. What is "the angle between to “points” relative to the x-axis? (especially when you then cite code examples in the x-y plane) Angle() gives you the angle between two vectors, done. If you have two points and you’re seeking some angle, then you have to have a point of origin in mind, get the vector positions of each point relative to that point of origin, then get the angle between those vectors and you’re done.
That’s impossible to answer without any knowledge as to what values prvx, prvy, evt.position.x and evt.position.y might have. Can you perhaps provide the actual values for those variables as well as the result of the Angle() call? Maybe it’s just due to the data being fed in more than any misbehaving by the function?
Again, that doesn’t make sense to me. Perhaps it’s Monday and I’m being daft, but you want the angle of what “shaped line” relative to the x-axis? The line defined by those two points? So you have two points, they’re in the x-y plane, those points define a line and you want the angle between that line and the x-axis? If that’s what you’re after than you are not using Angle in the right way at all above. The code above says I have to position vectors (in the x-y plane) and I want the angle between those two position vectors, there is nothing in there with respect to the x-axis in particular.
So let’s define more clearly what you want in words, then we can make sure that you’re using the right code.
Thanks for the answer. Regarding Vector3.Angle, I’ll make my study on your words and come back if necessary.
In short, what I imagine as an angle between two points is this:
step 1) take the two points and transform them until the first one sits on (0,0)
step 2) from there, rotate (in mind) the x+ sub-axis for enough degrees until it finds the second point.
These degrees is the “angle” between the two points, relative to x axis.
Then I think that you might need to stop thinking like that, it’s confusing and I think a bit off…
Angle() gives you the angle between two vectors, therefore you need to think about and explain what those two vectors are. Draw this on paper or as an image perhaps? Your whole “transform until one is at 0,0” just sounds off, what you really seem to be after is this (correct me if I’m wrong):
You have two points, there is a vector defining the delta between those two points
You have some x-axis (world based or relative to some frame of reference)
You want the angle between those two vectors?
Sorry, but what you’re saying just isn’t making sense to me so seriously, draw it out in an image if necessary.
If I understand the OP, I think that the first vector is a line that intersects two points (on the XY plane), and the second vector is the X axis itself.
I could be mistaken.
Me too as that’s where I last left off above:
So, let’s run with that for one second and show it by example:
var tPrvPosn = new Vector3(prvx, prvy, 0.0);
var tEvtPosn = new Vector3(evt.position.x, evt.position.y, 0.0);
var tVectorA = tPrvPosn - tEvtPosn;
var tVectorB = new Vector3(1.0, 0.0, 0.0);
var tAngle = Vector3.Angle(tVectorA, tVectorB);
The first vector (tVectorA) would be the position delta between the two points. The second vector (tVectorB) is the world’s x-axis. The result is the angle between those two vectors. On track?
it is quite simpe if you know the conditions clearly.
This image illustrates it (i am using x/z axis):
the code would be like:
var A:Vector3 = Vector3( 0.0, 0.0, 1.0 ) // x,y,z
var B:Vector3 = Vector3( 1.0, 0.0, 1.0 ) // x,y,z
var angle:float = Vector3.Angle( A,B );
// angle == 45
Debug.DrawLine ( Vector3.zero, A, color.yellow );
Debug.DrawLine ( Vector3.zero, B, color.yellow );
so what we do here is set the Vector A along the z-axis and the Vector B along the x/z plane.
The important thing to understand is that (if i’m not mixed up) Vector3.Angle always calculates the angle from the Vector3.zero to the actual Points.
I’m not big on math myself, so to attempt getting all 3 angles between two positions I’ve tried atan2 and also tried swapping (oh no!) around the coordinates so I could attempt using Vector3.Angle to get the appropriate angles, both to no avail. But those were merely shots in the dark, I’ve not a clue as to what I’m [entirely] doing.
Could anyone help me out to get all 3 angles between any two given 3D vectors?
The answer to your very first question is:
angle = Mathf.Atan2(pixel1.x-pixel2.x, pixel1.y-pixel2.y) * Mathf.Rad2Deg;
I’m having the same problem all day and can’t make it work in any way.
It’s hasr not to see this:
(In the XZ plane)
Zero to A = 90 degrees
Zero to B = 45 degrees
Diference between Zero-to-A and Zero-to-B = 90 - 45 = 45.
But A-to-B should be 0 degrees, the angle direction from A to B is 0 degrees…
Anyway I get the point and, it’s still not working, all I get are results of 0.0 and 90.0, no matter the values, either in Vector2 and Vector3.
I solved my problem by other means, but still didn’t get this function to work.
You need to say “The angle from 0->1 to 0->A”, “The angle from 0->1 to 0->B”, “The angle from A->B to A->B”
Angles are defined by two line (segments), not by two points. The angle from one point to another is nonsense; the angle between two directions is +sense.
Vector3.Angle( pt1 - origin, pt2-origin ) is the format you should use.
If you say:
pt1 = A
pt2 = B
origin = 0
Then you’ll have
Vector3.Angle( A - origin (that is, the line from origin to A), B - origin (line from origin to B) ) = 45.0
The concept is that Vector3.Angle takes two directions, meaning, offsets from (0,0,0), as its inputs. Sending it positions is “useless”.
To answer the question I missed about finding all 3 cardinal angles or w/e they’re called -
function CardinalAngles( pos1 : Vector3, pos2 : Vector3 ) {
// Adjust both positions to be relative to our origin point (pos1)
pos2 -= pos1;
pos1 -= pos1;
var angles : Vector3;
// Rotation to get from World +Z to pos2, rotated around World X (degrees up from Z axis)
angles.x = Vector3.Angle( Vector3.forward, pos2 - Vector3.right * pos2.x );
// Rotation to get from World +Z to pos2, rotated around World Y (degrees right? from Z axis)
angles.y = Vector3.Angle( Vector3.forward, pos2 - Vector3.up * pos2.y );
// Rotation to get from World +X to pos2, rotated around World Z (degrees up from X axis)
angles.z = Vector3.Angle( Vector3.right, pos2 - Vector3.forward * pos2.z );
return angles;
}
That ought to work. To use local space instead of world space, replace the Vector3.directions with transform.directions (up, right, forward).