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 4625436
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:16:19+00:00 2026-05-22T03:16:19+00:00

I want to rotate an Image in java, initialized with playerimage = Toolkit.getDefaultToolkit().getImage(C:/Game/player.png); //Load

  • 0

I want to rotate an Image in java, initialized with

            playerimage = Toolkit.getDefaultToolkit().getImage("C:/Game/player.png"); //Load Player Image

drawed with:

        Graphics2D g = buffer.createGraphics();
    g.drawImage(playerimage, player.getX(), player.getY(), null); // Draw Player

Now, I want my playerimage, to rotate to my mouse, so it’s basically looking to my mouse. How would I do this?

  • 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-22T03:16:20+00:00Added an answer on May 22, 2026 at 3:16 am

    The first thing you’ll need to do is get both the mouse pos and the player pos in the same coord system. When you get our mouse pos it will normally be in Screen Coords while your player maybe in your own ‘World Coords’ space. If the players position is tied directly to pixels then you can skip ahead.

    Convert the Mouse Position to World coords..

    mouseWorld.X = (mouseScreen.X / screenWidth) * worldWidth;
    

    Once you are in the same coord system you need to find the angle required to rotate. This equation will change based on which way your Player art is facing, lets assume it is facing up the position X axis. Then you can just use the dot product to find the angle between where your player is facing and where the point is pointing.

    The dot product is A(dot)B = mag(A) * mag(B) * cos (theta) 
    mag = magnitude of the vector
    theta = angle between the two vectors
    So if you normalize the vectors (make them length 1) then..
    A(dot)B = 1 * 1 * cos(theta)
    A(dot)B = cos(theta)
    acos(A(dot)B) = theta
    

    So lets do code…

    Vector mouseVec(mouseWorld.X, mouseWorld.Y);
    Vector playerVec(playerWorld.X, playerWorld.Y);
    
    //You want to find the angle the player must turn, so pretend the player pos it the origin
    mouseVec -= playerVec;
    
    //Create a vector that represents which way your player art is facing
    Vector facingVec(1, 0);
    
    mouseVec.Normalize();  //Make their length 1
    facingVec.Normalize();
    
    double dotProd = mouseVec.dot(facing);
    double angBetween = acos(dotProd);
    

    then call ‘rotate’ and pass in the angBetween!

    Double check that the units are correct, often acos will return ‘radians’ and the rotate function will take ‘degrees’ so you need to convert.

    More vector info:
    Vector Info

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

Sidebar

Related Questions

I want to rotate an image continuously on page load. Code works fine for
I want to rotate a image with UIslider control. I have done that with
I want to be able to rotate, zoom and move an image in android
I want to Rotate Image according to a specific angle in android ,some thing
I want to rotate the image in clockwise or anticlockwise direction corresponding to the
I want to rotate an image using ImageView.setImageMatrix(matrix) but it simply doesn't have any
I want to rotate the image around its center point.The problem i am facing
hai i want to rotate an image back and forth. But now it is
I want to rotate the below image on a center point using one finger
I want to rotate an image, but i seem to be unable to maintain

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.