Getting the True Bearing between 2 points. [Still Not Working]
Is your code trying to calculate the amount to rotate the line to face X? Are you trying to do some interpolation to animate the line rotating from line AB to AX? I'm still not quite following, couldn't you just create a new line (Ax, Ay), (MouseX, MouseY) every time you detect the mouse moving and draw that line to the screen?
yea, i need to work out what direction the mouse is in relation to A so that it can make B at mouse x and mouse y and draw a line from A to B
Can't you just reassign your point B to be equal to the current mouse location? I don't see why this wouldn't work, if A was your achor point and B the mouse location you'd always be drawing a line from A to the current mouse location. If not, if you're looking for an angular relationship b/w the lines AB and AX and then take the dot product b/w these two lines.
Take the direction vector of AX and dot it with the direction vector of AB,
so you'd have acos(AX/|AX|*AB/|AB|) where || is the length of the vector and * is the dot product.
The dot product is defined as |AX|*|AB|Cos(T) = AXx*ABx + AXy*ABy so to solve for T you'd divide by the length of AX and AB and take the arccos of both sides.
Take the direction vector of AX and dot it with the direction vector of AB,
so you'd have acos(AX/|AX|*AB/|AB|) where || is the length of the vector and * is the dot product.
The dot product is defined as |AX|*|AB|Cos(T) = AXx*ABx + AXy*ABy so to solve for T you'd divide by the length of AX and AB and take the arccos of both sides.
Try this. I dug it up from an old proggie of mine; it's in a BASIC dialect, but it should be easy enough to translate.
I believe this returns 0 degrees at 12 o'clock, 90 at three o clock and so on. However, rememmber that standard math puts 0 degrees at three o clock, 90 degrees at twelve o clock, 180 degrees at 9 o clock, etc...
--j_k
FUNCTION mathAngle2D% (x1 AS SINGLE, y1 AS SINGLE, x2 AS SINGLE, y2 AS SINGLE)x = x1 - x2y = (y1 - y2)IF SGN(x) = 0 THEN IF SGN(y) = 1 THEN bearing = 0 ELSEIF SGN(y) = -1 THEN bearing = 180 END IFELSE refAngle = ATN(ABS(x) / ABS(y)) / (3.1416 / 180)END IFIF SGN(x) = 1 AND SGN(y) = 1 THEN bearing = 360 - refAngleELSEIF SGN(x) = -1 AND SGN(y) = 1 THEN bearing = refAngleELSEIF SGN(x) = -1 AND SGN(y) = -1 THEN bearing = 180 - refAngleELSEIF SGN(x) = 1 AND SGN(y) = -1 THEN bearing = 180 + refAngleELSEIF SGN(y) = 0 THEN IF SGN(x) = 1 THEN bearing = 270 ELSEIF SGN(x) = -1 THEN bearing = 90 END IFEND IFmathAngle2D = bearingEND FUNCTION
I believe this returns 0 degrees at 12 o'clock, 90 at three o clock and so on. However, rememmber that standard math puts 0 degrees at three o clock, 90 degrees at twelve o clock, 180 degrees at 9 o clock, etc...
--j_k
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement