Jump to content
  • Advertisement
Sign in to follow this  
Emexus

rotating a image from a given mouse coordinate

This topic is 5083 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Im playing about with java, and doing some simple sprite manipulation, but my math is not very good and so i was wondering if someone could explain to me how i would rotate and image to face the direction of where the mouse pointer is, the method call to rotate the image is easy enough but im unsure how i would work out how many degree it would need to turn, in order to face the mouse, hope im making sense? cheers

Share this post


Link to post
Share on other sites
Advertisement
this is simple trigonometry.

public void mouseMoved(MouseEvent e){
int mx=e.getX();
int my=e.getY();
int cx=imageX-imageWidth/2;
int cy=imageY-imageHeight/2;
int dx=mx-cx;
int dy=my-cy;
double angle=Math.atan2((double)dy,(double)dx);
Graphics2D g=(Graphics2D) YourFrame.getGraphics();
g.rotate(angle);
g.drawImage(image, imageX, imageY, null);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!