0

I need help with trying to make the circle in this program continuously follow my mouse's x and y coordinates.It does follow my mouse in certain points. I think this is due to the fact that i am using the slope between the two coordinates in my program. An example of the issue would be that it will go in the exact opposite direction and move away from my mouse if I am to the right of its x position. Can someone give me some guidance?

The ObjectMove class.

import java.awt.*;
import hsa.LASSConsole;

public class ObjectMove
{
    static LASSConsole c;           // The output console

    public static void main (String[] args) throws java.lang.InterruptedException
    {
        c = new LASSConsole ();

        double xcoord = c.maxx () / 2;
        double ycoord = c.maxy () / 2;
        double mouseX;
        double mouseY;
        double vely;
        double velx;
        double m;
        int d = 20;

        while (true)
        {
            mouseX = c.GetMouseX ();
            mouseY = c.GetMouseY ();

            m = ((mouseY - ycoord) / (mouseX - xcoord));

            if (((mouseX - xcoord) / -1) == (mouseX - xcoord) / (mouseX - xcoord))
            {
                velx = -1;
            }
            else
            {
                velx = 1;
            }

            if (((mouseY - ycoord) / -1) == (mouseY - ycoord) / (mouseY - ycoord) && velx == -1)
            {
                vely = -m;
            }
            else if ((((mouseY - ycoord) / -1) == (mouseY - ycoord) / (mouseY - ycoord)) && velx == 1)
            {
                vely = m;
            }
            else if ((((mouseY - ycoord) / -1) != (mouseY - ycoord) / (mouseY - ycoord)) && velx == -1)
            {
                vely = -m;
            }
            else if (mouseX - xcoord == 0)
            {
                if (mouseY > ycoord)
                {
                    vely = 1;
                    velx = 0;
                }
                else
                {
                    vely = -1;
                }
            }
            else
            {
                vely = m;

            }
            c.setColor (Color.black);
            xcoord = xcoord - velx;
            ycoord = ycoord - vely;
            c.fillOval ((int) xcoord, (int) ycoord, (int) d, (int) d);
            Thread.sleep (100);
            c.setColor (Color.white);
            c.fillOval ((int) xcoord, (int) ycoord, (int) d, (int) d);
        }



    } // main method
} // ObjectMove class
`
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
REALZZ
  • 1
  • 1
  • 4
  • I'm not sure if I get what you're asking exactly but if you need the exact coordinates of the mouse and apply them to the x and y of your circle object here's a good answer : http://stackoverflow.com/questions/1439022/get-mouse-position – Yassin Hajaj Sep 20 '15 at 19:21
  • The problem is that in order for the object to follow my mouse, similar to Agar.io , It needs to use the utilize both sets of coordinates. I am having trouble with that. My program already determines the coordinates. – REALZZ Sep 20 '15 at 19:37

0 Answers0