Maineri - 9 months ago 44

Java Question

I want to make a ball move to certain direction like I give number from

0 - 360. Currently my convention is:

0 moves right, 90 down, 180 left and 270 up.

The problem is that if I put something like 10, 11, 12 etc., it doesn't move at all. Some numbers makes it move in 45 angle someway.

My current code:

`float x = 0, y = 0;`

float speed = 1f;

int angle = 90;

float moveX += speed * Math.cos(Math.toRadians(angle));

float moveY += speed * Math.sin(Math.toRadians(angle));

x += moveX;

y += moveY;

moveX = 0;

moveY = 0;

If I take off

`toRadians`

My math and English are terrible so try to explain this simple as possible. And I don't know differences between radians and degrees.

Answer Source

I don't know differences between radians and degrees etc.

This is not exactly the site for this, but here's a simple explanation. Radians and degrees are 2 measurement units (like kilometers and miles). A circle has 360 degrees, or 2π radians. That means that

```
1 degree = π/180 (0.0174533) radians
1 radian = 180/π (57.2958) degrees
```

The 4 cardinal directions on the circle using radiands would be 0 or 2π, π/2, π, 3π/2 corresponding to 0 or 360, 90, 180, 270 degrees.

I want to make ball move to certain direction like I give number from 0 - 360

Now to your code. You have a syntax error in the line

```
float moveX += speed * Math.cos(Math.toRadians(angle));
```

You just declared `moveX`

, so you don't add to it with `+=`

, you just assign to it with `=`

. Same with `moveY`

. Now if you print `x`

and `y`

after

```
x += moveX;
y += moveY;
```

you will get the right results. I don't know why after that you're setting them to 0 - you will lose the position of the ball.