afra89

04-12-2010, 02:21 PM

Ok, so basically i want my character to rotate his arm and point it where the mouse is, just like teeworlds. So i thought i could use this:

A · B = |A| * |B| * cos(radians)

Like this:

http://www.mvps.org/directx/articles/math/dot/dot1.gif

(Pic from: http://www.mvps.org/directx/articles/math/dot/index.htm)

But the thing is i'm getting wierd results. It seem to work a bit on the right side of the character but not quite..

My code to get the degree is hits:

V1[0] = mouseX - x_pos; //V1 is the first vector

V1[1] = mouseY - y_pos; //x_pos is char X position

//y_pos is char Y position

V2[0] = 255 + x_pos; //V2 is the second vector

V2[1] = 0;

degreeUp = (V1[0]*V2[0]); // The upper part of the formula

degreeDown = sqrt(pow(V1[0],2)+pow(V1[1],2))*sqrt(pow(V2[0],2)+pow(V2[1],2)); //crazy math, down part

handDegree = acos (degreeUp / degreeDown); //get radians by taking acos

handDegree = (handDegree * 360) / 2 * PI; //convert to degrees

A · B = |A| * |B| * cos(radians)

Like this:

http://www.mvps.org/directx/articles/math/dot/dot1.gif

(Pic from: http://www.mvps.org/directx/articles/math/dot/index.htm)

But the thing is i'm getting wierd results. It seem to work a bit on the right side of the character but not quite..

My code to get the degree is hits:

V1[0] = mouseX - x_pos; //V1 is the first vector

V1[1] = mouseY - y_pos; //x_pos is char X position

//y_pos is char Y position

V2[0] = 255 + x_pos; //V2 is the second vector

V2[1] = 0;

degreeUp = (V1[0]*V2[0]); // The upper part of the formula

degreeDown = sqrt(pow(V1[0],2)+pow(V1[1],2))*sqrt(pow(V2[0],2)+pow(V2[1],2)); //crazy math, down part

handDegree = acos (degreeUp / degreeDown); //get radians by taking acos

handDegree = (handDegree * 360) / 2 * PI; //convert to degrees