Hello I have a game where a spaceship flies towards a different point every time I press a button. I am able to rotate the image but I don't know how to rotate the image to face the direction it is going. How can I make the image face towards the target area. Heres my code:

public class ship {

    public boolean arrived,sel1,sel2;
    public int amountOfPop = 0;

    public int fc = -1,sc = -1,rot =5;
    private Image img;

    private Vector2 position;
    private Vector2 start;
    private Vector2 target;
    private Vector2 direction;

    private float speed ;

    private float distance;
    public boolean remove;

    public ship(Vector2 position, Vector2 target, float speed,int aop) {
        this.position = position;

        this.start = new Vector2(position.x, position.y);
        this.target = target;

        distance = Vector2.distance(start, target);
        direction = Vector2.direction(target.x - start.x, target.y - start.y);

        this.speed =speed;
        this.amountOfPop = aop;


    public void tick(){

        start.x = position.x;
        start.y = position.y;

        if(play.ds.s1.p.get(play.ds.s1.secondclicked).moon == false){
        target = new Vector2((int)  play.ds.s1.p.get( play.ds.s1.secondclicked).x + 25, (int)  play.ds.s1.p.get( play.ds.s1.secondclicked).y + 25);
            target = new Vector2((int)  play.ds.s1.p.get( play.ds.s1.secondclicked).x + 10, (int)  play.ds.s1.p.get( play.ds.s1.secondclicked).y + 10);

        distance = Vector2.distance(start, target);
        direction = Vector2.direction(target.x - start.x, target.y - start.y);

        position.x += direction.x * speed;
        position.y += direction.y * speed;

        if (Vector2.distance(start, position) >= distance) {
                remove = true;

        if(rot >= 360){
            rot = 0;


    public void render(Graphics g){

        Graphics2D g2 = (Graphics2D) g;

        g.setColor(new Color(255,127,42));
        g.drawLine((int)start.x+7, (int)start.y+7, (int)target.x, (int)target.y);

        AffineTransform at = AffineTransform.getTranslateInstance((int)position.x,(int)position.y );

        ImageIcon i121 = new ImageIcon("res/misc/circle4739.png");
        img = i121.getImage();
        g2.drawImage(img,at, null);

  • \$\begingroup\$ float directionInRadians = Math.atan2(deltaY, deltaX); is the trig you want. :) \$\endgroup\$ Commented Jul 14, 2015 at 16:11
  • \$\begingroup\$ what would delatx and delta y be \$\endgroup\$ Commented Jul 14, 2015 at 16:18
  • 1
    \$\begingroup\$ Deltax and deltay are the direction the ship is traveling. The x and y amounts you add to the ships position every frame to make it move. \$\endgroup\$
    – Alan Wolfe
    Commented Jul 14, 2015 at 16:34

2 Answers 2


Here's a quick mock up demonstration of vector mathematics in action:


Click to move.

Following on from my answer to your previous question here, and applying what @Frank and @David have explained, you can retrieve the angle of rotation by calculating the inverse tangent value of the direction:

float angle = Math.atan2(direction.y, direction.x);

This value is in Radians, which is represented as a decimal from -PI to PI. You can convert this into Degrees by using Math.toDegrees(angle);. The calculation for converting Radians to Degrees:

degrees = radians * 180 / PI;

The following answer gives the angle from horizontal. (https://stackoverflow.com/questions/7586063/how-to-calculate-the-angle-between-a-line-and-the-horizontal-axis)

(This will boil down to essentially using the same info David van Brink provided in his comment.)

You might want one that works from the current angle of the ship. Unless the rotation is instantaneous, then you only care about the target heading.

The details of mbecker's comment in the thread above give you a change in angle.

In my code I'm fix this with:

double arc = Math.atan2(mouse.y - obj.getPy(), mouse.x - obj.getPx()); 
degrees = Math.toDegrees(arc); 
if (degrees < 0) 
  degrees += 360; 
else if (degrees > 360) 
  degrees -= 360;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .