java - Get Image/Transform/Shape to rotate to Follow Mouse Cursor in JavaFX -
i have tried this, @ positions, object, in case, imageview
, "teleport" degree.
how can make not choppy , follow mouse cursor in javafx scene?
i did in blog post how create heat seeking missile.
the relevant part code fragment:
public void move() { spritebase follower = this; if( target != null) { //get distance between follower , target double distancex = target.getcenterx() - follower.getcenterx(); double distancey = target.getcentery() - follower.getcentery(); //get total distance 1 number double distancetotal = math.sqrt(distancex * distancex + distancey * distancey); //calculate how move double movedistancex = this.turnrate * distancex / distancetotal; double movedistancey = this.turnrate * distancey / distancetotal; //increase current speed follower.dx += movedistancex; follower.dy += movedistancey; //get total move distance double totalmove = math.sqrt(follower.dx * follower.dx + follower.dy * follower.dy); //apply easing follower.dx = missilespeed * follower.dx/totalmove; follower.dy = missilespeed * follower.dy/totalmove; } //move follower follower.x += follower.dx; follower.y += follower.dy; //rotate follower toward target double angle = math.atan2(follower.dy, follower.dx); double degrees = math.todegrees(angle) + 90; follower.r = degrees; }
the follower object , target mouse cursor. use java 8 features of point2d calculate distance, make code less. didn't compatibility reasons java 7.
let me know if helps you. if isn't you're looking for, please more precise.
Comments
Post a Comment