Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:26:33+00:00 2026-05-31T00:26:33+00:00

Im am trying to learn some more about vectors and rotation so I can

  • 0

Im am trying to learn some more about vectors and rotation so I can try out making a top down shooter. Right now I am trying to make an image point at the mouse cursor. It works on only one half of the image because I can only get the angle to be 180 before it decreases. how can I make the angle increase past 180 so it will follow the mouse the whole way round? If my question is a bit confusing just run the code and you will see my issue.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.vecmath.Vector2d;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test extends JPanel implements Runnable{
    BufferedImage img;
    double angle;
    int imgw;
    int imgh;
    JFrame f;
    PointerInfo cursor = MouseInfo.getPointerInfo();
    Point point = new Point(cursor.getLocation());
    AffineTransform at = new AffineTransform();

    public Test(JFrame f){
        this.f = f;
        setSize(400, 400);

        try {
            img = ImageIO.read(new File("res/rocket.png"));
        } catch (IOException e) {}

        imgw = img.getWidth();
        imgh = img.getHeight();

        Thread t = new Thread(this);
        t.start();
    }

    public static void main(String[] Args){
        JFrame frame = new JFrame();

        frame.add(new Test(frame));
        frame.setVisible(true);
        frame.setSize(400,400);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D)g;
        g2.drawImage(img,at,this);
    }

    public void run(){
        while(true){
            cursor = MouseInfo.getPointerInfo();
            point = new Point(cursor.getLocation());
            SwingUtilities.convertPointFromScreen(point, f);

            Vector2d mouse = new Vector2d(point.getX(),point.getY());
            Vector2d rocket = new Vector2d(100,100);
            Vector2d facing = new Vector2d(0, -1);

            mouse.sub(rocket);
            mouse.normalize();
            facing.normalize();

            angle = mouse.angle(facing);
            System.out.println(Math.toDegrees(angle));
            at.setToTranslation(100,100); 
            at.rotate(angle, imgw/2, imgh/2); 
            repaint();

            try {Thread.sleep(25);
            } catch (InterruptedException e) {}
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:26:34+00:00Added an answer on May 31, 2026 at 12:26 am

    The angle method of the Vector2d says that the return value is constrained to the range [0,PI]. With a little hack, I was able to get what you are looking for, by adjusting the angle if the pointer is left to the rocket

    if(point.getX()<100){
                angle=(Math.PI*2-angle);
    }
    

    [EDIT] Or better, calculating the angle by the Math way simplifies it all

    Point procket=new Point(100,100);
    angle=(Math.atan2( point.getY()-procket.getY(), point.getX()-procket.getX())+(Math.PI/2));
    

    (point is your original mouse pointer point and the extra PI/2 for adjusting the axis of reference)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn some more about vectors and rotation in Java by
I am trying to learn more about the DOM and have been writing some
I am currently trying to learn some more about web design and I used
I'm trying to dissect some well designed sites, in order to learn more about
In my bid to try and learn some more about WPF I've been looking
Just trying to ping up some experienced Thread gurus out there...trying to learn more
I was trying to learn some more about the inner workings of vtables and
Im currently trying to learn some stuff about encryption, it's algorithms and how it
I'm trying to learn more about database transactions, I found the ACID rule of
I'm trying to learn more about 'ajax' and so far have had the most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.