Java: a moving ball with an angle?

I began to study the physics of the game, and I am trying to move the ball at an angle. But he does not change his angle. The Java coordinate system is a little different, and I think my problem is there. Here is my code.

This is for calculating the speed of x and y:

 scale_X= Math.sin(angle);
 scale_Y=Math.cos(angle);
     velosity_X=(speed*scale_X);
 velosity_Y=(speed*scale_Y);

This is for moving the ball in the run () function:

  ball.posX =ball.posX+(int)velosity_X;
  ball.posY=ball.posY+(int)velosity_Y;

I used (int)velosity_Xand (int)velosity_Ybecause in the class ballI draw an object

g.drawOval(posX, posX, width, height);

and g.drawOvalrequired here int. I don’t know if the problem is or not. Also, if I use angle 30, it goes + X and + Y, but if I use angle 35, then it goes -X and -Y. I did not understand how to work with the coordinate system in Java.

+3
source share
3 answers

Math.sin() Math.cos() . ( * Math.PI/180).

+7

Math # toRadians()

scale_X = Math.sin(Math.toRadians(angle));
scale_Y = Math.cos(Math.toRadians(angle));
+2

. , , , , . ints, , 1 0 . , , , ( ), .

- ( ) int ( ). , , , - .

+2
source

Source: https://habr.com/ru/post/1743418/


All Articles