Java-Gaming.org
Java4K - to go         Javadoc:
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, peek at the official java tutorials or join us at irc #jgo.
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Calculate angle between two points ??  (Read 6537 times)
0 Members and 2 Guests are viewing this topic.
Offline NewOnJava

Full Member
**

Posts: 157


1970 born Java game hobbyer.


« on: 2010-03-30 09:50:32 »

Hello!

I have the following code to calculate the angle between two 2D points ->

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public float calcAngle ( Point p1 , Point p2 )
    {
        float fx = (float)(p2.getX()-p1.getX());
        float fy = (float)(p2.getY()-p1.getY());
        float f1 = 0f;
               
        if ( fx >= 0 ) if ( fy <= 0 ) f1 = (float)-Math.toDegrees(Math.atan(fx/fy));
        if ( fx >= 0 ) if ( fy >= 0 ) f1 = 90-(float)Math.toDegrees(Math.atan(fx/fy))+90;
        if ( fx <= 0 ) if ( fy <= 0 ) f1 = 90-(float)Math.toDegrees(Math.atan(fx/fy))+270;
        if ( fx <= 0 ) if ( fy >= 0 ) f1 = 90-(float)Math.toDegrees(Math.atan(fx/fy))+90;
               
        return f1;
    }


My code is working all right, but, still i feel that it is a bit too complicated to be shown to other coders Wink
How with Java i can make my code shorter and faster ??

-----

Thanks..
Offline Bonbon-Chan

Sr. Member
**

Posts: 412
Medals: 13



« Reply #1 on: 2010-03-30 10:48:11 »

"The angle between two points" doesn't mean anything.
For the angle between two vectors (dx1,dy1) and (dx2,dy2), just use :
 Math.toDegrees(Math.atan2(dy1,dx1)-Math.atan2(dy2,dx2))

No ?
Offline Karmington

Full Member
**

Posts: 176
Medals: 1


Co-op Freak


« Reply #2 on: 2010-03-30 10:58:40 »

this is 'calculate angle of one line' in effect.

Games published by our own members! Go get 'em!
Offline Roquen

JGO Ninja
***

Posts: 578
Medals: 17



« Reply #3 on: 2010-03-30 11:53:44 »


I think this is what you want:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
  public static final float calcAngle(Point p0, Point p1)
  {
    float x0 = (float)p0.getX();
    float y0 = (float)p0.getY();
    float x1 = (float)p1.getX();
    float y1 = (float)p1.getY();
   
    // op = m sin(theta)
   // ip = m cos(theta), where m = |P0| * |P1|
   float op = x1*y0-x0*y1;
    float ip = x0*x1+y0*y1;  

    return (float)Math.toDegrees(Math.atan2(op,ip));
  }


But I'm not sure.  Your code returns 45 for: (0,1) & (1,0)  and something near 90 for (0,1) & (10000,0).  So I might be not understanding the question.
Offline dishmoth

Sr. Member
**

Posts: 494
Medals: 15



« Reply #4 on: 2010-03-30 12:27:16 »

If you're after the angle of the line that passes through points p1 and p2, then
1  
Math.toDegrees( Math.atan2( p2.getY()-p1.getY(), p2.getX()-p1.getX() ) )

should do it.

The result will be in the range -180 to +180 degrees, measured anti-clockwise from East (see the comments here on angles in polar coordinates).

Simon

Offline Eli Delventhal
« League of Dukes »

JGO Kernel
*****

Posts: 3478
Medals: 39


Game Engineer


« Reply #5 on: 2010-03-30 12:45:15 »

Yup, that's exactly what atan2 is for. You don't have to bother with checking quadrants like you've been doing.

See my work:
OTC Software
<br />
Currently Working On:
Secret project...
Quote from: _Riven
I edit JGO in production, because I simply don't waste time writing bugs
Offline NewOnJava

Full Member
**

Posts: 157


1970 born Java game hobbyer.


« Reply #6 on: 2010-03-30 17:07:46 »

If you're after the angle of the line that passes through points p1 and p2, then
1  
Math.toDegrees( Math.atan2( p2.getY()-p1.getY(), p2.getX()-p1.getX() ) )

should do it.

The result will be in the range -180 to +180 degrees, measured anti-clockwise from East (see the comments here on angles in polar coordinates).

Simon

Angle!

Ok, i get this example working on my plane game, i receive East -180 to East +180 degrees, still,
im after North +0 to North +360 degrees at on my game, is there any matematical solution with Java for North 0 to North 360 angles.

