I am not sure what I am doing wrong, but I have a feeling the move() method not the best option for this. I am trying to get the actor to move to the location where I have right clicked. I used trigonometry to calculate the angle (tanX = Opposite/Adjacent) of "X". Then I rotated the actor by -X then used the move() method. For some locations the actor does not come close to the point in which it theoretically should. I'am not sure if its my code, or it's just that move() moves by whole pixels. Any suggestions? P.S. I looked at Bresenham's line algorithm, but was really confused by it.
public void act() { setImage("testPlayer.PNG"); playerMove(); playerStats(); } /** sets moving to True if the player is moving, else false */ public void setMoving(boolean move){ moving = move; } /** Returns True if the player is moving, else returns false */ public boolean getMoving(){ return moving; } /** move() the player by 1 towards the clicked location */ public void playerMove(){ mouse = Greenfoot.getMouseInfo(); if (mouse!= null){ if (mouse.getButton() == 3){ double x2,x1,y1,y2; int degreesRotate = 0; int previousRotation = 0; double base; double height; nextX = mouse.getX(); //The last clicked X position of mouse nextY = mouse.getY(); //The last clicked Y position of mouse x2= nextX; x1= getX(); y2 = nextY; y1= getY(); base = Math.abs(x1-x2); height = Math.abs(y1-y2); if (x1 != x2 && y1 != y2){ if (x1 < x2 && y1 > y2){ degreesRotate = (int)( Math.toDegrees(Math.atan(height/base)) * -1); }else if( x1 > x2 && y1 > y2){ degreesRotate = (int)(180- Math.toDegrees(Math.atan(height/base)) * -1); }else if( x1 > x2 && y1 < y2){ degreesRotate = (int)( Math.toDegrees(Math.atan(height/base)) * -1 - 180); }else if( x1 < x2 && y1 < y2){ degreesRotate = (int)( 360 - Math.toDegrees(Math.atan(height/base)) * -1); } }else if (y1!= y2){ if(y1 > y2){ degreesRotate= -90; }else{ degreesRotate = 90; } }else if(x1 != x2){ if(x1 > x2){ degreesRotate= 180; }else{ degreesRotate = 0; } } setMoving(true); setRotation(getRotation() * 0); setRotation(degreesRotate); } } if(Math.abs(getX() - nextX) < 5 && Math.abs(getY()- nextY) < 5){ setMoving(false); } if (getMoving() == true){ move(1); } } public void playerStats(){ System.out.println("\f"); System.out.println("Player X: " + getX()); System.out.println("Player Y: " + getY()); System.out.println("Next X: " + nextX); System.out.println("Next Y: " + nextY); System.out.println("Rotation: " + (360-getRotation())); System.out.println("Moving: " + getMoving()); } }