Jump to content
  • Advertisement
Sign in to follow this  
Andreas Ljungberg

Get angle between two lines (3 vectors)

This topic is 2528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!