Detecting if object is within image

Greetings all,
I’m a newcomer to processing and have had fun creating various designs in the program. In an attempt to tackle something more complex, I’m currently creating a game where a mouse-controlled “parasite” object moves through a human body eating nutrients scattered throughout,
but I’m stumped on how to constrain the object’s movement to only occur within the “human body” image.
Preferably it would bounce off the inside of the image’s edges as if it was trapped inside the body.
Thanks so much in advance!!

1 Like

usually the collision used is by a concept, see
Help for create collision between the characters ,
but also possible just to check on a color change in the direction a object should go.

example code:

PImage img;
float speed = 0.3, ang, dang=0.3, clim = 200;                    // fix set color limit
PVector dir = new PVector(1, 1, 0), pos, go= new PVector(0, 0, 0);

void setup() {
  size(500, 500);
  img = loadImage("data/body.png");
  pos = new PVector(width/2, height/2, 0);

void draw() {
  image(img, 0, 0);

void virus_move() {
  go = dir.copy();
  go = go.add(pos);
  color c = img.get(int(go.x), int(go.y));              // get color on next position
  float cval = brightness(c);                           // use brightness
  if ( cval > clim )   dir = PVector.random2D();        // let it move somewhere else
  else                 pos = go.copy();                 // ok can use pos
  translate(pos.x, pos.y);
  ang += dang;
  fill(200, 0, 0);
  stroke(0, 200, 0);
  rect(0, 0, 6, 6);

1 Like