or, do i do tricks, like a i did on my first post ??

-----

Thanks..

Offline Eli Delventhal
« League of Dukes »

JGO Kernel
*****

Posts: 3478
Medals: 39


Game Engineer


« Reply #7 on: 2010-03-30 18:33:25 »

Angle!

Ok, i get this example working on my plane game, i receive East -180 to East +180 degrees, still,
im after North +0 to North +360 degrees at on my game, is there any matematical solution with Java for North 0 to North 360 angles.

or, do i do tricks, like a i did on my first post ??

-----

Thanks..


The result above makes East = 0.
If you want North to be 0, East effectively becomes -90.
That means, add 90 to the value returned.

The result above gives you a value from -180 to 180 (where 0 is east).
If you want to make it 0 to 360, effectively everything is 180 more.
That means, add 180 to the value returned.

We want to do both.

Let's convert it into an equation.

1  
2  
3  
4  
5  
6  
7  
8  
public double convertToNorth(double angle)
{
    return angle + 90;
}
public double convertTo360(double angle)
{
    return angle + 180;
}


Doing both means you've got to convert it to 360 and then make sure 0 is considered north. That means:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public double doBoth(double angle)
{
    //At this point, -180 to 180 and East is 0.

    angle = convertTo360(angle);

    //At this point, 0 to 360 and West is 0.
   
    angle = convertToNorth(angle);

    //Now, 0 to 360 and North is 0.

    return angle;
}


Make sense yet? What's the final result? That's right. Add 270.

 Roll Eyes

See my work:
OTC Software
<br />
Currently Working On:
Secret project...
Quote from: _Riven
I edit JGO in production, because I simply don't waste time writing bugs
Offline NewOnJava

Full Member
**

Posts: 157


1970 born Java game hobbyer.


« Reply #8 on: 2010-03-31 03:28:07 »

calcAngle!

Here is my final function, it is now lot shorter and maybe a lot faster ->

1  
2  
3  
4  
5  
6  
7  
8  
public float calcAngle (Point p1, Point p2 )
{
    float f1 = (float)Math.toDegrees( Math.atan2( p1.getY()-p2.getY(), p1.getX()-p2.getX() ) );
    f1+=270;
    while ( f1 > 360 ) f1-=360;
    while ( f1 <   0 ) f1+=360;
    return f1;
}


-----

Thanks..
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5509
Medals: 204


Hand over your head.


« Reply #9 on: 2010-03-31 09:06:13 »

1  
2  
3  
4  
5  
public float calcAngle (Point p1, Point p2 )
{
    float f1 = (float)Math.toDegrees( Math.atan2( p1.getY()-p2.getY(), p1.getX()-p2.getX() ) );
    return (f1+270)%360;
}

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Games published by our own members! Go get 'em!
Offline Markus_Persson

JGO Kernel
*****

Posts: 2092
Medals: 10


Mojang Specifications


« Reply #10 on: 2010-03-31 09:46:14 »

Do you need to know the angle?

I often find that it's both faster and easier to work with vectors and dot products.

Play Minecraft!
Offline Eli Delventhal
« League of Dukes »

JGO Kernel
*****

Posts: 3478
Medals: 39


Game Engineer


« Reply #11 on: 2010-03-31 12:31:35 »

Do you need to know the angle?

I often find that it's both faster and easier to work with vectors and dot products.
One time I needed the angle specifically for where I was applying a rotation to Sprite (a goal compass). Otherwise I've always used vectors.

See my work:
OTC Software
<br />
Currently Working On:
Secret project...
Quote from: _Riven
I edit JGO in production, because I simply don't waste time writing bugs
Offline imorio

JGO n00b
*

Posts: 2



« Reply #12 on: 2010-09-05 07:09:09 »

Make sense yet? What's the final result? That's right. Add 270.

I know I'm replying to an old post but this answer is just wrong I think.

The original results would give if:
north=90
west=180 or -180
south= -90
east= 0

adding 270 gives:

north=360
west=450 or 90
south=180
east=270

the only result I can think of that would be usefull is:

north=0 or 360
east or west=270
south=180
east or west=90

edit: This gives the right answer if the original answer was "polar":
int nonPolar= 90-((polar+180)%360)
if(nonPolar<0)
{
nonPolar=nonPolar+360;
}
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.163 seconds with 21 queries.