关于向量坐标的数学计算问题

时间:2019-05-25 14:01:51

标签: java swing vector netbeans paint

我有一个程序,该程序绘制几个球,并用其坐标移动每个球,这些球都朝着MouseMotionListener管理的鼠标方向移动。球的速度使用单位矢量随加速度增加。当我尝试使球向后移动(从当时的位置)时,它们走了一次然后沿其路径运动,并且我希望它们一直在向后向前摆动,因为它们已经到达了鼠标的坐标。

private void aumentarCoordenadas() { //this method increments coordinates for //each ball
//nB are the number of balls
//
        for(int i = 0;i < nB; i++){
            double x = coordenadasBolas[i][0];//coordinate x of current ball
            double y = coordenadasBolas[i][1];//coordinate y of current ball
            double unX = coordenadas[i][0];//x of unit
            double unY = coordenadas[i][1];//y of unit
            velX = unX * acc; //increment for acceleration 
            velY = unY * acc;//""
            Ellipse2D temp = null;
//THIS is where I try to make them stay, in case they where decrementing with //unit vector, they increment, and if they where incrementing, they decrement
            if(unX<0&&x<ratonX||unY<0&&y<ratonY)//si ha ido decrementando
                temp = new Ellipse2D.Double(x-velX,y-velY,60,60);
            if(unX>0&&x>ratonX||unY<0&&y<ratonY)//si ha ido incrementando
                temp = new Ellipse2D.Double(x-velX,y-velY,60,60);
            else
                temp = new Ellipse2D.Double(x+velX,y+velY,60,60);   
            bolas[i] = temp;
            coordenadasBolas[i][0] += velX;
            coordenadasBolas[i][1] += velY;

        }
//Just a print to make sure this part is done
        System.out.println("Coordinates done OK");
        repaint();
    }

正如我所说,它只会执行一次增/减,然后会继续遵循已经存在的路径。

0 个答案:

没有答案