D
D
Dmitry Smerks2018-11-14 07:19:41
Java
Dmitry Smerks, 2018-11-14 07:19:41

How to set the direction angle of an object in Android Studio?

There is a ball, the user clicks on the screen, and I need to smoothly, through the animation, move it. There is a separate thread for this, which calls the function of redrawing and updating the coordinates.
Question: what should the values ​​of Vx and Vy be equal to in order for the ball to fly smoothly. The redraw is called every 10ms.

method
public void run() {       //Этот метод выполняется в побочном потоке        
            do {
                draw.postInvalidate();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
            } while (MyDraw.Gameisrunning);
        }
    }

public class MyDraw extends View {
    static public int PlayerX;
    static int PlayerY;
    int PlayerR;
    float EndLx =0,EndLy=0; // В эти значения кидаем нажатие пользователя
    static float Vy = 0, Vx = 0;  // Эти значения плюсуются каждые 50 МС, в их определение собственно и есть проблема
    protected boolean fl = true;
    public static boolean Gameisrunning = true;

protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int OnePercentHeight = canvas.getHeight() / 100; // create percents value
        int OnePercentWidth = canvas.getWidth() / 100;
        canvas.drawColor(getResources().getColor(R.color.BackGroundColor)); // drawing backgroung
        Paint PaintPlayer = new Paint(); //Settings for ball of a player
        Paint PaintMainFrame = new Paint();             //settings for form rectangle
        Paint PaintWallGreen = new Paint(); //Settings for green walls
        PaintMainFrame.setColor(Color.GREEN);
        PaintMainFrame.setStyle(Paint.Style.STROKE);
        PaintMainFrame.setStrokeWidth(OnePercentWidth);
        PaintWallGreen.setColor(Color.GREEN);
        PaintWallGreen.setStyle(Paint.Style.FILL);
        PaintPlayer.setColor(Color.WHITE);
        PaintPlayer.setStyle(Paint.Style.FILL);
        frame MainFrame = new frame(0 + OnePercentWidth / 2, 0 + OnePercentWidth / 2, canvas.getWidth() - OnePercentWidth / 2, getHeight() - OnePercentWidth / 2, PaintMainFrame);
        Wall RightWall = new Wall(OnePercentWidth * 40, canvas.getHeight() - OnePercentHeight, OnePercentWidth * 43, OnePercentHeight * 60, PaintWallGreen);
        if (fl) {
            PlayerX = OnePercentWidth * 10;
            PlayerY = canvas.getHeight() - (OnePercentHeight * 8 + OnePercentWidth * 2);
            PlayerR = OnePercentWidth * 4;
            canvas.drawRect(RightWall.getX1(), RightWall.getY1(), RightWall.getX2(), RightWall.getY2(), RightWall.getPaint()); // drawing wall
            canvas.drawRect(MainFrame.getX1(), MainFrame.getY1(), MainFrame.getX2(), MainFrame.getY2(), MainFrame.getPaint()); // drawing form
            canvas.drawCircle(PlayerX, PlayerY, PlayerR, PaintPlayer); // drawing player
            Paint PaintLine = new Paint();
            PaintLine.setColor(Color.RED);
            PaintLine.setStrokeWidth(5);
            canvas.drawLine(PlayerX,PlayerY,EndLx,EndLy,PaintLine); //Пользователь нажатием выбирает координаты, ему помогает линия
        }
 else {
            update();
            Player player = new Player(PlayerX, PlayerY, PlayerR, PaintPlayer);
            canvas.drawCircle(PlayerX, PlayerY, PlayerR, PaintPlayer); // drawing player
            if (((player.getY1() <= MainFrame.getY1() + OnePercentWidth * 4) || (player.getY1() >= MainFrame.getY2() - OnePercentWidth * 4) || (player.getY1() + 50 >= RightWall.getY2()) && (player.getY1() + 60 <= RightWall.getY2()) && (player.getX1() - 50 < RightWall.getX2()))) {
                ChangeY();
            }

            if ((player.getX1() <= MainFrame.getX1() + OnePercentWidth * 4) || (player.getX1() >= MainFrame.getX2() - OnePercentWidth * 4) || ((player.getX1() - PlayerR < RightWall.getX2()) && (player.getX1() + PlayerR > RightWall.getX1()) && (player.getY1() + PlayerR > RightWall.getY2()))) {
                ChangeX();
            }

 canvas.drawRect(RightWall.getX1(), RightWall.getY1(), RightWall.getX2(), RightWall.getY2(), RightWall.getPaint()); // drawing wall
  canvas.drawRect(MainFrame.getX1(), MainFrame.getY1(), MainFrame.getX2(), MainFrame.getY2(), MainFrame.getPaint()); // drawing form
            canvas.drawCircle(PlayerX, PlayerY, PlayerR, PaintPlayer);


The function of a shot along the selected trajectory of the user, where it is not possible to determine the coordinates corresponding to the desired angle.
Shot function
if (eventAction == MotionEvent.ACTION_DOWN) {
            if ((event.getX() < PlayerX + 50) && (event.getX() > PlayerX - 50) && (event.getY() < PlayerY + PlayerR) && (event.getY() > PlayerY - PlayerR)) {
                fl = false;
                Vy = ???;
                Vx = ???;
            }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question