• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

Angle Between Points

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
Hello. I got a little math problem here. I am in need of detecting an angle between points in C#. And since I havnt learned the math I have to take the math from somewhere.

Atm I want to convert the BJ AngleBetweenPoints to C# code. But there is an small error.
JASS:
return bj_RADTODEG * Atan2(GetLocationY(locB) - GetLocationY(locA), GetLocationX(locB) - GetLocationX(locA))

here is a " , " I dont know what that means. shouldnt it be + or something?

EDIt: new problem/issue, info in this post
http://www.hiveworkshop.com/forums/...e-98/angle-between-points-244493/#post2447414
 
Last edited:
Level 12
Joined
Mar 24, 2011
Messages
1,082
It should be " / "

I am studying survey and I use this formula almost every day.

The formula is: Y1-Y2= dY (d = difference, normaly it is whritten with greek delta) X1-X2= dX
Atan ( dY / dX ) = Angle

The whole formula is: Atan [ (Y1-Y2) / (X1-X2) ]

Hope I was helpfull.

PS: Not sure if it is like this in computer science

Edit// Started whriting before TriggerHappys post. Better refer to his post.
 
Last edited:

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
I tried it just now and it doesnt work. Or well it does work but it doesnt calculate as it should.

Note that this is just a test not a serious line so variable names are not supposed to be normal :p
JASS:
angle = (int)(Math.PI / 180) * (int)Math.Atan2(Move.position.Y - Chest.chests[0].minY, Move.position.X - Chest.chests[0].minX);

this gives the value 0
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
if that was the case it would resault in doing in the opposite direction? that is not the case. it still says the angle is around 0 even tho I move around ingame.

edit: ok something is really wrong here o_O
I put into some really odd number and it still gives 0 back..
JASS:
angle = (int)(Math.PI / 180) * (int)Math.Atan2(321321 - 2 , 12222 - 213);
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
This is what i tried and here are the results.

Code:
        public static double DegToRad(double d)
        {
            return d * (Math.PI / 180.0);
        }

        public static double AngleBetweenPoints(double x1, double y1, double x2, double y2)
        {
            return RadToDeg(Math.Atan2(y2 - y1, x2 - x1)); //321321 - 2, 12222 - 213));
        }

Answer = 0.0267635706007019

And the other way.

Code:
        public static double RadToDeg(double d)
        {
            return d * (180.0 / Math.PI);
        }

        public static double AngleBetweenPoints(double x1, double y1, double x2, double y2)
        {
            return RadToDeg(Math.Atan2(y2 - y1, x2 - x1)); //321321 - 2, 12222 - 213));
        }

Answer = 87.8596195169717

I think you want the second answer.
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
In both of them Math.Atan2 returns a radian.

You are trying to read degrees.

So to turn rad to degrees you do this.

Code:
        public static double RadToDeg(double d)
        {
            return d * (180.0 / Math.PI);
        }

180 / Math.PI;

You were basically trying to turn Rad to Rad with the (Math.PI / 180.0)

In the method it could have can be written like this.

Code:
        public static double RadToDeg(double d) // This returns degrees from radians.
        {
            return d * (180.0 / Math.PI);
        }

        public static double AngleBetweenPointsDegrees(double x1, double y1, double x2, double y2) // This returns degrees by converting the answer to degrees.
        {
            return RadToDeg(Math.Atan2(y2 - y1, x2 - x1)); //321321 - 2, 12222 - 213));
        }

        public static double AngleBetweenPointsRadians(double x1, double y1, double x2, double y2) // This returns radians.
        {
            return AngleBetweenPoints(double x1, double y1, double x2, double y2); 
        }

I dont even see the differance lol

edit: my bad.

edit2: its still bugging a little tho.

Can you show the whole code maybe there is another problem.

What i posted above shouldn't have a problem but it is easy to forget that the system handles almost everything in radians when we are more likely to use degrees.
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
to update this thread.

I got a new problem now.

The angle wont work as I want it to. When my character in my game moves right the angle seems to think I moved left. Its really odd.
JASS:
angle = (180 / Math.PI) * Math.Atan2(Move.position.Y - 200, Move.position.X - 500);                
Enemy_Shot tempshot = new Enemy_Shot(500, 200, 200 * Math.Cos(angle * Math.PI / 180) * 0.1f, 500 * Math.Sin(angle * Math.PI / 180) * 0.1f, Game1.bossmissile);
Enemy_Shot.shotlist.Add(tempshot);
 
Last edited by a moderator:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Something at 0,0 in display coordinates is at the top left of the display. Something at 1,1 (assuming normalized float position) is at the bottom right. This means that increasing y actually decreases the height on the display. If you mess up trying to map this space to actual map coordinates it is very possible that you get the entire game flipped. Legend of Zelda Twilight Princess Wii version did such a thing where it did a fliplr on the geometry so that link was right handed.

What I am trying to say is make sure that increasing X actually means going right on the screen and not left.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
I am sorry, the returned values for your case (as specified by the documentation...).
Right = 0
Up = -90
Left = 180
Down = 90


It returns values in the range of [pi >= x > -pi] with a few special cases.

The fact none of the angels you are getting are negative shows you are not reading them properly. Unless you specifically convert the angel into 0 to 360 space (which you have not stated anywhere) then you are just making up angels.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Yeh so this proves (if you tested using constant angles like your program shows?) the problem is with "Enemy_Shot" or its creation and not with the angle returned using Atan2. It is returning the right angle, just you are mapping it back incorrectly. What I said about the values from Atan2 is still perfectly valid, you never tested it.

I am guessing you have x and y swapped when using Cos/Sin to convert back. As you know, Cos(x) = Sin(x + pi/2) (offset by 90 degrees, ring any bells?). With out knowing how a "Enemy_Shot" works it is impossible to tell.
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
You were right, swapping sin and cos with eachother now gives the right angles(0 = right etc).

however this is still not working as it should. Now my shots follow me in the right direction but it does not really target me perfectly. (will upload a few images, soon).


2nhnpz9.png

2hchidc.png

2q20ltj.png

 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Check that shots are moving in an expected way. A shot fired right should keep going right and not end up drifting up or down.

If they are moving in an expected way, then you need to look into the mathematics that determines the angle they are fired at. Maybe their target position does not represent your character's visual position. Or maybe you are adding incorrect offsets to it.

If the units you are using are very small or very large there is a possibility that such an error is the result of floating point error.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
It would really help if you posted your code instead of leaving us to guess...

Looking at...
angle = 0;
Enemy_Shot tempshot = new Enemy_Shot(500, 200, 200 * Math.Cos(angle * Math.PI / 180) * 0.1f, 500 * Math.Sin(angle * Math.PI / 180) * 0.1f, Game1.bossmissile);
Enemy_Shot.shotlist.Add(tempshot);
The reason is because you skew the missile towards an axis with some kind of distortion.

Try using the same magnitude value for both the X and Y coordinates.

Basically use...
Enemy_Shot tempshot = new Enemy_Shot(500, 200, 20.0f * Math.Cos(angle * Math.PI / 180), 20.0f * Math.Sin(angle * Math.PI / 180), Game1.bossmissile);
Remembering to swap whatever fixed the last problem as you did not post the fixed code.
 
Status
Not open for further replies.
Top