Reputation: 319
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
Upvotes: 2
Views: 8762
Reputation: 161
90 degrees is equivalent to π/2. The cosine of π/2 = 0. so the balls speed in the X should not change.
The sine of π/2 = 1 so the ball speed in the Y should increase by 1.
Degrees | Radians | Value
cos(0°) = cos(0) = 1
cos(90°) = cos(π/2) = 0
cos(180°) = cos(π) = -1
cos(270°) = cost(3π/2) = 0
sin(0°) = sin(0) = 0
sin(90°) = sin(π/2) = 1
sin(180°) = sin(π) = 0
sin(270°) = sin(3π/2) = -1
Upvotes: 3
Reputation: 2049
Why not subtract 90 degrees then?
bollX += bollspeed*Math.cos(Math.toRadians(degrees-90));
bollY += bollspeed*Math.sin(Math.toRadians(degrees-90));
Upvotes: 4
Reputation: 328608
How about Math.toRadians(degrees-90)
? When using polar coordinates, 0 degree is supposed to be at 3 o'clock, not at 12.
Upvotes: 9