# Detecting if object is within image

**URL:** <https://discourse.processing.org/t/detecting-if-object-is-within-image/9054>\
**Category:** Coding Questions\
**Created:** [March 7, 2019, 1:13am UTC](https://discourse.processing.org/t/detecting-if-object-is-within-image/9054 "2019-03-07T01:13:22Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 7, 2019, 3:01pm UTC](https://discourse.processing.org/t/detecting-if-object-is-within-image/9054/2 "2019-03-07T15:01:41Z")

</div>

usually the collision used is by a concept, see  
[Help for create collision between the characters](https://discourse.processing.org/t/help-for-create-collision-between-the-characters/8791/2) ,  
but also possible just to check on a color change in the direction a object should go.

 ![2019-03-07_21-54-23_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/11ef7ec78d002fdde2f7cb243063828d4f218d5e.png)

example code:

```auto
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);
  rectMode(CENTER);
}

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

void virus_move() {
  go = dir.copy();
  go.mult(speed);
  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;
  rotate(ang);
  fill(200, 0, 0);
  stroke(0, 200, 0);
  rect(0, 0, 6, 6);
}

```

---

_[View the full topic](https://discourse.processing.org/t/detecting-if-object-is-within-image/9054)._
