java - JavaFX - Drawn shape that follows mouse stutters when moving small distances -


i'm attempting make triangle shape drawn using graphicscontext2d follow mouse, while facing towards it.

it works nice , nifty when you're moving on 10+ pixels of distance. however, shape stutters when you're moving small precise distances , doesn't correctly face mouse.

game:

public class game extends application {      final static private string game_name = "trigon";     final static private int window_width = 960;     final static private int window_height = 640;     private playership ply;      @override     public void start(stage thestage) throws exception {         // set title of window & make root, canvas, graphics context         thestage.settitle(game_name);         group root = new group();         scene thescene = new scene(root, window_width, window_height);         canvas canvas = new canvas(window_width, window_height);         graphicscontext gc = canvas.getgraphicscontext2d();         thestage.setscene(thescene);         root.getchildren().add(canvas);          // initialize game variables         ply = new playership(window_width/2, window_height/2);          thescene.setonmousemoved(             new eventhandler<mouseevent>() {                 @override                 public void handle(mouseevent e) {                     ply.setpos(e.getx(), e.gety());                 }             }         );          new animationtimer() {             @override             public void handle(long currentnanotime) {                 gc.setfill(color.white);                 gc.fillrect(0, 0, window_width, window_height);                 ply.draw(gc);             }         }.start();          thestage.show();     }      public static void main(string[] args) {         launch(args);     }  } 

playership:

public class playership {     private double shiplength, shipwidth;     private double posx, posy, rotangle;      public playership(double posx, double posy) {         this(posx, posy);     }      public playership(double posx, double posy) {         this.posx = posx;         this.posy = posy;         this.shipwidth = 30;         this.shiplength = 30;     }      public void setpos(double posx, double posy) {         double distx = posx - this.posx;         double disty = posy - this.posy;         rotangle = math.todegrees(math.atan2(distx, -disty));         this.posx = posx;         this.posy = posy;     }      public void draw(final graphicscontext gc) {         // save         gc.save();          // translate + rotate         gc.translate(posx, posy);         gc.rotate(rotangle);          // draw ship         gc.beginpath();         gc.moveto(0, -shiplength/2);         gc.lineto(shipwidth/2, shiplength/2);         gc.lineto(-shipwidth/2, shiplength/2);         gc.lineto(0, -shiplength/2);         gc.stroke();         gc.closepath();          // restore         gc.restore();     } } 

sorry block of text or if i'm forgetting important.

this happens when origin of ship close position of mouse. small changes of mouse position can lead large changes of angle, results in shuttering effect.

you solve issue not moving ship mouse location, moving point close mouse:

public void setpos(double posx, double posy) {     double distx = posx - this.posx;     double disty = posy - this.posy;      // movement distance     double magnitude = math.sqrt(distx * distx + disty * disty);      if (magnitude > 5) {         // move, if distance greater 5          // factor move distance 5         double factor = (magnitude - 5) / magnitude;          this.posx += distx * factor;         this.posy += disty * factor;         rotangle = math.todegrees(math.atan2(distx, -disty));     } } 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -