• The angle between 2 vectors.
    12 replies, posted
-snip-
360 - angle?
-snip-
What you want to do is get the difference in y value of your position and the mouse position and the difference in x value of your position and the mouse position. Then do atan2(diffy, diffx). That will return the angle in radians. You may need to include math.h for atan2.
-snip-
[b]Pseudocode:[/b] diffy = mousepos.y - yourpos.y diffx = mousepos.x - yourpos.x angle = atan2(diffy, diffx) / PI * 180 [editline]09:47AM[/editline] [QUOTE=likesoursugar;19641400] or like: float x = gun_pos.x - gun_pointing.x; float y = gun_pos.y - gun_pointing.y; float result = atan2 (y,x) ?[/QUOTE] It should be gun_pointing.x - gun_pos.x
-snip- didn't read the OP properly
-snip-
I really don't know if I'm correct and haven't confirmed this in any way but I think since you have two vectors on a two-dimensional plane and a×b = -b×a The sign of the third component (still assuming you're on an XY plane) of the vectors' cross product should indicate whether the vector b is to the left or to the right of the vector a So it could be done universally (on an XY plane again) with any two vectors rather easily
[QUOTE=ThePuska;19641541]I really don't know if I'm correct and haven't confirmed this in any way but I think since you have two vectors on a two-dimensional plane and a×b = -b×a The sign of the third component (still assuming you're on an XY plane) of the vectors' cross product should indicate whether the vector b is to the left or to the right of the vector a So it could be done universally (on an XY plane again) with any two vectors rather easily[/QUOTE] Yea, I'm fairly certain that that is correct.
-snip-
[code] double direction(vectors pov, vec) { // IN RADIANS double cw = pov.x * vec.y - vec.x * pov.y; if (cw > 0) { return Pi + arccos( dotproduct(pov, vec) / (magnitude(pov) * magnitude(vec)) ); } else { return arccos( dotproduct(pov, vec) / (magnitude(pov) * magnitude(vec)) ); } } [/code] Something like that should work, though it's most likely unoptimized for what you're doing if your other vector is always going to be (0, 1), in which case you should just use the atan2() pov being the "point of view" vector and vec being the vector whose direction you want to know
-snip-
Sorry, you need to Log In to post a reply to this thread.