Get angle between two lines (3 vectors)

Started by
11 comments, last by Andreas Ljungberg 12 years, 3 months ago
Currently trying to get an angle between two lines that are beased on the user input the problem is taht i cannot get the correct angle.

[attachment=6642:angle.png]

I have the vectors V[sub]a , [/sub]V[sub]b[/sub] and V[sub]o[/sub] (the intresect point).

I've been searching for almost a week but i cannot get the math right so now i'm asking for your help. (this includes the gamedev forums)

This is the code i've been playing around with.

private double AngleBetweenVectors(Vector2 o, Vector2 a, Vector2 b)
{
Vector2 Va = Vector2.Subtract(a, o), Vb = Vector2.Subtract(b, o); // differenses
double result = 0;
double angle;
//result = (Math.Atan2(t1.Y, t1.X) - Math.Atan2(t2.Y, t2.X)) * 180 * Math.PI;
angle = -(180/Math.PI)*Math.Atan2(Va.X*v2.Y - Vb.Y*Vb.X, Va.X*Vb.X+Va.Y*Vb.Y);
forShowingDataLong = (long)angle;
return result;
}
Advertisement
Since the dot product of two vectors a and b is equal to the product of the magnitude of the two vectors and the cosine of the angle between the two vectors, you can use that instead to get the angle you are looking for.
Okey, so i got the angle but it's not working like it's saying i get valuse from - 77 to far over a few thousend.

[attachment=6644:angle2.png]

I currently have a platform that's rotation around the center point of the game field. everything that is blue i have. This includes the platforms currently degree.

The problem is that my ball moving direction is based on two decimals so it's very specific moving 0.0231023 in X and -||- in y(for example) every time gametime updates . It woks fine untill it should collide with the platform as i cannot caculate the new specific vectors when it bounces of the platform.

And ofc. the detection works i just cannot transform the old valueas that moves the ball correctly. any tips?
You probably don't even need to do anything with angles. On collision you can just reverse the velocity vector along the normal.

So if the ball collides with something that has normal n you set

v_new = v - 2*n*dot(v,n)
Ofc i understand that is it vector mathemetics but i'm not really getting what you mean with normal. Could you help me out a bit. Atleast i'm udnerstanding it as if the normail is some kind of position. but how do i find it ? if V = the position of the ball.
With the normal I mean a normalized vector that is perpendicular to the surface the ball is impacting on. You should think of it as the "away"-direction of your platform, not a position.
I'm getting a result but it seems like it's a little to big value.


newV = v - 2 * n * Vector2.Dot(v, n);


V is the balls location(vector) , N is the normal

The lower numer in the upper left corner is what the X value of the new vector is.
The vector v has to be the velocity. (usually "x" is used for the location).
I'm getting a few interresting results, and i've implented the changes you sugessted but i'm still getting to big numbers around a few millions.. when the platform is at the top of the playing field i get 110'000, still way to big to be displayed on the screen.

I'm getting a few interresting results, and i've implented the changes you sugessted but i'm still getting to big numbers around a few millions.. when the platform is at the top of the playing field i get 110'000, still way to big to be displayed on the screen.


This would imply the math is beyond your current level. I strongly suggest you suspend that area of the project until you get a solid grasp on the math required. It looks like you need linear algebra. Consider using an existing library written (and debugged) by people who DO understand the math.


If you are determined to continue without prerequisites, verify your vectors at every step. Find out what step they explode to an unreasonable size. (Although how can you decide they are unreasonable if you don't know the math to know what reasonable is?) For example, perhaps you forgot to normalize the vectors before some operation, or after the operation. Unless you know what to expect at every level, you cannot tell if the error is in your math logic itself, or if the logic is sound and the bug is in your code.

This topic is closed to new replies.

Advertisement