Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

object - Java Rotate Image Towards Point from any X/Y position on screen

I have a ship object that I want to be able to rotate towards any certain point I click on the screen (See pic of ship and sqaures as points). I can get the ship to face the general direction, but it has this strange behavior of as it rotates the ship itself; the image moves around an circular axis, instead of rotating from the center point of the image itself.

Can someone please, give me the formula to get the angle from the center xy point of image to the destination sqaure(mouse click)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First you have to find the difference on the x axis and the difference on the y axis. Since you seem to say that your ship is centered, you only have to get the (x,y) coordinates of the click. To find the angle, you must know that sin(your angle) = y/sqrt(y^2 + x^2), therefore, your angle = sin^-1(y/sqrt(y^2 + x^2)). It is basic maths, but it's always good to refresh basic knowledge, as it can cause quite the headache when debugging if you made a small mistake there, so I suggest you check out this page: https://www.mathsisfun.com/algebra/trig-finding-angle-right-triangle.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...