Fast way to check if any object of a class is located at a point.
in
Programming Questions
•
2 years ago
I'm pretty new to java/processing and am trying to make a sort of cellular simulation. I have a bunch of white dots (pigs) that move around the screen and reproduce whenever they "eat" yellow dots (lemons). I've gotten the pigs to be pretty smart at finding lemons (they check each of 9 pixels around them to see which has the highest "lemon gravity," and move to that point.) The problem at this point is framerate. I have two functions that look through an ArrayList and check the location of every object to see if a pig is standing on a lemon or if a prospective movement space is already occupied. There must be a better way to do this! I've tried the following as a (probably silly) workaround, but the println always returns the background color and the boolean never comes up true. Any other ideas?
- boolean isLemon(float ilemonx, float ilemony)
- {
- boolean isLemon = false;
- float lemonx, lemony;
- lemonx = ilemonx;
- lemony = ilemony;
- PVector checkLoc;
- loadPixels();
- color lemonColor = get(int(lemonx), int(lemony));
- checkLoc = new PVector(lemonx, lemony);
- println(red(lemonColor));
- if(red(lemonColor) >= 254 && green(lemonColor) >= 254 && blue(lemonColor) <= 250)
- {
- isLemon = true;
- myFeeder.eat(lemonx, lemony);
- }
- return isLemon;
- }
1