简体   繁体   中英

How do I rotate my rocket image to point towards the mouse position on screen

How do I rotate my rocket image to point towards the mouse position on screen

Am working on a JavaScript game I want to rotate my rocket to point towards the position of the mouse

The answer varies a lot based on whether youre using vanilla JS or tools like Phaser, Babylon so on.

Going with direct approach, You could divide the problem and solve. But this may not be what as the question being unclear:

  • Get cursor position (cx,cy) - more details from the source here
  • Get image position (ix,iy). This can be simply retrieved by imgElement.getBoundingRect()
  • Compute the angle of rotation as arctan((cy-iy) / (cx-ix))
  • Apply this angle as css transform style for image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM