Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with cos and sin in Android

I have a problem with my code in Android. I am using this to moving a ball. If the degree is 90 it should move to the right, if the degree is 180 it should move down etc.

This is the code I have done.

int degrees=180;
int bollspeed=4;

bollX += bollspeed*Math.cos(Math.toRadians(degrees)); 
bollY += bollspeed*Math.sin(Math.toRadians(degrees));

The problem is that when the ball should move 90 degrees it moves 180, and when it should move 180 degrees it moves 270. I have tested all I could come to think... Can anyone see any error in my code or have a expanation I would be really glad. Thanks

like image 721
bajen micke Avatar asked May 16 '26 05:05

bajen micke


2 Answers

How about Math.toRadians(degrees-90) ? When using polar coordinates, 0 degree is supposed to be at 3 o'clock, not at 12.

like image 85
assylias Avatar answered May 18 '26 19:05

assylias


Why not subtract 90 degrees then?

bollX += bollspeed*Math.cos(Math.toRadians(degrees-90)); 
bollY += bollspeed*Math.sin(Math.toRadians(degrees-90));
like image 25
ghostbust555 Avatar answered May 18 '26 18:05

ghostbust555



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!