2

I am making a program which auto-plays a game called bemuse. Currently, I have all of the components set up, however, I am having determining if the color block is over the area that you hit it in.

I tried using the distance formula that was provided in a thread that had a similar question, but I am consistently getting wrong results.

boolean similarTo(Color c,Color v){
        double distance = Math.sqrt((c.getRed() - v.getRed())*(c.getRed() - v.getRed()) + (c.getGreen() - v.getGreen())*(c.getGreen() - v.getGreen()) + (c.getBlue() - v.getBlue())*(c.getBlue() - v.getBlue()));
//      double average1 = c.getRed()+c.getBlue()+c.getGreen();
//      double average2 = v.getRed()+v.getBlue()+v.getGreen();

        if(distance < 100 ){
            return true;
        }else{
            return false;
        }
    }

Using this it should press when the pixel becomes similar, but the results always differ such as it saying that they are the same all the time, of never updating.

Marco R.
  • 2,667
  • 15
  • 33
MmMm SsSs
  • 87
  • 7
  • Having one complex and long line of code is hard to debug. Try breaking down the pieces into separate variables. Such as `distanceRed = c.getRed() - v.getRed();` and `sqrRed = distanceRed * distanceRed;` and `distance = Math.sqrt(sqrRed + sqrGreen + ...`. – Jason May 13 '19 at 01:41
  • You should look at this [answer](https://stackoverflow.com/a/15262443/10470233). – Yuri Chervonyi May 13 '19 at 01:43

1 Answers1

2

Your problem may be due to using the wrong Color class. You can find 2 Color classes in the JDK:

  1. java.awt.Color: Which exposes getRed, getGreen, getBlue methods returning int values between 0-255.
  2. javafx.scene.paint.Color: Which exposes getRed, getGreen, getBlue methods returning double values between 0-1.

If you are using javafx.scene.paint.Color your distance will never be greater than 1.73. You need to use java.awt.Color to have a distance between 0 - 441.67

Marco R.
  • 2,667
  • 15
  • 33