java - Find an angle representing the direction of travel from origin coordinates -


i'm working on animation number of triangular objects move across screen. in order ensure each faces direction in travelling, need rotate image appropriate angle.

my problem angle code returns not accurately this. values returned not change more few degrees.

/**  * accepts 2 grid positions arguments. current position  * of object , next grid position. returns angle representing   * direction of travel current position towards next position. converting cartesian coordinates polar coordinates.   *    */ public void setangle(vector2d currentpos, vector2d nextpos ) {     double delta_x = current.xpos - next.xpos;     double delta_y = current.ypos - next.ypos;     double theta = math.atan2(delta_y, delta_x);     this.angle = theta; }      example:  || current: 1031.1438073417544 , 268.3133503758045 || next: 1033.101761841174 , 269.0819944286846 || angle: 0.0  || current: 1033.1901579769194 , 242.19363555578593 || next: 1035.1281222295695 , 243.08778242413436 || angle: 0.0  || current: 1022.1577455080815 , 255.24422527831163 || next: 1024.0301966330894 , 256.19078788718997 || angle: 0.0 

// calc deltas next minus current double delta_x = next.xpos - current.xpos; double delta_y = next.ypos - current.ypos;  // calc angle in radians using atan2 double theta = math.atan2(delta_y, delta_x);  // this.angle in degrees // or leave off *180/math.pi if want radians this.angle = theta*180/math.pi; 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -