<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with #collisions - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=%23collisions</link>
      <pubDate>Sun, 08 Aug 2021 14:50:10 +0000</pubDate>
         <description>Tagged with #collisions - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/tagged%23collisions/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>Collision between object moved by keys and random object</title>
      <link>https://forum.processing.org/two/discussion/28106/collision-between-object-moved-by-keys-and-random-object</link>
      <pubDate>Tue, 28 Aug 2018 01:15:35 +0000</pubDate>
      <dc:creator>rmar15</dc:creator>
      <guid isPermaLink="false">28106@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I'm having problems with this collision thingy between the 2 rectangles, I know I'm supposed to use dist(), It worked when my object was controlled by mouse, but I can't make it work now with keys, any help would be appreciated! :)</p>

<pre><code>Ship myShip;
int a=1;
int b=100;
int c=200;
int d=300;
int e=400;
int f=500;
int z=10;
int j=160;
boolean [] keys = new boolean[128];


void setup() {
  size(500, 600); 
  smooth();
  myShip = new Ship();
}
void gameOver() {
    textAlign(CENTER);
    text("GAME OVER", width / 2, height / 2);

    }

void draw() {
  background(0);
  a=a+3;
  b=b+3;
  c=c+3;
  d=d+3;
  e=e+3;
  f=f+3;
  z=z+4;
  rect(j,z+3,25,25);
  rect(270,a+3,5,30);
  rect(270,b,5,30);
  rect(270,c,5,30);
  rect(270,d,5,30);
  rect(270,e,5,30);
  rect(270,f,5,30);
  rect(80,0,5,1500);
  if (a&gt;=600)
  a=-10;
  if (b&gt;=600)
  b=-10;
  if (c&gt;=600)
  c=-10;
  if (d&gt;=600)
  d=-10;
  if (e&gt;=600)
  e=-10;
  if (f&gt;=600)
  f=-10;
  myShip.run();
}

void keyPressed() {
  keys[key] = true;
}

void keyReleased() {
  keys[key] = false;
}


class Ship {  
  PVector pos, vel;
  float rotation;


  Ship() {
    pos = new PVector(160, height/2);
    vel = new PVector(5, 5);
    rotation = 0;
  }

  void run() {    //generic function container
    display();
    move();
  }


  void display() {
    pos.x=constrain(pos.x, 25, width);
    pos.y=constrain(pos.y, 25, height);
    pushMatrix();
    translate(pos.x, pos.y);
    rect(0, 0, 50, 50);
    popMatrix();
   if (pos.x&gt;=260) 
  noLoop();
  if (pos.x&lt;=60) 
  noLoop();
  if (pos.x&gt;=260)
  gameOver();
    if (pos.x&lt;=60)
  gameOver();
  if (dist(pos.x,pos.y,50,a+1)&lt;25)
  noLoop();


  }


  void move() {
    if (keys['a']) //move left 
      pos.x -= vel.x;
    if (keys['d']) //move right
      pos.x += vel.x;
    if (keys['w']) //move up
      pos.y -= vel.y;
    if (keys['s']) //move down
      pos.y += vel.y;

  }

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How to connect two or more things in one array</title>
      <link>https://forum.processing.org/two/discussion/28081/how-to-connect-two-or-more-things-in-one-array</link>
      <pubDate>Sun, 22 Jul 2018 23:53:44 +0000</pubDate>
      <dc:creator>Tymekk123</dc:creator>
      <guid isPermaLink="false">28081@/two/discussions</guid>
      <description><![CDATA[<p>Hello,
Im trying to change colors of two (or more) cirlces when their centers are in range of each others radiuses. Something like that:
<img src="https://forum.processing.org/two/uploads/imageupload/581/PB5448623JLQ.png" alt="Bez tytułu" title="Bez tytułu" /></p>

<p>I tried double for loop, but I failed.</p>

<pre><code>Postac[] p;

void setup() {
  size(600, 600);
  p = new Postac[5];

  for (int i = 0; i &lt; p.length; i++) {
    p[i] = new Postac();
  }
}

void draw() {
  background(0);

  for (int j, i = 0; i &lt; p.length; i++) {
    p[i].display();
    p[i].mouseOver();
  }
}

class Postac {

int x = int(random(50, width-50)), y = int(random(50, height-50)), 
  radius = int(random(50, 200));
int r = 250, g = 3, b = 255;
boolean over;

  void display() {
    strokeWeight(2);
    stroke(255);
    point(x, y);

    strokeWeight(1);
    stroke(r, g, b, 100);
    fill(r, g, b, 50);
    ellipse(x, y, radius, radius);
  }

  void mouseOver() {

    if (mouseX &gt; x-3 &amp;&amp; mouseX &lt; x+3 &amp;&amp; mouseY &gt; y-3 &amp;&amp; mouseY &lt; y+3) {
      r = 255;
      g = 255;
      b = 255;
      over = true;
    } else {
      r = 250;
      g = 3;
      b = 255;
      over = false;
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Bullet runs into issue when collided with more then one enemy</title>
      <link>https://forum.processing.org/two/discussion/27928/bullet-runs-into-issue-when-collided-with-more-then-one-enemy</link>
      <pubDate>Mon, 07 May 2018 00:59:21 +0000</pubDate>
      <dc:creator>ctrembla</dc:creator>
      <guid isPermaLink="false">27928@/two/discussions</guid>
      <description><![CDATA[<p>Hey, I'm making a space invaders game &amp; I made a bunch of different kind of bullets the player can use, but when a bullet collides with 2 enemies it gives me a bullet remove error.
<code>for (int i = Bullet.size()-1; i &gt;= 0; i--)
{ bullet bu = Bullet.get(i); 
bu.show(); 
bu.move(); 
for (int j = Enemy.size()-1; j &gt;= 0; j--)
{ enemy en = Enemy.get(j); 
float d = dist(bu.x,bu.y,en.x,en.y); 
if (d &lt; bu.r + en.r){ 
Bullet.remove(i); 
en.life -= 1; } } }</code></p>

<p>anyone know a way to fix it so my bullet ca hit 2 enemies without an error. I think the trouble is that both enemies it collided with are trying to remove it at the same time.</p>
]]></description>
   </item>
   <item>
      <title>Per-pixel collision with arrays</title>
      <link>https://forum.processing.org/two/discussion/27918/per-pixel-collision-with-arrays</link>
      <pubDate>Fri, 04 May 2018 20:15:49 +0000</pubDate>
      <dc:creator>Elliot12345</dc:creator>
      <guid isPermaLink="false">27918@/two/discussions</guid>
      <description><![CDATA[<p>I'm fairly new to processing but I've been progressing well with learning how to use it. I've been working on a simple flappy bird esque game and what I want to do is to have the bird and "pipes" collisions be recognized on a pixel-level, ive seen Peter Lagers code for pp_collision but i can't seem to get it to work for the obstacles. can someone tell me how to incorporate the classes into pp_collision(PImage imgA, float aix, float aiy, PImage imgB, float bix, float biy)</p>

<p>for souls <a href="https://i.imgur.com/PZm7ivN.png" target="_blank" rel="nofollow">https://i.imgur.com/PZm7ivN.png</a>  and   <a href="https://i.imgur.com/wvOkeEZ.png" target="_blank" rel="nofollow">https://i.imgur.com/wvOkeEZ.png</a>   . I would love to know how to get collisions on a pixel level for animated sprites (ie. the souls array) as well but its secondary which is why i commented its code out.</p>

<pre><code>//int numFrames = 2;  // The number of frames in the animation
//int currentFrame = 0;
//PImage[] souls = new PImage[numFrames];


int gameState; //0 = startscreen, 1 = in-game, 2 = game over, 3 = restart-screen
int o = 240;


Ghost ghost;
Obstacle[] obstacles = new Obstacle[2];
Score score;
// Game states
boolean gameStarted = false;
boolean gameOver = false;
boolean gameRestart = false;


void setup() {
  //frameRate(30);
  fullScreen(FX2D);
  //size(1280, 720, FX2D);

  ghost = new Ghost(width/2, height/2);
  obstacles[0] = new Obstacle(width, random(100, height-100));
  obstacles[1] = new Obstacle(width*1.5+25, random(100, height-100));
  score = new Score();

  //startTimer = new Timer (0);

  //for (int k = 0; k &lt; numFrames; k++) {
   // String imageName = "Soul_" + nf(k, 2) + ".png";
   // souls[k] = loadImage(imageName);
  //}
}


void draw() {
  background(175); 

  if (gameState==0) {
    gameRestart = true;
    drawMenuScreen();
    score.highscores();
    //startTimer.countUp();
    //fill (255);
    //text (startTimer.getTime(), 60, 60);
  } 
  if (gameState==0 &amp;&amp; mousePressed) {
    if (gameRestart == true)
      //timerReset();
    score.reset();
    gameState = 1;
  }


  if (gameState==1) { 
    gameStarted = true;
    gameRestart = false;
    ghost.draw();
    for (Obstacle o : obstacles) { 
      o.draw();
      //Souls();
    }
    score.draw();
    detectCollision();

    if (gameState==1 &amp;&amp; mousePressed) {
      //startTimer.countUp();
      //fill (255);
      //text (startTimer.getTime(), 60, 60);
    }
    if (gameState==1 &amp;&amp; mousePressed) {
      ghost.jump();
    }
  }


  if (gameState==2) {
    //startTimer.countUp();
    //fill (255);
    //text (startTimer.getTime(), 60, 60);
    gameStarted = false;
    gameRestart = false;
    drawGameOver();
    ghost.reset();
    for (Obstacle obs : obstacles) { 
      obs.reset();
    }
  }
  //if (gameState==2 &amp;&amp; startTimer.getTime()&gt;=3.5) {
  if (gameState==2 &amp;&amp; mousePressed) {
    if (gameStarted == false &amp;&amp; gameRestart == false);
    //timerReset();
    gameState=3;
  }


  if (gameState==3) {
    gameRestart = true;
    drawMenuScreen();
    score.highscores();
  } 
  if (gameState==3 &amp;&amp; mousePressed) {  
    if (gameRestart == true)
      score.reset();
    gameState = 1;
  }
}






 class Score {
  private int score = 0;
  private int highscore;
  private boolean scoreIncreased = false;

// Methods for increasing scores. If score is NOT increased

  void highscores(){
    if (score&gt;highscore)
      highscore=score;
      else if (gameState==0 || gameState==3) {
      textAlign(CENTER);
      fill(255);
      textSize(width/60);
      text("HIGHSCORE: " + highscore, width/2, height/2 + height/8.25);
    }  
  }

  void increase() {
    if (!scoreIncreased) {
      score += 1;
      scoreIncreased = true;
    }
  }

  void reset() {
    score = 0;
    scoreIncreased = false;
  }

  void allowScoreIncrease() {
    scoreIncreased = false;
  }


  void draw() {
    pushStyle();
    if (gameState==2) {
      textAlign(CENTER);
      fill(0);
      textSize(width/60);
      text("SCORE: " + score, width/2, height/2 + 65);
    } 
      else if (gameState==1) {
      //rectMode(CORNER);
      textAlign(CENTER);
      fill(255);
      textSize(width/60);
      text("Score: " + score, width/2, 40);
    }
    popStyle();
  }
}



 class Obstacle {
  float initX;
  float topX;
  float topY;
  float w = 120; // original was 50
  PImage obstacle1, obstacle2;

  Obstacle(float initialTopX, float initialTopY) {
    initX = initialTopX;
    topX = initialTopX;
    topY = initialTopY;
    obstacle1 = loadImage("https://" + "i.imgur.com/9Dnn4sI.png");
    obstacle2 = loadImage("https://" + "i.imgur.com/d83OfMi.png");
  }


  void draw() {
    pushStyle();
    imageMode(CORNERS);
    image(obstacle1, topX, topY, topX+w, height-1);
    image(obstacle2, topX, 0, topX+w, topY - 180);
    popStyle();

     // Controls speed of object x movements (namely for obstacles)
    // topX -= 4.25;
    topX -= 9.5;
  }

  void reset() {
    topX = initX;
    topY = random(100, height-100);
  }

  void reposition() {
    topX = width;
    topY = random(100, height-100);
  }
 } 




class Ghost {
  float x;
  float y;
  float size = 85;
  float vy = 0;
  float ay = 0.63;
  PImage ghost;

  Ghost(float initialX, float initialY) {
    x = initialX;
    y = initialY;
    ghost = loadImage("https://" + "i.imgur.com/GPRyMO7.png");
  }

  void draw() {
    pushStyle();
    imageMode(CENTER);
    image(ghost, x, y, size, size);
    popStyle();


    y += vy;
    vy += ay;
  }

  void reset() {
    y = 200;
    vy = 0;
  }

  void jump() {
    vy = -9.5;
  }
}






// void Souls(){   
//   currentFrame = (currentFrame+1) % numFrames;  // Use % to cycle through frames
//   image(souls[(currentFrame) % numFrames], width/2, height/2);
//}




void drawGameOver() {
  pushStyle();
  fill(200, 200, 200, 200);
  noStroke();
  rect(-20, -20, width + 40, height + 40);
  score.draw ();
  popStyle();
}

void drawMenuScreen() {
  fill(0);
  noStroke();
  rect(-20, -20, width + 40, height + 40);
  ;
}



void detectCollision() {
  // Did the ghost come out of the screen?
  if (ghost.y &gt; height || ghost.y &lt; 0) {
    gameState=2;
  }

  for (Obstacle obstacle : obstacles) {
    if (ghost.x - ghost.size/2.0 &gt; obstacle.topX + obstacle.w) {
      score.increase();
    }

    if (obstacle.topX + obstacle.w &lt; 0) {
      obstacle.reposition();
      score.allowScoreIncrease();
    }

    //if (obstacle.detectCollision(ghost)) {
      //gameState=2;
    //}
  }
}
</code></pre>

<p>Can you help with incorporating the collision detection method below to the work on the ghost class and the obstacles array in my code</p>

<pre><code>  obstacle2 = loadImage("up2.png");
  obstacle1x = (width - obstacle1.width)/2;
  obstacle1y = (height/3 - obstacle1.height)/2;
//  obstacle2x = (width - obstacle2.width)/4;
//  obstacle2y = (height - obstacle2.height)/4;
//    f2x = (width - ghost.width)/2;
//      f2y = (height - ghost.height)/2;
}

void draw(){
  background(0);
  image(obstacle1,obstacle1x,obstacle1y);
  obstacle1x = obstacle1x-2;
  if (obstacle1x &lt;= -150){
    obstacle1x = 900;}
  image(ghost,mouseX,mouseY);
  if(pp_collision(obstacle1,obstacle1x,obstacle1y,ghost,mouseX,mouseY)){
    stroke(255,64,64);
    strokeWeight(1);
    noFill();
    rect(obstacle1x,obstacle1y,obstacle1.width,obstacle1.height);
    rect(mouseX,mouseY,ghost.width,ghost.height);
  }
}

final int ALPHALEVEL = 20;

boolean pp_collision(PImage imgA, float aix, float aiy, PImage imgB, float bix, float biy) {
  int topA, botA, leftA, rightA;
  int topB, botB, leftB, rightB;
  int topO, botO, leftO, rightO;
  int ax, ay;
  int bx, by;
  int APx, APy, ASx, ASy;
  int BPx, BPy; //, BSx, BSy;

  topA   = (int) aiy;
  botA   = (int) aiy + imgA.height;
  leftA  = (int) aix;
  rightA = (int) aix + imgA.width;
  topB   = (int) biy;
  botB   = (int) biy + imgB.height;
  leftB  = (int) bix;
  rightB = (int) bix + imgB.width;

  if (botA &lt;= topB  || botB &lt;= topA || rightA &lt;= leftB || rightB &lt;= leftA)
    return false;

  // If we get here, we know that there is an overlap
  // So we work out where the sides of the overlap are
  leftO = (leftA &lt; leftB) ? leftB : leftA;
  rightO = (rightA &gt; rightB) ? rightB : rightA;
  botO = (botA &gt; botB) ? botB : botA;
  topO = (topA &lt; topB) ? topB : topA;


  // P is the top-left, S is the bottom-right of the overlap
  APx = leftO-leftA;   
  APy = topO-topA;
  ASx = rightO-leftA;  
  ASy = botO-topA-1;
  BPx = leftO-leftB;   
  BPy = topO-topB;

  int widthO = rightO - leftO;
  boolean foundCollision = false;

  // Images to test
  imgA.loadPixels();
  imgB.loadPixels();

  // These are widths in BYTES. They are used inside the loop
  //  to avoid the need to do the slow multiplications
  int surfaceWidthA = imgA.width;
  int surfaceWidthB = imgB.width;

  boolean pixelAtransparent = true;
  boolean pixelBtransparent = true;

  // Get start pixel positions
  int pA = (APy * surfaceWidthA) + APx;
  int pB = (BPy * surfaceWidthB) + BPx;

  ax = APx; 
  ay = APy;
  bx = BPx; 
  by = BPy;
  for (ay = APy; ay &lt; ASy; ay++) {
    bx = BPx;
    for (ax = APx; ax &lt; ASx; ax++) {
      pixelAtransparent = alpha(imgA.pixels[pA]) &lt; ALPHALEVEL;
      pixelBtransparent = alpha(imgB.pixels[pB]) &lt; ALPHALEVEL;

      if (!pixelAtransparent &amp;&amp; !pixelBtransparent) {
        foundCollision = true;
        break;
      }
      pA ++;
      pB ++;
      bx++;
    }
    if (foundCollision) break;
    pA = pA + surfaceWidthA - widthO;
    pB = pB + surfaceWidthB - widthO;
    by++;
  }
  return foundCollision;
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Eggs disappearing in mid-air as they are going down</title>
      <link>https://forum.processing.org/two/discussion/27857/eggs-disappearing-in-mid-air-as-they-are-going-down</link>
      <pubDate>Thu, 26 Apr 2018 19:28:42 +0000</pubDate>
      <dc:creator>arrowencer</dc:creator>
      <guid isPermaLink="false">27857@/two/discussions</guid>
      <description><![CDATA[<p>eggs disappear midway as they are going down, please send help! idk if its the timer or array problem, but if anyone can detect why it disappears at random times, then please send the correct code</p>

<pre><code> PImage pan;
 PImage egg;
 PImage goldegg;
 PImage rottenegg;

 Pan catcher;
 Timer timer;
 Normal[] normal; 
 Golden[] golden;
 Rotten[] rotten;
 int totalNormal = 0;
 int totalGolden = 0;
 int totalRotten = 0;

 void setup() {
   size(500, 500);
   imageMode(CENTER);

   catcher = new Pan(20); 
   normal = new Normal[10];
   golden = new Golden[5];
   rotten = new Rotten[5];
   timer = new Timer(3000);
   timer.start();
 }

 void draw() {
   background(250, 237, 235);

   catcher.setLocation(mouseX, 450); 
   catcher.display(); 


   if (timer.isFinished()) {

     normal[totalNormal] = new Normal();
     totalNormal++ ;

     golden[totalGolden] = new Golden();
     totalGolden++;

     rotten[totalRotten] = new Rotten();
     totalRotten++;

     if (totalNormal &gt;= normal.length) {
       totalNormal = 0; 
     }

     if (totalGolden &gt;= golden.length) {
      totalGolden = 0;
     }

     if (totalRotten &gt;= rotten.length) {
       totalRotten = 0;
     }

     timer.start();
   }


   for (int i = 0; i &lt; totalNormal; i++ ) {
     normal[i].move();
     normal[i].display();

     if (catcher.collision(normal[i])) {
       normal[i].caught();
     }
   }

   for (int i = 0; i &lt; totalGolden; i++) {
    golden [i].move();
    golden[i].display();

    if(catcher.collision(golden[i])) {
      golden[i].caught();
    }
   }

   for (int i = 0; i &lt;totalRotten; i++){
     rotten[i].move();
     rotten[i].display();

     if(catcher.collision(rotten[i])) {
       rotten[i].caught();
     }
   }
 }
 class Golden {
   float x, y, r;
   float speed;

   Golden() {
     r = 10;
     x = random(width);
     y = -20;
     speed = 4;
   }

   void move() {
     y += speed;
   }


 //  boolean atBottom() {

 //    if (y &gt; height + r) { 
 //      return true;
 //    } else {
 //      return false;
 //    }
 //  }


   void display() {
     goldegg = loadImage ("GoldenEgg.png");

     for (int i = 2; i &lt; r; i++ ) {
     image(goldegg, x, y);
     }
   }


   void caught() {
    speed = 0; 
    y = -1000;
   }
 }
 class Normal {
   float x, y, r;
   float speed;

   Normal() {
     r = 10;
     x = random(width);
     y = -20;
     speed = random(1, 2);
   }

   void move() {
     y += speed;
   }


   //boolean atBottom() {

   //  if (y &gt; height + r) { 
   //    return true;
   //  } else {
   //    return false;
   //  }
   //}


   void display() {
     egg = loadImage ("RegularEgg.png");

     for (int i = 2; i &lt; r; i++ ) {
     image(egg, x, y);
     }
   }


   void caught() {
    speed = 0; 
    y = -1000;
   }
 }
 float r; 
   float x, y; 

   Pan(float tempR) {
     r = tempR;
     x = 0;
     y = 0;
   }

   void setLocation(float tempX, float tempY) {
     x = tempX;
     y = tempY;
   }

   void display() {
    pan = loadImage ("Pan.png");
    image(pan, x, y);

   }


   boolean collision(Normal n) {
    float d = dist(x, y, n.x, n.y); 

     if (d &lt; r + n.r) { 
       return true;
     } else {
       return false;
     }
   }

   boolean collision(Golden g) {
    float d = dist(x, y, g.x, g.y);

     if (d &lt; r + g.r) {
       return true;
     } else {
       return false;
    }
   }

   boolean collision(Rotten o) {
     float d = dist(x, y, o.x, o.y);

     if (d &lt; r + o.r) {
       return true;
     } else {
       return false;
     }
   }
  }
 class Rotten {
   float x, y, r;
   float speed;

   Rotten() {
     r = 10;
     x = random(width);
     y = -20;
     speed = 3;
   }

   void move() {
     y += speed;
   }


   //boolean atBottom() {

   //  if (y &gt; height + r) { 
   //    return true;
   //  } else {
   //    return false;
   //  }
   //}


   void display() {
     rottenegg = loadImage ("RottenEgg.png");

     for (int i = 2; i &lt; r; i++ ) {
     image(rottenegg, x, y);
     }
   }

   void caught() {
    speed = 0; 
    y = -1000;
   }
 }
 class Timer {

   int savedTime; 
   int totalTime; 

   Timer(int tempTotalTime) {
     totalTime = tempTotalTime;
   }


   void start() {

     savedTime = millis();
   }


   boolean isFinished() { 

     int passedTime = millis()- savedTime;
     if (passedTime &gt; totalTime) {
       return true;
     } else {
       return false;
     }
   }
 }
</code></pre>
]]></description>
   </item>
   <item>
      <title>mouse Drag &amp; Drop</title>
      <link>https://forum.processing.org/two/discussion/27803/mouse-drag-drop</link>
      <pubDate>Fri, 20 Apr 2018 04:12:34 +0000</pubDate>
      <dc:creator>hellostranger</dc:creator>
      <guid isPermaLink="false">27803@/two/discussions</guid>
      <description><![CDATA[<p>I'm doing some Instagram story-like things, like putting emojis on my processing sketch.
I set 84 emojis in 7x12 array and if I click each of emojis, then the emoji should be dragged and dropped.
the problem is the code I wrote. The emoji stuck when I click and drag.</p>

<hr />

<p>void mouseDragged() {</p>

<p>if (155&gt;mouseX &amp;&amp; mouseX&gt;100 &amp;&amp; 100&gt;mouseY &amp;&amp; mouseY&gt;51 == true)
{
    image(emoji, pmouseX, pmouseY);
}</p>

<p>---------------------=============
I think that range is the issue. The emoji is just stuck in that range.
how can I drag and drop images where I want to put on?</p>
]]></description>
   </item>
   <item>
      <title>Collision detection between 2 objects with an ultrasonic sensor (Arduino)</title>
      <link>https://forum.processing.org/two/discussion/27682/collision-detection-between-2-objects-with-an-ultrasonic-sensor-arduino</link>
      <pubDate>Tue, 03 Apr 2018 15:35:25 +0000</pubDate>
      <dc:creator>120898</dc:creator>
      <guid isPermaLink="false">27682@/two/discussions</guid>
      <description><![CDATA[<p>So I'm working from the code in Step 6 (I will attach below) of this tutorial: <a href="http://www.instructables.com/id/How-to-control-a-simple-Processing-game-with-Ardui/" target="_blank" rel="nofollow">http://www.instructables.com/id/How-to-control-a-simple-Processing-game-with-Ardui/</a></p>

<p>I am using an HR-SR04 ultrasonic sensor to control the plane, moving it up and down via hand movements.</p>

<p>What I am trying to do is implement an obstacle feature to the game, by having the score decrease by 1 when the plane hits a cloud. However I can't get the collision detection working for when the plane hits a cloud. The original author has managed to get this working perfectly for when the plane hits the bird but I'm not sure how to get it working between the plane and clouds, I've tried everything I can think of but I'm stuck.</p>

<p>Any help would be appreciated.</p>

<p>Below is the code that I'm working from:</p>

<pre><code>`//Ultrasound plane, a game with a plane and an ultrsound sensor (but

//by Yoruk for Instructables

//send commments or questions to Yoruk16_72 AT yahoo DOT fr


//19 07 14 : initial code
//20 07 14 : it works with arduino !!
//07 07 15 : picture for the plane and for the grass
//06 12 15 : score system with the bird


int i, j; 

int Score ;
float DistancePlaneBird;


float Hauteur; //en Y
float Angle;
int DistanceUltra;
int IncomingDistance;
//float Pas; //pour deplacements X

float BirdX;
float BirdY;

float GrassX ;  //for X position




String DataIn; //incoming data on the serial port

//5 a 32 cm


float [] CloudX = new float[6];
float [] CloudY = new float[6];

//vitesse constante hein


PImage Cloud;
PImage Bird;
PImage Plane;
PImage Grass;



// serial port config
import processing.serial.*; 
Serial myPort;    



//preparation
void setup() 
{

    myPort = new Serial(this, "/dev/cu.usbmodem1411", 9600); 

    myPort.bufferUntil(10);   //end the reception as it detects a carriage return

    frameRate(30); 

    size(800, 600);
    rectMode(CORNERS) ; //we give the corners coordinates 
    noCursor(); //why not ?
    textSize(16);

    Hauteur = 300; //initial plane value


    Cloud = loadImage("cloud.png");  //load a picture
    Bird = loadImage("bird.png");  
    Plane = loadImage("plane.png");  //the new plane picture

        Grass = loadImage("grass.png");  //some grass


    //int clouds position
    for  (int i = 1; i &lt;= 5; i = i+1) {
        CloudX[i]=random(1000);
        CloudY[i]=random(400);
    }


    Score = 0;
}


//incoming data event on the serial port


void serialEvent(Serial p) { 
    DataIn = p.readString(); 
    // println(DataIn);

    IncomingDistance = int(trim(DataIn)); //conversion from string to integer

    println(IncomingDistance); //checks....

    if (IncomingDistance&gt;1  &amp;&amp; IncomingDistance&lt;100 ) {
        DistanceUltra = IncomingDistance; //save the value only if its in the range 1 to 100     }
    }
}


//main drawing loop
void draw() 
{
    background(0, 0, 0);
    Ciel(); //draw the sky
    fill(5, 72, 0);



    //rect(0, 580, 800, 600); //some grass

    //new grass : 

    for  (int i = -2; i &lt;= 4; i = i+1) {  //a loop to display the grass picture 6 times

        image(Grass, 224*i  + GrassX, 550, 224, 58);  // 224 58 : picture size
    }

    //calculates the X grass translation. Same formulae than the bird
    GrassX = GrassX  -  cos(radians(Angle))*10;

    if (GrassX &lt; -224) {  //why 224 ? to have a perfect loop
        GrassX=224;
    }


    text(Angle, 10, 30); //debug things...
    text(Hauteur, 10, 60); 


    //new part : check the distance between the plane and bird and increase the score
    DistancePlaneBird = sqrt(pow((400-BirdX), 2) + pow((Hauteur-BirdY), 2)) ;

    if (DistancePlaneBird &lt; 40) {
        //we hit the bird   
        Score = Score+ 1;

        //reset the bird position
        BirdX = 900;
        BirdY = random(600);
    }

    //here we draw the score
    text("Score :", 200, 30); 
    text( Score, 260, 30); 



    //Angle = mouseY-300; //uncomment this line and comment the next one if you want to play with the mouse
    Angle = (18- DistanceUltra)*4;  // you can increase the 4 value...



    Hauteur = Hauteur + sin(radians(Angle))*10; //calculates the vertical position of the plane

    //check the height range to keep the plane on the screen 
    if (Hauteur &lt; 0) {
        Hauteur=0;
    }

    if (Hauteur &gt; 600) {
        Hauteur=600;
    }

    TraceAvion(Hauteur, Angle);

    BirdX = BirdX - cos(radians(Angle))*10;

    if (BirdX &lt; -30) {
        BirdX=900;
        BirdY = random(600);
    }

    //draw and move the clouds
    for  (int i = 1; i &lt;= 5; i = i+1) {
        CloudX[i] = CloudX[i] - cos(radians(Angle))*(10+2*i);

        image(Cloud, CloudX[i], CloudY[i], 300, 200);

        if (CloudX[i] &lt; -300) {
            CloudX[i]=1000;
            CloudY[i] = random(400);
        }
    }

    image(Bird, BirdX, BirdY, 59, 38); //displays the useless bird. 59 and 38 are the size in pixels of the picture
}


void Ciel() {
    //draw the sky

    noStroke();
    rectMode(CORNERS);

    for  (int i = 1; i &lt; 600; i = i+10) {
        fill( 49    +i*0.165, 118   +i*0.118, 181  + i*0.075   );
        rect(0, i, 800, i+10);
    }
}


void TraceAvion(float Y, float AngleInclinaison) {
    //draw the plane at given position and angle

    noStroke();
    pushMatrix();
    translate(400, Y);
    rotate(radians(AngleInclinaison)); //in degres  ! 


    /*
    Drawing concept :  ;-)

     |\___o__
     ________&gt;     

     */

    scale(0.5);  //0.2 pas mal

    //unless drawing the plane "by hands", just display the stored picture instead. Note that the parameters 2 and 3 are half the picture size, to make sure that the plane rotates in his center.
    image(Plane, -111, -55, 223, 110); // 223 110 : picture size



    popMatrix(); //end of the rotation matrix
}

//file end`
</code></pre>
]]></description>
   </item>
   <item>
      <title>Beginner Array Question</title>
      <link>https://forum.processing.org/two/discussion/26389/beginner-array-question</link>
      <pubDate>Fri, 16 Feb 2018 21:35:16 +0000</pubDate>
      <dc:creator>Neowso</dc:creator>
      <guid isPermaLink="false">26389@/two/discussions</guid>
      <description><![CDATA[<p>I have an array of narrow vertical rectangles. Each rectangle moves randomly back and forth (left to right) across the screen.</p>

<p>When *any of these overlap I want an event to happen, eg. println "overlapping". So far this works fine with 2 rectangles, but only by referring directly to their number and not array indices.</p>

<p><img src="https://forum.processing.org/two/uploads/imageupload/604/HC5EPSAJJD48.png" alt="Screen Shot 2018-02-16 at 21.21.04" title="Screen Shot 2018-02-16 at 21.21.04" /></p>

<p>When I try indexing, I am running into the issue that, when a rectangle is checking if it overlaps another, it is also checking if it overlaps itself, and thus always returns "overlapping". I get what is happening but how to solve it is a little beyond my reasoning abilities.</p>

<p>Would anyone care to offer some tips? Many thanks.</p>
]]></description>
   </item>
   <item>
      <title>How To Set Up Array Class Collisions?</title>
      <link>https://forum.processing.org/two/discussion/26884/how-to-set-up-array-class-collisions</link>
      <pubDate>Fri, 16 Mar 2018 20:06:54 +0000</pubDate>
      <dc:creator>JawshyDawg</dc:creator>
      <guid isPermaLink="false">26884@/two/discussions</guid>
      <description><![CDATA[<p>I'm attempting to give my array classes 'Opp' and 'Ally' the same behaviours as the playable 'Balls' So they must all collide with one another and to collide with the puck in order to manoeuvre it in the correct and appropriate direction. Any assistance would be more than greatly appreciated. Thank you!
Miles. M</p>

<p>(Please forgive the poor structure of my code, if that can be improved also any advice would be very helpful)</p>

<p>&gt;</p>

<pre><code>boolean keyW = false;
boolean keyA = false;
boolean keyS = false;
boolean keyD = false;

boolean keyUp = false;
boolean keyLeft = false;
boolean keyDown = false;
boolean keyRight = false;


float gVelocity     = .2;
final float gVelocityFriction = .99;
final float gElasticity    = 1;

ArrayList&lt;Opp&gt; Opps;
ArrayList&lt;Ally&gt; Allys;

Ball player1;
Ball player2;
Ball puck;

int score1, score2;

PFont title;

int mode;
int stage;

void setup() {

  stage = 1;
  mode = 0;

  size(1280, 720);
  frameRate(60);

  Opps = new ArrayList&lt;Opp&gt;();
  for (int i = 0; i &lt; 2; i++) {
    Opps.add(new Opp(random(640,1280),random(height)));
  }
    Allys = new ArrayList&lt;Ally&gt;();
  for (int i = 0; i &lt; 2; i++) {
    Allys.add(new Ally(random(0,640),random(height)));
  }

  player1 = new Ball(new PVector(width*3/4, height/2), 
  new PVector(width*3/4, height/2), 
  64, 24);
  player2 = new Ball(new PVector(width/4, height/2), 
  new PVector(width/4, height/2), 
  192, 24);
  puck = new Ball(new PVector(width/2, height/2), 
  new PVector(width/2, height/2), 
  128, 16);                   

  score1 = 0;
  score2 = 0;
}

void draw() {

  background(205, 255, 255);

    if(stage==1){

      rectMode(CENTER);
      noStroke();
      fill(255, 55, 55, 185);
      rect(640, 170, 430, 160);
      fill(55, 255, 55, 185);
      rect(640, 350, 430, 160);
      fill(55, 55, 255, 185);
      rect(640, 530, 430, 160);

      textAlign(CENTER);
      textSize(32);
      fill(0);
      text("Demo", 640, 180);
      text("One Player Start", 640, 360);
      text("Two Player Start", 640, 540);

      if(mousePressed) {
        if (mouseX &gt; 425 &amp;&amp; mouseX &lt; 855 &amp;&amp; mouseY &gt; 85 &amp;&amp; mouseY &lt; 255) {
        stage = 2; 
        mode = 1;
      }
    }
      if(mousePressed) {  
        if (mouseX &gt; 425 &amp;&amp; mouseX &lt; 855 &amp;&amp; mouseY &gt; 265 &amp;&amp; mouseY &lt; 435) { 
        stage = 2;
        mode = 2;
      }
    }

      if(mousePressed) {
        if (mouseX &gt; 425 &amp;&amp; mouseX &lt; 855 &amp;&amp; mouseY &gt; 445 &amp;&amp; mouseY &lt; 615) {
        stage = 2; 
        mode = 3;
      }
    }
  }

if(stage==2){

  rectMode(CORNER);
  background(205, 255, 255);

  noStroke();
  fill(255, 55, 55, 185);
  rect(0, 0, width/2, height);
  fill(55, 55, 255, 185);
  rect(width/2, 0, width, height);

  fill(205, 5, 5, 95);
  rect(0, 180, width/16, height/2);
  fill(5, 5, 205, 95);
  rect(width*15/16, 180, width, height/2);

  for (Opp o : Opps) {
    o.applyBehaviors(Opps);
    o.update();
    o.display();
  }

    for (Ally a : Allys) {
    a.applyBehaviors(Allys);
    a.update();
    a.display();
  }

  if (puck.position.x &lt; width/16 &amp;&amp; puck.position.y &gt; 180 &amp;&amp; puck.position.y &lt; 540 ) {
    score2++;
    reset(-1);
  }
  if (puck.position.x &gt; width*15/16 &amp;&amp; puck.position.y &gt; 180 &amp;&amp; puck.position.y &lt; 540 ) {
    score1++;
    reset(1);
  }

  // Ticks and renders test balls
  player1.tick();
  player2.tick();
  puck.tick();

  player1.render();
  player2.render();
  puck.render();

  if (mode==1) {
  hal(player1, puck, player2, 1);
  hal(player2, puck, player1, -1);
  }

  if (mode==2) {
  hal(player1, puck, player2, 1);
  //hal(player2, puck, player1, -1);
  }

  if (mode==3) {
  //hal(player1, puck, player2, 1);
  //hal(player2, puck, player1, -1);
  }

  arrowkeyS(player1);
  wasd(player2);

  Ball tempb1 = new Ball(new PVector(player1.position.x, player1.position.y), 
  new PVector(player1.last.x, player1.last.y), 
  0, player1.radius);
  Ball tempb2 = new Ball(new PVector(player2.position.x, player2.position.y), 
  new PVector(player2.last.x, player2.last.y), 
  0, player2.radius);
  Ball temppuck = new Ball(new PVector(puck.position.x, puck.position.y), 
  new PVector(puck.last.x, puck.last.y), 
  0, puck.radius);

  // Colliding balls 1 and 2
  player1.collide(tempb2);
  player2.collide(tempb1);
  player1.collide(temppuck);
  player2.collide(temppuck);
  puck.collide(tempb2);
  puck.collide(tempb1);

  fill(0);
  textSize(16);
  text("FPS:" + floor(frameRate), width-64, height-8);

  fill(0);
  textSize(32);
  text(score1, width/4, height/16);
  text(score2, width*3/4, height/16);

  if (score1==5){
    stage = 3;
  }

   if (score2==5){
    stage = 4;
  }

  }

  if (stage==3) {

    background(205, 255, 255);

    noStroke();
    fill(255, 55, 55, 185);
    rect(0, 0, 1280, 720);

    textAlign(CENTER);
    textSize(64);
    fill(0);
    text("Red Team Wins!", 640, 340);
    textSize(32);
    text("Press 'R' To Restart!", 640, 400);

    if (keyPressed) {
      if (key == 'r' || key == 'R') {

        stage = 1;
        score1 = 0;
        score2 = 0;

      }

    }

  }

  if (stage==4) {

    background(205, 255, 255);


    noStroke();
    fill(55, 55, 255, 185);
    rect(0, 0, 1280, 720);

    textAlign(CENTER);
    textSize(64);
    fill(0);
    text("Blue Team Wins!", 640, 340);
    textSize(32);
    text("Press 'R' To Restart!", 640, 400);

    if (keyPressed) {
      if (key == 'r' || key =='R') {

        stage = 1;
        score1 = 0;
        score2 = 0;

      }

    }

  }


}
// Input
void keyPressed() {
  gameKeyPressed();
}

void keyReleased() {
  gameKeyReleased();
}

void reset(int winner) {
  player1.reset(new PVector(width*3/4, height/2));
  player2.reset(new PVector(width/4, height/2));
  puck.reset(new PVector(width/2+winner*(width/8), random(height/4, height*3/4)));
}

void hal(Ball tb, Ball tp, Ball opp,int side){
  PVector ratio = new PVector(0, 0);
  if(tp.position.y != tb.position.y) ratio.y += getSign(tp.position.y-tb.position.y);
  if(tp.position.x != tb.position.x) ratio.x += getSign(side*12+tp.position.x-tb.position.x);

  if(tp.position.y != opp.position.y) ratio.y += .02*getSign(tp.position.y-opp.position.y);

  if(ratio.x != 0) tb.position.x += (gVelocity) * getSign(ratio.x) * sqrt(abs(ratio.x)) / (abs(ratio.x)+abs(ratio.y));
  if(ratio.y != 0) tb.position.y += (gVelocity) * getSign(ratio.y) * sqrt(abs(ratio.y)) / (abs(ratio.x)+abs(ratio.y));
}

float getSign(float num) {return num/abs(num);}

// Detects active keyS
void gameKeyPressed() {

  if (key == CODED) {
    if (keyCode == UP) {
      keyUp = true;    
      keyDown = false;
    }
    if (keyCode == LEFT) {
      keyLeft = true;  
      keyRight = false;
    }
    if (keyCode == RIGHT) {
      keyRight = true; 
      keyLeft = false;
    }
    if (keyCode == DOWN) {
      keyDown = true;  
      keyUp = false;
    }
  }

  if (key == 'w' || key == 'W') {
    keyW = true; 
    keyS = false;
  }
  if (key == 'a' || key == 'A') {
    keyA = true; 
    keyD = false;
  }
  if (key == 'd' || key == 'D') {
    keyD = true; 
    keyA = false;
  }
  if (key == 's' || key == 'S') {
    keyS = true; 
    keyW = false;
  }
  if (key == 'r' || key == 'R') reset(0);
}

// Detects when keyS go inactive
void gameKeyReleased() {
  if (key == CODED) {
    if (keyCode == UP) keyUp = false;
    if (keyCode == LEFT) keyLeft = false;
    if (keyCode == DOWN) keyDown = false;
    if (keyCode == RIGHT) keyRight = false;

  }

  if (key == 'w' || key == 'W') keyW = false;
  if (key == 'a' || key == 'A') keyA = false;
  if (key == 's' || key == 'S') keyS = false;
  if (key == 'd' || key == 'D') keyD = false;

}


void arrowkeyS(Ball tb) {

  if (keyDown == true) 
    // Limits positioneration when two directions are active
    if (keyLeft == true || keyRight == true) 
      tb.position.y += gVelocity/sqrt(2);
    else 
      tb.position.y += gVelocity;
  else if (keyUp == true) { 
    if (keyLeft == true || keyRight == true) 
      tb.position.y -= gVelocity/sqrt(2);
    else 
      tb.position.y -= gVelocity;
  }

  if (keyRight == true) 
    if (keyUp == true || keyDown == true) 
      tb.position.x += gVelocity/sqrt(2);
    else 
      tb.position.x += gVelocity;
  else if (keyLeft == true) {
    if (keyUp == true || keyDown == true) 
      tb.position.x -= gVelocity/sqrt(2);
    else 
      tb.position.x -= gVelocity;
  }
}

void wasd(Ball tb) {
  // Test ball 2
  if (keyS == true)
    if (keyA == true || keyD == true) 
      tb.position.y += gVelocity/sqrt(2);
    else  
      tb.position.y += gVelocity;
  else if (keyW == true) {  
    if (keyA == true || keyD == true) 
      tb.position.y -= gVelocity/sqrt(2);
    else 
      tb.position.y -= gVelocity;
  }

  if (keyD == true) 
    if (keyW == true || keyS == true) 
      tb.position.x += gVelocity/sqrt(2);
    else 
      tb.position.x += gVelocity;
  else if (keyA == true) {
    if (keyW == true || keyS == true) 
      tb.position.x -= gVelocity/sqrt(2);
    else 
      tb.position.x -= gVelocity;
  }
}

class Ally {

  // All the usual stuff
  PVector position;
  PVector velocity;
  PVector acceleration;
  float r;
  float maxforce;    // Maximum steering force
  float maxspeed;    // Maximum speed

  Ally(float x, float y) {
    position = new PVector(x, y);
    r = 12;
    maxspeed = 3;
    maxforce = 0.2;
    acceleration = new PVector(0, 0);
    velocity = new PVector(0, 0);
  }

  void applyForce(PVector force) {
    acceleration.add(force);
  }

  void applyBehaviors(ArrayList&lt;Ally&gt; Allys) {
     PVector separateForce = separate(Allys);
     PVector seekForce = seek(new PVector(puck.position.x,puck.position.y));
     separateForce.mult(2);
     seekForce.mult(1);
     applyForce(separateForce);
     applyForce(seekForce); 
  }

  PVector seek(PVector target) {
    PVector desired = PVector.sub(target,position);  // A vector pointing from the position to the target

    desired.normalize();
    desired.mult(maxspeed);
    PVector steer = PVector.sub(desired,velocity);
    steer.limit(maxforce);  // Limit to maximum steering force

    return steer;
  }

  PVector separate (ArrayList&lt;Ally&gt; Allys) {
    float desiredseparation = r*2;
    PVector sum = new PVector();
    int count = 0;
    for (Ally other : Allys) {
      float d = PVector.dist(position, other.position);
      if ((d &gt; 0) &amp;&amp; (d &lt; desiredseparation)) {
        PVector diff = PVector.sub(position, other.position);
        diff.normalize();
        diff.div(d);        // Weight by distance
        sum.add(diff);
        count++;            // Keep track of how many
      }
    }
    // Average -- divide by how many
    if (count &gt; 0) {
      sum.div(count);
      sum.normalize();
      sum.mult(maxspeed);
      sum.sub(velocity);
      sum.limit(maxforce);
    }
    return sum;
  }


  void update() {
    velocity.add(acceleration);
    velocity.limit(maxspeed);
    position.add(velocity);
    acceleration.mult(0);
  }

  void display() {
    fill(182, 45, 182, 245);
    noStroke();
    pushMatrix();
    translate(position.x, position.y);
    ellipse(0, 0, 50, 50);
    popMatrix();
  }

}

class Opp {

  // All the usual stuff
  PVector position;
  PVector velocity;
  PVector acceleration;
  float r;
  float maxforce; 
  float maxspeed;  


  Opp(float x, float y) {
    position = new PVector(x, y);
    r = 12;
    maxspeed = 3;
    maxforce = 0.2;
    acceleration = new PVector(0, 0);
    velocity = new PVector(0, 0); 
  }

  void applyForce(PVector force) {
    acceleration.add(force);
  }

  void applyBehaviors(ArrayList&lt;Opp&gt; Opps) {
     PVector separateForce = separate(Opps);
     PVector seekForce = seek(new PVector(puck.position.x,puck.position.y));
     separateForce.mult(2);
     seekForce.mult(1);
     applyForce(separateForce);
     applyForce(seekForce); 
  }


  PVector seek(PVector target) {
    PVector desired = PVector.sub(target,position);  

    desired.normalize();
    desired.mult(maxspeed);
    PVector steer = PVector.sub(desired,velocity);
    steer.limit(maxforce);  // Limit to maximum steering force

    return steer;
  }


  PVector separate (ArrayList&lt;Opp&gt; Opps) {
    float desiredseparation = r*2;
    PVector sum = new PVector();
    int count = 0;
    for (Opp other : Opps) {
      float d = PVector.dist(position, other.position);
      if ((d &gt; 0) &amp;&amp; (d &lt; desiredseparation)) {
        PVector diff = PVector.sub(position, other.position);
        diff.normalize();
        diff.div(d);        
        sum.add(diff);
        count++;           
      }
    }
    if (count &gt; 0) {
      sum.div(count);
      sum.normalize();
      sum.mult(maxspeed);
      sum.sub(velocity);
      sum.limit(maxforce);
    }
    return sum;
  }

  void update() {
    velocity.add(acceleration);
    velocity.limit(maxspeed);
    position.add(velocity);
    acceleration.mult(0);
  }

  void display() {
    fill(62, 43, 181, 245);
    noStroke();
    pushMatrix();
    translate(position.x, position.y);
    ellipse(0, 0, 50, 50);
    popMatrix();
  }

}

class Ball {

  PVector position, last;
  private int hue, radius;

  Ball(PVector t, PVector tl, int th,  int tr) {
    position   = t;
    last  = tl;
    hue = th;
    radius = tr;
  }

  void tick() {
    PVector tempp = new PVector();
    tempp.set(position.x, position.y);

    position.set(position.x + gVelocityFriction*((position.x - last.x)), 
            position.y + gVelocityFriction*(position.y - last.y));
    last.set(tempp.x, tempp.y);

    if (position.x &lt; 0)      bounceX(0);
    if (position.x &gt; width)  bounceX(width);
    if (position.y &lt; 0)      bounceY(0);
    if (position.y &gt; height) bounceY(height);
  }

    void render() {
    noStroke();
    fill(hue, 25, 185);
    ellipse(position.x, position.y, radius*2, radius*2);
  }

  void collide(Ball hit) {
    if (dist(hit.position.x, hit.position.y, position.x, position.y) &lt; hit.radius+radius) {
      float phi = atan2(hit.position.y - position.y, hit.position.x - position.x);

      float v1 = dist(position.x, position.y, last.x, last.y);
      float ang1 = atan2(position.y - last.y, position.x - last.x);
      float v2 = dist(hit.position.x, hit.position.y, hit.last.x, hit.last.y);
      float ang2 = atan2(hit.position.y - hit.last.y, hit.position.x - hit.last.x);

      last.x = position.x-gElasticity*(v2*cos(ang2-phi)*cos(phi)+v1*sin(ang1-phi)*cos(phi+PI/2));
      last.y = position.y-gElasticity*(v2*cos(ang2-phi)*sin(phi)+v1*sin(ang1-phi)*sin(phi+PI/2));

      tick();
    }
  }

  void bounceY(float bound) {
    last.y = bound + gElasticity * (position.y - last.y);
    position.y  = bound;
  }

  void bounceX(float bound) {
    last.x = bound + gElasticity * (position.x - last.x);
    position.x  = bound;
  }

  void reset(PVector t) {
    position    = t;
    last.x = position.x;
    last.y = position.y;
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Circle Collision</title>
      <link>https://forum.processing.org/two/discussion/26799/circle-collision</link>
      <pubDate>Tue, 13 Mar 2018 03:08:50 +0000</pubDate>
      <dc:creator>Marmite</dc:creator>
      <guid isPermaLink="false">26799@/two/discussions</guid>
      <description><![CDATA[<p>I'm having some real trouble getting my two Classes to acknowledge one another and create some sort of collision physics.
i'll post my code below, just wondering if there's a way in which this can be achieved. The game is based on table top football, so the ball should be reacting to the 'player' by moving in the opposite direction once they collide, both on the X and Y axis.</p>

<p>Thanks in advance</p>

<pre><code>        Ball ball;
Timer startTimer;
Blue blue;
Red red;


void setup () {
  size (1200, 800);
  frameRate(100);
  startTimer = new Timer (0);

  ball = new Ball (600, 400,15); 
  blue  = new Blue (400, 230, 50,0,0,255);

  red = new Red (800, 230, 50, 200, 255, 0, 0);
}












    void draw () {
      drawPitch ();
      ball.display();
      ball.update();
      ball.bCollision();


      startTimer.countUp();

      blue.displayBlue();
      blue.moveBlue();



      fill(0);
      textSize (20);
      text (startTimer.getTime(), 575, 20);


      println (mouseX, mouseY);
    }














    void drawPitch () {
      background (#4BAF22);

      //SIDELINES 
      strokeWeight (5);
      stroke(255);
      line (50, 780, 1150, 780);
      line (50, 20, 1150, 20);
      //GOAL LINES
      line (50, 20, 50, 780);
      line (1150, 780, 1150, 20);
      //CENTER LINE
      line (600, 20, 600, 780);
      //CENTER ELLIPSE
      fill (255);
      ellipse (600, 400, 20, 20);
      //CENTER BOX (CIRCLE)
      noFill();
      ellipse (600, 400, 250, 250);
      //REDTEAM BOX 
      line (50, 120, 280, 120);
      line (50, 680, 280, 680);
      line (280, 120, 280, 680);
      //REDTEAM INNER BOX
      line (50, 275, 120, 275);
      line (50, 525, 120, 525);
      line (120, 275, 120, 525);
      //REDTEAM PENATLY SPOT
      fill(255);
      ellipse (200, 400, 20, 20);
      //REDTEAM GOAL
      noFill();
      rect (0, 337, 50, 124);
      //REDTEAM SEMICIRCLE
      arc (280, 400, 125, 200, -HALF_PI, HALF_PI);
      //BLUETEAM BOX
      line (1150, 120, 925, 120);
      line (1150, 680, 925, 680);
      line (925, 120, 925, 680);
      //BLUETEAM INNER BOX
      line (1080, 275, 1150, 275);
      line (1080, 525, 1150, 525);
      line (1080, 275, 1080, 525);
      //BLUETEAM PENALTY SPOT
      fill(255);
      //BLUETEAM GOAL
      ellipse (1000, 400, 20, 20);
      noFill();
      rect(1150, 337, 1200, 124);
      //BLUETEAM SEMICIRCLE
      arc (925, 400, 125, 200, HALF_PI, PI+PI-HALF_PI);
      //CORNERS
      arc (50, 20, 70, 75, -QUARTER_PI+QUARTER_PI, QUARTER_PI+QUARTER_PI);
      arc (50, 780, 70, 75, -HALF_PI, PI-PI);
      arc (1150, 780, 70, 75, PI, PI+QUARTER_PI+QUARTER_PI);
      arc (1150, 20, 70, 75, HALF_PI, PI);
    }


    class Ball {

      PVector pos1 = new PVector (0, 0);
      PVector velo;

      float radius;
      float m;

      Ball (float x, float y, float r_) {
        pos1 = new PVector (x, y);
        velo = PVector.random2D();
        velo.mult(3);
        radius = r_;
        m = radius * .1;
      }

      void update () {
        pos1.add(velo);
      }




      void pCollision () {

      } 


      void bCollision () {

        if (pos1.x &gt; width-60) { //RIGHT WALL
          pos1.x = width-60;
          velo.x *= -1;
        } else if (pos1.x &lt; 60 ) { //LEFT WALL
          pos1.x = 60;
          velo.x *= -1;
        } else if (pos1.y &gt; height-35) { //BOTTOM WALL
          pos1.y = height-35;
          velo.y *= -1;
        } else if (pos1.y &lt;40) { //TOP WALL
          pos1.y = 40;
          velo.y *= -1;
        }
      }




          void display () {
            noStroke();
            fill(0);
            ellipse (pos.x, pos.y, radius*2, radius*2);
          }
    }


        class Blue {

      PVector pos;
      float Size = 50;
      float Speed = 0.1;
      float radius;
      color Colour = color (0, 0, 255);


      Blue (float x, float y, float r_,  int red, int green, int blue) {
        pos = new PVector(x, y);
        radius = r_;
        Colour = color (red, green, blue);
      }

      void displayBlue () {
        fill (Colour);
        strokeWeight (2);
        stroke(255);
        ellipse(pos.x, pos.y, radius, radius);
      }


      void moveBlue () {

        if ((keyPressed == true) &amp;&amp; (key == 'a')) {
          pos.x = pos.x -3;
        }

        if ((keyPressed == true) &amp;&amp; (key == 'd')) {
          pos.x = pos.x +3;
        }

        if ((keyPressed == true) &amp;&amp; (key == 'w')) {
          pos.y = pos.y -3;
        }

        if ((keyPressed == true) &amp;&amp; (key == 's')) {
          pos.y = pos.y +3;
        }
      }
    }

    class Red {
      float xPos;
      float yPos;
      float Size = 50;
      float Speed = 0.1;
      color Colour = color (0, 0, 255);
      PVector Direction;

      Red (float x, float y, float size, float speed, int red, int green, int blue) {
        yPos = y;
        xPos = x;
        Size = size;
        Speed = speed;
        Colour = color (red, green, blue);
      }

      void display () {
        fill (Colour);
        strokeWeight (2);
        stroke(255);
        ellipse(xPos, yPos, Size, Size);
      }

      void move () {

        if ((keyPressed == true) &amp;&amp; (key == 'a')) {
          xPos = xPos -2;
        }

        if ((keyPressed == true) &amp;&amp; (key == 'd')) {
          xPos = xPos +2;
        }

        if ((keyPressed == true) &amp;&amp; (key == 'w')) {
          yPos = yPos -2;
        }

        if ((keyPressed == true) &amp;&amp; (key == 's')) {
          yPos = yPos +2;
        }
      }
    }

    class Timer {

      float Time;  //ATTRIBUTES



      Timer (float set) { //CONSTRUCTOR
        Time = set;
      }

      float getTime() { //RETURNS CURRENT TIME
        return(Time);
      }

      void setTime (float set) {  // SET TIMER
        Time  = set;
      }
      void countUp () {    //UPDATE TIMER +
        Time += 1/frameRate;
      }
      void countDown () {
        Time -=1/frameRate;
      }
    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>Collisions/ End Game</title>
      <link>https://forum.processing.org/two/discussion/26823/collisions-end-game</link>
      <pubDate>Wed, 14 Mar 2018 00:02:16 +0000</pubDate>
      <dc:creator>mnoel</dc:creator>
      <guid isPermaLink="false">26823@/two/discussions</guid>
      <description><![CDATA[<p>I got somewhat far with my code, in my eyes, and I have one slight problem. I wanted to make my game end when my white character guy touches any of the fast moving red rectangles. The rectangles were supposed to move side to side, but what I have now will do. I also wanted to collect coins or some sort of rewards, (like pac-man) and then the game will finish. However, since I haven't been able to figure out the whole overlap/distance thing I'm not even sure if that is possible. Does any one have an example to explain this to me, or what I have to add to make this work?  I've been trying for a whole week and nothing has worked yet. Thank you.</p>

<pre><code>PFont title;
int menu;
int sub_cnt;
int sub_cntt;
int sub_cnttt;
int earthsize = 30;
int earth = 200;
int x=0;
int y=0;
float a,b, s=8;
boolean fireNow = false;
float xPos = 600;
float xxPos = 600;
float xxxPos = 600;
float xxxxPos = 600;
int rad = 40;
int xdirection = 1;
int ydirection = 1;
float xspeed = 2.8; 
float yspeed= 2.2;
float yPos;







//float rectX;
//float rectY;




void setup () {
  size(700, 700);
  title = createFont ("font", 28, true);
  menu = 1;
  background(0, 0, 30);


}

void draw () {

  if (menu == 1) {
    ellipse (random (width), random (height), random (4), random (4));
    textAlign(CENTER);
    textSize(112);
    text ("SEPTUA", 350, 300);
    textSize(30);
    text("press any key to start", 350, 325);
  }
  if (menu == 2) {
    background (0, 0, 30);
    //earth color
    fill(25, 54, 76);
    ellipse(earth, earth + 98,earthsize, earthsize);
    //rectangle (text box)
    fill(0, 0, 60);
    rect (0, 500, 700, 700);
    //text inside the box
    textSize(17);
    fill(255, 255, 255);
    String msg = "IN THE YEAR 3064, EARTH HAS BEEN CONSUMED BY A PLAGUE.";
    text (msg.substring(0,constrain(int(sub_cnt/5),0,msg.length())), 300, 560);
    sub_cnt++;
    if( sub_cnt == 12*msg.length() ){
      menu = 3;
    }
  }
  if ( menu == 3 ) {
    background(0,0,30);
    //earth animation (how it expands)
    fill(25, 54, 76);
    earthsize = earthsize + 1;
    ellipse(earth, earth+98, earthsize, earthsize);
    //text box
    fill(0,0,60);
    rect (0,500,700,700);
    textSize (17);
    fill(255, 255, 255);
    String msg = "THE HUMAN POPULATION HAS DECREASED BY 64%" ;
    text (msg.substring(0, constrain(int(sub_cntt/5), 0,msg.length())), 250, 560);
    sub_cntt++;
    if (sub_cntt == 12*msg.length () ){
      menu= 4;
    }
  }

  if (menu ==4) {

      background(0);
      text("You're going on a mission to retrieve the cure", width/2, height/2);
      text("Press Z to continue", width/2, height/2-20);
      if(keyPressed==true){
        if (key=='z' || key == 'Z'){
          menu = 6;
        }
      }
      if (keyPressed == true){
      menu= 5;
      }
    }




    if (menu ==5){
     ellipse (random (width), random (height), random (4), random (4));
      background (0, 0, 30);
       fill(220,220,220);
      rect(0, 400, 700, 300);
      //this next code is our sprite....
      fill(255, 255, 255);
      rect(x, y, 40, 40);

      //this next code will be our enemy

      //enemy # 1
      fill(160, 0, 0);
     // xPos = xPos - 1;
      rect(xPos - 20, 200, 40, 40);
      //enemy # 2
      fill(160, 0, 0);
      rect(xxPos - 20, 300, 40, 40);

      fill(160, 0, 0);
      rect(xxxPos-20, 400, 40, 40);

      fill(160, 0, 0);
      rect(xxxxPos-20, 500, 40, 40);




     // xPos = xPos + (xspeed * xdirection);
     // yPos = yPos + (yspeed * ydirection);
     // if(xPos&gt; width-rad|| xPos &lt; rad){
     //  xdirection = -1;
    //  }

    //  if (yPos &gt; height-rad || yPos &lt; rad) {
   // ydirection *= -1;
  //}

  if (xPos&lt;700){
    xPos= xPos +10;
  }
  else if (xPos-30&gt;600){
    xPos=xPos *-5;
  }

   if (xxPos&lt;700){
   xxPos=xxPos +10;
  }
  else if (xPos-40&gt;200){
    xxPos=xxPos *-5;

  }

       if (xxxPos&lt;700){
  xxxPos=xxxPos+10;
  }
  else if (xxxPos-30&gt;200);
   xxxPos=xxxPos*-5;

  }

  if (xxxxPos&lt;700){
    xxxxPos=xxxxPos+10;
  }
  else if(xxxxPos-30&gt;200){
    xxxxPos=xxxxPos*-5;
  }












      if (fireNow==true){
        if(a &lt; height &amp;&amp; b &lt; width){
          fill(0,191,255);
          rect(a, b, 45, 10);
          a = a+x;
          b = b+(s);
        } else {
          fireNow = false;

        }


      }
    }




void keyPressed() {
  if (menu == 1) {
    menu = 2;
    sub_cnt = 0;
  }
  if (menu == 3) {
    menu = 4;
    sub_cnt = 0;
 }
 if(menu == 4) {
   menu = 5;
   sub_cnt = 0;
 }




   if (key == CODED) {
     if (keyCode == UP) {
       y-= 4;
     } else if (keyCode == DOWN) {
       y += 4;
     }
     else if (keyCode ==LEFT) {
       x-= 4;
     }
     else if (keyCode ==RIGHT) {
       x+= 4;
     }
     else if (keyCode == SHIFT) {
       y -= 60;
}
}


    }



void keyReleased() {
    if (key ==TAB){
      fireNow=true;
      a=x;
      b=y;
    }
  }
</code></pre>
]]></description>
   </item>
   <item>
      <title>How do I have objects in an ArrayList collide with one another?</title>
      <link>https://forum.processing.org/two/discussion/20651/how-do-i-have-objects-in-an-arraylist-collide-with-one-another</link>
      <pubDate>Sun, 05 Feb 2017 06:13:17 +0000</pubDate>
      <dc:creator>theZetoranian</dc:creator>
      <guid isPermaLink="false">20651@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to make a relatively simple little project for fun. In it, you can click your mouse to have a circle appear. That circle will grow until it collides with a wall or another circle, then shrink. You can keep clicking in different places to add new circles. Unfortunately, I can get everything but the circle-to-circle collision to work.</p>

<p>Here is my code:</p>

<pre><code>ArrayList&lt;Circle&gt; circleArray = new ArrayList&lt;Circle&gt;();
int circleCount = 0;

void setup()
{
  size(300, 300);
}

void draw()
{
  background(0);

  for (int i = 0; i &lt; circleCount; i++)
  {
    circleArray.get(i).display();
    circleArray.get(i).update();
    circleArray.get(i).collideWall();

    //for (int j = i+1; j &lt; circleCount; j++)
    //{
    //  circleArray.get(i).collideCircle(circleArray.get(i));
    //}
  }
}


class Circle
{
  int x, y;
  int circumference;
  int radius;
  int growSpeed;
  boolean isGrowing;
  color col;

  Circle(int tempX, int tempY)
  {
    x = tempX;
    y = tempY;

    circumference = 10;
    radius = circumference/2;
    growSpeed = 1;
    isGrowing = true;
    col = color(random(100, 255), random(100, 255), random(100, 255));
  }

  void display()
  {
    // Draw circle:
    noStroke();
    fill(col);
    ellipse(x, y, circumference, circumference);
  }

  void update()
  {
    // Alternate growing and shrinking:
    if (isGrowing)
      circumference += growSpeed;
    else
      circumference -= growSpeed;

    // Redefine radius:
    radius = circumference/2;
  }

  void collideWall()
  {
    // Wall collision detection:
    if (radius &gt; dist(x, y, x, 0) || radius &gt; dist(x, y, x, height)
     || radius &gt; dist(x, y, 0, y) || radius &gt; dist(x, y, width, y))
      isGrowing = false;
    else if (circumference &lt; 20)
      isGrowing = true;
  }

  void collideCircle(Circle other)
  {
    // Other circle collision detection:
    if (radius + other.radius &gt; dist(x, y, other.x, other.y))
      isGrowing = false;
    else if (circumference &lt; 20)
      isGrowing = true;
  }
}


void mouseClicked()
{
  circleArray.add(new Circle(mouseX, mouseY));
  circleCount++;
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Best way to make world borders for a game.</title>
      <link>https://forum.processing.org/two/discussion/26345/best-way-to-make-world-borders-for-a-game</link>
      <pubDate>Wed, 14 Feb 2018 11:11:04 +0000</pubDate>
      <dc:creator>InferNova</dc:creator>
      <guid isPermaLink="false">26345@/two/discussions</guid>
      <description><![CDATA[<p>Hey guys so I play around with coding games but I am wondering what the best way to set boundaries for a player, the way I use now is using co-ordinates points when the player reaches them he can't pass but it becomes excruciating for bigger projects any ideas ? (I was looking at the Sprites library and noticed that the tank demo uses colors then overlaid with the map and I presume when the player interacts with those colors it triggers a certain action but still not sure how to use that).</p>
]]></description>
   </item>
   <item>
      <title>Redirection object after collision</title>
      <link>https://forum.processing.org/two/discussion/26577/redirection-object-after-collision</link>
      <pubDate>Wed, 28 Feb 2018 11:30:07 +0000</pubDate>
      <dc:creator>Zmcarthur</dc:creator>
      <guid isPermaLink="false">26577@/two/discussions</guid>
      <description><![CDATA[<pre><code>let bubble;
let targets = [];

function setup() {
    createCanvas(500, 700);
    bubble = new Bubble();
        for (let i = 0; i &lt; 10; i++) {
            targets[i] = new Target;
        }
}

function draw() {
    background(0);
    bubble.show();
    bubble.move();
    for (let i = 0; i &lt; targets.length;  i++) {
        targets[i].show();
        if (targets[i].intersects(bubble) == true) {
            console.log('True!');
        }
    }
}

function Target() {
        this.x = random(50, 450);
        this.y = random(50, 650);
        this.r = 50;
        this.val = floor(random(1, 100));

    this.show = function() {
        fill(255);
        rectMode(CENTER);
        rect(this.x,this.y,this.r,this.r);
        fill(0);
        textSize(18);
        textAlign(CENTER, CENTER);
        text(this.val, this.x, this.y);
    }
    this.intersects = function(other) {
            dx = this.x - other.x;
            dy = this.y - other.y;
            d = sqrt(dx * dx + dy * dy);
            minDistance = this.r + other.r;
            if (d &lt; minDistance) {
                return true;
            } else {
                return false;
            }
        }
}


function Bubble() {
            this.x = 50;
            this.y = 50;
            this.r = 10;
            this.xspeed = 10;
            this.yspeed = 7;

        this.move = function() {

            if (this.x &gt; width || this.x &lt; 0) {
                this.xspeed = this.xspeed * -1;
            }

            if (this.y &gt; height || this.y &lt; 0) {
                this.yspeed = this.yspeed * -1;
            }

             this.x = this.x + this.xspeed;
             this.y = this.y + this.yspeed;
            }
        this.show = function() {
            noStroke();
            fill(255);
            ellipse(this.x,this.y,this.r*2,this.r*2);
        }
    }
</code></pre>

<p>So, basically i found a way to calculate whether or not a collision occurred with the bubble and the targets.  Using the Pythagorean theorem to calculate d (the distance) and comparing it to the minDistance, which i've said is whether or not the radii have crossed.  I've confirmed this is working by console.logging 'True!' if that occurs and it works.  Where I'm stumped is getting the ball to bounce correctly.  I'm assuming that i need to calculate the angle of incidence and use that to generate a new angle off of the target.  Foolishly i was using xspeed = xspeed * - 1 and yspeed = yspeed * -1.  That changes the direction completely and isn't a "bounce" it's simply a reversal of the direction.</p>
]]></description>
   </item>
   <item>
      <title>How to fix the collision bug?</title>
      <link>https://forum.processing.org/two/discussion/26584/how-to-fix-the-collision-bug</link>
      <pubDate>Wed, 28 Feb 2018 19:48:45 +0000</pubDate>
      <dc:creator>jkr137</dc:creator>
      <guid isPermaLink="false">26584@/two/discussions</guid>
      <description><![CDATA[<p>Hello guys! The bouncing ball (Puck) stucks sometimes in the other ball (Player). Is there a fix for that? What did I do wrong?
Thank's in advance!</p>

<pre><code>Puck puck;
Player player;

void setup() {
  puck = new Puck (width/2, height/2, 2.5, 2.5, 0, 0, 60);
  size (500, 500);
}

void draw() {
  player = new Player (width/2, height/2, 75);
  background(0);
  puck.update();
  puck.display();
  player.update();
  player.display();
  player.checkCollision(puck);
}

class Player {

  int size;
  float x;
  float y;

  PVector playerPosition;
  PVector playerDirection;

  Player(float x, float y, int size) {
    this.size = size;
    this.x = x;
    this.y = y;
    playerPosition = new PVector(x, y);
    playerDirection = new PVector(mouseX-pmouseX, mouseY-pmouseY);
  }

  void display() {
    pushStyle();
    noStroke();
    fill(255, 0, 0);
    ellipse (x, y, size, size);
    popStyle();
  }

  void update() {
    x = mouseX;
    y = mouseY;


  }
  void checkCollision(Puck P) {
    if (dist(x, y, P.xpos, P.ypos) &lt;= (size/2)+(P.size/2+1)) { //x,y == Player Position; P.x, P.y == Puck Position
      println("collision");

      P.xdirection = playerDirection.x;
      P.ydirection = playerDirection.y;

      //P.xpos = x + size/2 + P.size/2;
      //P.ypos = y + size/2 + P.size/2;
    } else {
      println(" ");
    }
  }
}

class Puck {

  int size;
  float xpos, ypos;
  float xspeed;
  float yspeed;
  float xdirection, ydirection;


  Puck(float xpos, float ypos, float xspeed, float yspeed, float xdirection, float ydirection, int size) {
    this.xpos = xpos;
    this.ypos = ypos;
    this.xspeed = xspeed;
    this.yspeed = yspeed;
    this.xdirection = xdirection;
    this.ydirection = ydirection;
    this.size = size;
  }


  void display() {
    pushStyle();
    noStroke();
    ellipse(xpos, ypos, size, size);
    popStyle();
  }

  void update() {
    xpos = xpos + (xspeed * xdirection);
    ypos = ypos + (yspeed * ydirection);

    if (xpos &gt; width-size/2 || xpos &lt; 0+size/2) {
      xdirection = -xdirection;
    }
    if (ypos &gt; height-size/2 || ypos &lt; 0+size/2) {
      ydirection = -ydirection;
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How can i get the ball to bounce properly</title>
      <link>https://forum.processing.org/two/discussion/26329/how-can-i-get-the-ball-to-bounce-properly</link>
      <pubDate>Tue, 13 Feb 2018 04:18:42 +0000</pubDate>
      <dc:creator>indigopolo</dc:creator>
      <guid isPermaLink="false">26329@/two/discussions</guid>
      <description><![CDATA[<p>Hello! Im trying to get the ball to bounce off the center circle and the edges of the canvas. I've been successful with the edges but i'm having some issues with the circle in the center. Cant figure out how to get the ball to bounce off naturally then right back to where it was. I see that examples that are somewhat similar to this use vectors. I do not want to use vectors because i am very new to programming and haven't been taught how to use them. I would like to figure this out using trig but don't know how to approach it</p>

<p>GIF: <a href="https://gyazo.com/ffe4cffa5decbbb162c5ef06dfa5c1bb" target="_blank" rel="nofollow">https://gyazo.com/ffe4cffa5decbbb162c5ef06dfa5c1bb</a></p>

<p>code: <a href="https://pastebin.com/DFHYA1wM" target="_blank" rel="nofollow">https://pastebin.com/DFHYA1wM</a></p>
]]></description>
   </item>
   <item>
      <title>Target shooting game.</title>
      <link>https://forum.processing.org/two/discussion/26156/target-shooting-game</link>
      <pubDate>Sun, 28 Jan 2018 20:05:49 +0000</pubDate>
      <dc:creator>mickeyj13</dc:creator>
      <guid isPermaLink="false">26156@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I have a project where we have to make a target shooting game. however, I can't figure out how to work the dist function to figure out if the two circles are touching.</p>

<p>Heres the main class where I am having issues. Thanks in advance for any help</p>

<p>[code below]</p>
]]></description>
   </item>
   <item>
      <title>player collision with move ?</title>
      <link>https://forum.processing.org/two/discussion/26128/player-collision-with-move</link>
      <pubDate>Fri, 26 Jan 2018 14:45:28 +0000</pubDate>
      <dc:creator>GeorgeJava</dc:creator>
      <guid isPermaLink="false">26128@/two/discussions</guid>
      <description><![CDATA[<p>How to make player collision with moving player ? only 2 rectangles hits and moving AWSD i dont know how to make it :/ thx to help.</p>
]]></description>
   </item>
   <item>
      <title>Problem with inheritance</title>
      <link>https://forum.processing.org/two/discussion/26019/problem-with-inheritance</link>
      <pubDate>Thu, 18 Jan 2018 11:24:22 +0000</pubDate>
      <dc:creator>Ofelia</dc:creator>
      <guid isPermaLink="false">26019@/two/discussions</guid>
      <description><![CDATA[<p>Hi this is my first post here. 
Im doing a game, i got the superclass object Living_Body, that should manage collissions, health, etc.
Then i got the object Enemy_Swordman that manage the other stuff. This is a sub-class that inherites from living_body</p>

<p>The problem is that i can't get working the method overlaps() from the living_body parent, but if i put the overlaps in the Enemy class then it works, also i can get the health variable from the parent, so the inheritance it is working.</p>

<p>So i read a lot about inheritance, but it seems to bee all ok, but maybe you can help me with this.</p>

<p>When i run the game, the enemys in the map are moving but they dont collission each other, like if the overlaps method dosn't exist.</p>

<p>this is the overlaps method:</p>

<pre><code>void overlaps(Enemy_Swordman other){
  float d = dist(posx, posy, other.posx, other.posy);
  if (d &lt; diameter / 4 + diameter / 4)
  {
    if (posx &gt; other.posx) {
      posx += 3; other.posx -= 3;
    } else {
      posy -= 3; other.posy += 3;
    }
    if (posy &gt; other.posy) {
      posy += 3; other.posy -= 3;
    } else {
      posy -= 3; other.posy += 3;
    }     
  }
}
</code></pre>

<p>Thanks !</p>
]]></description>
   </item>
   <item>
      <title>Gameover section</title>
      <link>https://forum.processing.org/two/discussion/25927/gameover-section</link>
      <pubDate>Fri, 12 Jan 2018 18:33:58 +0000</pubDate>
      <dc:creator>EddyR</dc:creator>
      <guid isPermaLink="false">25927@/two/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>I'm making a game for a school project. The game is like Hundreds of Android and iOS and I have a little trouble with gameover section. I make a class to the circles and I want for each time the mousepressed is active and the mouse is on top of a circle, if that circle touch on other circle the gamestate change.</p>

<p>in my class i have this to the collision:</p>

<pre><code> boolean colide(Circulo c){
   return dist(x,y,c.x,c.y) &lt;= raio + c.raio;
 }
</code></pre>

<p>and this to see when mouse is hover a circle:</p>

<pre><code>   boolean hit(int px, int py){
     if(dist(px,py,x,y)&lt;raio){
       return true;
     }else{
       return false;
     }
   }
</code></pre>

<p>and I try to make something like this:</p>

<pre><code>  for(int j=0; j&lt;numcirc-1; j++){
    for(int i=j+1; i&lt;numcirc; i++){
      if(circulos[i].colide(circulos[j]) &amp;&amp; mousePressed &amp;&amp; circulos[i].hit(mouseX,mouseY)){
        gamestate = 2;
      }
    }
  }
</code></pre>

<p>but only work with some collisions...</p>

<p>hope someone can help me. thanks</p>
]]></description>
   </item>
   <item>
      <title>Need help making a colission</title>
      <link>https://forum.processing.org/two/discussion/25902/need-help-making-a-colission</link>
      <pubDate>Wed, 10 Jan 2018 17:57:11 +0000</pubDate>
      <dc:creator>oogabooga</dc:creator>
      <guid isPermaLink="false">25902@/two/discussions</guid>
      <description><![CDATA[<p>``I'm having trouble detecting the bullet collide with the enemy ship in my shooter game. Here's my code so far. Any ideas on how I can make it work?</p>

<pre><code>PImage background;
ship player= new ship();

int score;
PImage enemypic;
boolean enemyAlive = true;
boolean instructions = true;
boolean level1=true;
boolean gameOver=false;
enemy enemyArr [] = new enemy [11];
color rectColor = color (200);
ArrayList &lt;Bullet&gt; bullets;
void setup() {
size(1000, 750);
bullets = new ArrayList();
background=loadImage("shooterbackground.png");
ship=loadImage("ship.png");
enemypic = loadImage("enemy.png");
frameRate(13);
smooth();
noStroke();
float enemyY = 0;
for (int i = 0; i &lt; 11; i++) {

 enemyArr [i] = new enemy(900, 5 + enemyY);
enemyY +=65;
}
}
void draw() {
  println(frameRate);
image (background, 0, 0, 1000, 750); 

moveAll();
displayAll();
if (instructions) {
 fill(rectColor);
//rect(0, 0, 1000, 750);
fill(0);
text("afdsf", 0, 0);
          }
      if (instructions) {
background(200, 200, 200);
fill(225);
rect(300, 275, 400, 200);
fill(0);
textSize(65);
text("Start", 420, 390);
         }

       if (enemyAlive &amp;&amp; !instructions &amp;&amp; !gameOver) {

   player.display();
for (int i = 0; i &lt; 11; i++) {
  enemyArr[i].update();
  enemyArr[i].display();
}
}

if (playerX &gt; 244) {//player move limits
playerX = 244;
}
if (playerX &lt; 0) {
playerX = 0;
}
if (playerY &lt; -68) {//wrap screen
playerY = 740;
}
if (playerY &gt; 740) {
playerY = -68;
}
}  


void keyPressed() {
 if (key == 'a') {
  playerX -= 8;
 }
  if (key == 'd') {
  playerX+= 8;
 }
  if (key == 'w') {
  playerY -= 8;
 }
  if (key == 's') {
    playerY += 8;
   }
  }




   void mousePressed() {

   if (mouseX &gt; 300 &amp;&amp; mouseX &lt; 700 &amp;&amp;  mouseY &gt; 275 &amp;&amp; mouseY  &lt; 475 &amp;&amp; instructions) {
    instructions = false;
   }
   Bullet temp = new Bullet(playerX+65, playerY+34);
   bullets.add(temp);
   }



public class enemy {
  float health, speed;
  float yPos, xPos;


   enemy(float XPOS, float YPOS) {

    health=5;
     speed=10;
    xPos=XPOS;
    yPos=YPOS;
  }

  public void display() {
    image(enemypic, xPos, yPos, 75, 75);
   }
   public void update() {
     if (enemyAlive) {
     xPos -=7;
        }
   if (xPos &lt; -65) {
    xPos=925;
      }
            }
         }

     class Bullet{//bullet class

       float x=playerX +20;
      float y=playerY;
     float speed;
    Bullet(float tx, float ty){

        x = tx;
       y = ty;
     }
       void display(){

     fill(255, 0,0);
        rect(x,y, 10, 5);
      }
       void move(){

        x += 25;
      }

     void moveAll(){

     for (Bullet temp : bullets){

       temp.move();
        }
       }
       void displayAll(){

       for (Bullet temp : bullets){

           temp.display();
      }
       }

 PImage ship;
    int playerX = 100;
    int playerY = 300;

     public class ship {

 public void display() {
image(ship, playerX, playerY, 75, 75);
        }
    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>2D Physics engine made with Processing</title>
      <link>https://forum.processing.org/two/discussion/25700/2d-physics-engine-made-with-processing</link>
      <pubDate>Sun, 24 Dec 2017 13:41:16 +0000</pubDate>
      <dc:creator>Janglee123</dc:creator>
      <guid isPermaLink="false">25700@/two/discussions</guid>
      <description><![CDATA[<p>I had like to share my own small , simple and not more powerful 2D Physics engine that made in Processing. one year ago I started to learn processing with zero programing experience and started to work on game making to learn programing.</p>

<p>Actually i want to make a game but that require physics . I tried Box2D but i didn't like conversion between coordinates between world and pixels and  less control on body .then i started to make  physics engine and today I want to share my physics engine . it is not powerful as Box2D and need more work to do like to add friction,bounciness,and other things but collision detection and collision response works good . I also want to add continues collision detection for fast objects.</p>

<p>if anyone want to use it . I like to make Documentation for that.</p>

<p>Code : <a rel="nofollow" href="https://github.com/Janglee123/five">Github</a></p>

<p>video : <span class="VideoWrap"><span class="Video YouTube" id="youtube-meTTxhSbdLQ"><span class="VideoPreview"><a href="http://youtube.com/watch?v=meTTxhSbdLQ"><img src="http://img.youtube.com/vi/meTTxhSbdLQ/0.jpg" width="640" height="385" border="0" /></a></span><span class="VideoPlayer"></span></span></span></p>
]]></description>
   </item>
   <item>
      <title>How do I register contact in Processing?</title>
      <link>https://forum.processing.org/two/discussion/25734/how-do-i-register-contact-in-processing</link>
      <pubDate>Wed, 27 Dec 2017 20:28:32 +0000</pubDate>
      <dc:creator>hurrdurrhurr</dc:creator>
      <guid isPermaLink="false">25734@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to develop Galaga in Processing, and I've made progress, but now I need to figure out how to register contact in the game, so that when the bullets hit the enemies they die, and when they touch the ship, the ship is destroyed. How do I do that? I have the files attached to this post. You'll need the Minim Library to run this. Any help is appreciated.</p>

<p><a rel="nofollow" href="http://www.mediafire.com/file/0ot3rfzdob2jzxb/galaga.pde">mediafire.com/file/0ot3rfzdob2jzxb/galaga.pde</a></p>

<p><img src="https://forum.processing.org/two/uploads/imageupload/837/OIVTOMPH7AA8.gif" alt="Bat" title="Bat" />
<img src="https://forum.processing.org/two/uploads/imageupload/101/GLDEKLA5A2N6.png" alt="enemy_boss_a" title="enemy_boss_a" />
<img src="https://forum.processing.org/two/uploads/imageupload/720/XNROPBGHNJY2.png" alt="enemy_boss_b" title="enemy_boss_b" />
<img src="https://forum.processing.org/two/uploads/imageupload/884/E6P7YROM8271.png" alt="enemy_boss_c" title="enemy_boss_c" />
<img src="https://forum.processing.org/two/uploads/imageupload/816/NX22VZEIBSVD.png" alt="enemy_boss_d" title="enemy_boss_d" />
<img src="https://forum.processing.org/two/uploads/imageupload/738/7SDN24K4BJEM.png" alt="enemy_goei_a" title="enemy_goei_a" />
<img src="https://forum.processing.org/two/uploads/imageupload/330/MWEGTYZ0DJ6C.png" alt="enemy_goei_a_combined" title="enemy_goei_a_combined" />
<img src="https://forum.processing.org/two/uploads/imageupload/564/SNNOLHHFLOPG.png" alt="enemy_goei_b" title="enemy_goei_b" />
<img src="https://forum.processing.org/two/uploads/imageupload/875/NWZMU4TSQO11.png" alt="enemy_goei_b_combined" title="enemy_goei_b_combined" />
<img src="https://forum.processing.org/two/uploads/imageupload/606/KF2GN8VTC04I.png" alt="enemy_goei_c" title="enemy_goei_c" />
<img src="https://forum.processing.org/two/uploads/imageupload/676/U6H6TAQVIZ2C.png" alt="enemy_goei_c_combined" title="enemy_goei_c_combined" />
<img src="https://forum.processing.org/two/uploads/imageupload/039/AK3SVJ5A8UDN.png" alt="enemy_goei_d" title="enemy_goei_d" />
<img src="https://forum.processing.org/two/uploads/imageupload/715/1WK7ECAEYZUK.png" alt="enemy_goei_d_combined" title="enemy_goei_d_combined" />
<img src="https://forum.processing.org/two/uploads/imageupload/741/OE5OHQN4E1NK.png" alt="Galaga_Fighter" title="Galaga_Fighter" />
<img src="https://forum.processing.org/two/uploads/imageupload/662/J7AGYI70XF0W.png" alt="king_D2" title="king_D2" />
<img src="https://forum.processing.org/two/uploads/imageupload/847/MJTDZ6ILY5RT.png" alt="king_D3" title="king_D3" />
<img src="https://forum.processing.org/two/uploads/imageupload/809/600PHVG85ALT.png" alt="king_D4" title="king_D4" />
<img src="https://forum.processing.org/two/uploads/imageupload/422/YJ6RUHPCUF4Q.png" alt="king_D5" title="king_D5" /></p>
]]></description>
   </item>
   <item>
      <title>Collision with Abstract Entity</title>
      <link>https://forum.processing.org/two/discussion/25648/collision-with-abstract-entity</link>
      <pubDate>Wed, 20 Dec 2017 02:33:38 +0000</pubDate>
      <dc:creator>Garrett_T</dc:creator>
      <guid isPermaLink="false">25648@/two/discussions</guid>
      <description><![CDATA[<p>I made an L shape, was wondering if I could some how make a collision with it. I used vertexes to make the shape for example.</p>

<pre><code>beginShape();
vertex(20, 20);
vertex(100, 20);
vertex(100, 35);
vertex(35, 35);
vertex(35, 100);
vertex(20, 100);
vertex(20, 20);
endShape();
</code></pre>

<p>This is the method I used up above to make the L shape. Was wondering if I could make a simple collision with it, like this easy game for example.</p>

<pre><code>float bullX = 20;
float bullY = 165;
float targX = 500;
float targY = 150;
float spd = 7;
color targColour;
int countX = 0;
int countY = 0;
int dirCount = 0;
int dir = 1;
void setup () {
  size (800, 400);
  targColour = color (random (100, 255), random (100, 255), random (100, 255));
}
void draw () {
  background (0);
  countX++;
  countY++;
  dirCount++;
  fill (#FF0303);
  rect (bullX, bullY, 20, 5);
  if (countY == 10) {
    countY = 0;
    targY = targY + random (5, 8) * dir;
  }
  if (dirCount &gt; 130) {
    dir = dir * -1;
    dirCount = 0;
  }
  if (targY &lt; 0) targY = 0;
  if (targY &gt; height - 13) targY = height - 13;
  bullX = bullX + spd;

  if (bullX + 20  &gt;= targX &amp;&amp; bullX + 20 &lt;= targX + 50 &amp;&amp; bullY &lt;= targY + 40 &amp;&amp; bullY + 5 &gt;= targY) {
    spd = 0;
    targColour = color (random (100, 255), random (100, 255), random (100, 255));
  }
  fill ( targColour);
  rect (targX, targY, 50, 40);
  if (spd == 0 || bullX &gt; width) {
    bullX = 0;
    spd = 7;
  }
}

void keyPressed() {
  if (key == CODED) { 
    if (keyCode == UP) {
      if (bullY &gt; 0) {
        bullY = bullY - 5;
      }
    }
    if (keyCode == DOWN) {
      if (bullY &lt; height - 5) {
        bullY = bullY + 5;
      }
    }
  }
}
</code></pre>

<p>Thank you in advance for the help!</p>
]]></description>
   </item>
   <item>
      <title>Help appending array and detecting collision?</title>
      <link>https://forum.processing.org/two/discussion/25461/help-appending-array-and-detecting-collision</link>
      <pubDate>Fri, 08 Dec 2017 21:12:01 +0000</pubDate>
      <dc:creator>Cencu</dc:creator>
      <guid isPermaLink="false">25461@/two/discussions</guid>
      <description><![CDATA[<p>Hello, my current code is a racing game, the problem is detecting the collision with rockets. Currently there are  problems</p>

<p>When an object is deleted, the main car also gets deleted</p>

<p>When an object and rocket collide, nothing happens</p>

<p>The rocket array doesn't append</p>

<p>The code doesn't run when I have a number in the rocket array</p>

<p>Can anyone please help, I've been trying to figure this out for a while now with no avail.</p>

<pre><code>  Rocket(float tempx, float tempy, float tempspeed, float tempsizex, float tempsizey, float tempcolor) {
    x = tempx;
    y = tempy;
    sizeX = tempsizex;
    sizeY = tempsizey;
    speed = tempspeed;
    rocketColor = tempcolor;
  }
  //If launched is false, then the rocket will continue moving down the screen, however if it is true
  //then it will not
  void update() {
    if (!launched) {
    y += speed;
    }
  }
  //If powerup is false, and launched is true, then the rocket will fire by holding the mouse key
  void launchspeed() {
    if (!powerup) {
      if(launched) {
   y -= speed*2; 
      }
    }
  }

  void display() {
   fill(255);
    rectMode(CENTER);
    rect(x, y, sizeX, sizeY); 
    if (y &gt;= height+50) {
         // y = -50;

    }
  }
  //If your car comes into contact with the rocket, it will pick it up
  //the boolean powerup becomes true, activating the new location for the rocket, it now follows the car
  void collected(Car car) {
    boolean leftP = (x + sizeX/2 &gt; car.x - car.sizeX/2);
    boolean rightP = (x - sizeX/2 &lt; car.x + car.sizeX/2);
    boolean topP = (y + sizeY/2 &gt; car.y - car.sizeY/2);
    boolean bottomP = (y - sizeY/2 &lt; car.y + car.sizeY/2);

    if (leftP &amp;&amp; rightP &amp;&amp;topP &amp;&amp; bottomP) {
     powerup = true;
    }
  }
  //Follows the car, however, like mentioned in the Car class, it follows the car but not its true Y location to avoid moving the car when shooting the rocket
  void follow(Car car) {
   if (powerup) {
      x = car.p;
      y = car.c;
   }
  }
  //When the boolean is true and the mouse is held, then powerup becomes false adn launched becomes true.
  //When powerup becomes false, the rocket shoots upwards and hits an obstacle
  //When launched becomes true, the original update loop doesnt run, but the launchspeed loop does, causing the rocket to fly upwards
  void shooting() {
   if (mousePressed &amp;&amp; powerup) {
     powerup = false;
     launched = true;
   }
  }


  void hit(Obstacle obstacle) {

    boolean leftH = (x + sizeX/2 &gt; obstacle.x - obstacle.sizeX/2);
    boolean rightH = (x - sizeX/2 &lt; obstacle.x + obstacle.sizeX/2);
    boolean topH = (y + sizeY/2 &gt; obstacle.y - obstacle.sizeY/2);
    boolean bottomH = (y - sizeY/2 &lt; obstacle.y + obstacle.sizeY/2);

    if (leftH &amp;&amp; rightH &amp;&amp; topH &amp;&amp; bottomH) {
      obstacle.sizeX = 0;
      obstacle.sizeY = 0;

    } 
  }

    void timerRocket() {
    //Converts milliseconds to actual seconds
    //int converts millis to integers, minus temp time 
    tC = intervalC+int(millis()/1000)-tempTimeC;

    ////nf formats the numbers into strings, so time = 00, it'll show the string time, and adds 2 zeros
    timeC = nf(tC, 2);
    ////if the seconds equal 6 + car add, then the array appends and another car appears onscreen
    //carAdd starts at 0, and when the first timer reaches 6, it adds another six, so when the timer reaches 12, it adds a car, and the variable carAdd goes to 18
    if (tC == 6 + rocketAdd) {
      //The new object being added to the array, spawns on a random lane
      Rocket o = new Rocket(50 + b*floor(random(0, 5)), -80, speed, 10, 20, color(255, 0, 0));
      rocket = (Rocket[]) append(rocket, o);
      //Timer that adds the cars every six seconds
      rocketAdd +=5;
    }
    }


}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How do I make two objects collide?</title>
      <link>https://forum.processing.org/two/discussion/25404/how-do-i-make-two-objects-collide</link>
      <pubDate>Wed, 06 Dec 2017 00:10:11 +0000</pubDate>
      <dc:creator>kkyl18</dc:creator>
      <guid isPermaLink="false">25404@/two/discussions</guid>
      <description><![CDATA[<p>I'm building a basic paddleball game, and I'm having trouble making the ball bounce when it comes in contact with the rectangle paddle at the bottom of the screen.</p>
]]></description>
   </item>
   <item>
      <title>Colliding images change direction?</title>
      <link>https://forum.processing.org/two/discussion/25278/colliding-images-change-direction</link>
      <pubDate>Wed, 29 Nov 2017 17:36:20 +0000</pubDate>
      <dc:creator>letsgo</dc:creator>
      <guid isPermaLink="false">25278@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to make a program where the image changes direction when two images collide.</p>

<p>I want to use this code but I'm uncertain where I put it? Also what do I substitute for ob1 (object1) and x1 and where do I put the width and height variables?  Thanks</p>

<pre><code> boolean Collision()

  ( ob1 - x1, ob1 -y1, ob1 - w, ob1 - h,
    ob2 - x1, ob2 -y1, ob2 -w, ob2-h)

  return 
  (ob1 - x1 &lt; ob2 - x2 &amp;&amp; ob1 - x2 &gt; ob2 = x1 &amp;&amp;  //insert y variables








//start of code


    class Bouncy 
    {

      int x;
      int y;
      int dx; 
      int dy;

      PImage nw, ne, sw, se;

      Bouncy(int x, int y, int dx, int dy) 
      {
        this.x = x;
        this.y = y;
        this.dx = dx;
        this.dy = dy;
    nw = loadImage("NorthW.png");
    ne = loadImage("NorthE.png");
    sw = loadImage("SouthW.png");
    se = loadImage("SouthE.png");
      }


      void update() 
      {
        render();
        move();
      }

      void render() 
      {
    if (dx == -1 &amp;&amp; dy == -1)
      image(nw,x,y);

    else if (dx == 1 &amp;&amp; dy == -1)
    image(ne,x,y);

    else if (dx == -1 &amp;&amp; dy == 1)
    image(sw,x,y);

    else if (dx == 1 &amp;&amp; dy == 1)
    image(se,x,y);
          }

      void checkCollisions() 
      {
        int edge = 65; // half width of one of the PNG files

        if (y&lt;=(edge-edge)) // hit top border
     dy=1; // switch to moving downwards

    if (y&gt;=500-(edge*2)) // hit bottom border
     dy=-1; // switch to moving upwards

        if (x&lt;=edge-edge) // hit left border
    dx=1; // switch to moving right

    if (x&gt;=500-(edge*2)) // hit right border
    dx=-1;
      }



      void move() 
      {
    checkCollisions();
    x += dx;
    y += dy;

      }
    }

    Bouncy janet,jeff,jerry;

    void setup() 
    {
      size(500,500);
      janet = new Bouncy(10,100,1,-1);
      jeff = new Bouncy(10,150,-1,1);
      jerry = new Bouncy(10,350,1,1);

    }

    void draw() 
    {
      background(255);
      janet.update();
      jeff.update();
      jerry.update();

    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>how to do inetarcction</title>
      <link>https://forum.processing.org/two/discussion/25170/how-to-do-inetarcction</link>
      <pubDate>Thu, 23 Nov 2017 19:13:42 +0000</pubDate>
      <dc:creator>ppp</dc:creator>
      <guid isPermaLink="false">25170@/two/discussions</guid>
      <description><![CDATA[<pre><code>  ArrayList bales;
Bala bala1;

ArrayList soldats;
Soldat soldat1;
boolean matar= false;

void setup() {
size(600, 500);
stroke(255);
fill(255, 255, 255);
bales = new ArrayList();
bala1 = new Bala();

soldats= new ArrayList();
soldat1 = new Soldat();

bales.add( bala1 );
soldats.add (soldat1);
}

void draw() {
background(51);
line(300, 500, mouseX, 2 * mouseY);
strokeWeight(mouseY/10);

for ( int i = 0; i &lt; bales.size(); i++) {
bala1 = (Bala)bales.get(i);
bala1.balesmoviment();

for ( int i = 0; i &lt; bales.size(); i++) {
soldat1 = (Soldat)soldats.get(i);
soldat1.soldatsmoviment();
}
}

if (balapx = soldatpx) {
matar=true; // matar is kill, when bala collision rectangle (soldat=soldier) soldier die (dissapear)


class Bala {
float balapx, balapy;
float balavx, balavy;
Bala() {//creamos una bala
balapx = width/2.0;
balapy = height;
balavx = map(mouseX, 0, width, -10, 10);
balavy = -10;
}
void balesmoviment() {
balapx += balavx;
balapy += balavy;
ellipse( balapx, balapy, 10, 10);
}
//================================================================
class Soldat { // Here the problem, how to make rectangles that move and avance in the map
float soldatpx, soldatpy;
float i;
Soldat() {//creamos unsoldat
soldatpx = width+i;
soldatpy = 0;
}
void soldatsmoviment() {
soldatpx +=i;

rect( soldatpx, balapy, 10, 20);
}
}
}
}


void mousePressed() {
if ( bales.size() &gt; 9 ) bales.remove(0);
bales.add( new Bala() );
}
</code></pre>

<p>Don't do pro things</p>
]]></description>
   </item>
   <item>
      <title>Hit the rectangle and make a counter</title>
      <link>https://forum.processing.org/two/discussion/25093/hit-the-rectangle-and-make-a-counter</link>
      <pubDate>Sun, 19 Nov 2017 16:49:41 +0000</pubDate>
      <dc:creator>Hamzeh_89</dc:creator>
      <guid isPermaLink="false">25093@/two/discussions</guid>
      <description><![CDATA[<p>please i am very beginner of using processing , my question is how to make the balls hit the rectangle ? and make counter for the balls that hit it 
i will be very thankful for your help</p>

<p>here is the code</p>

<pre><code>int totalDots = 80;
Dot[] dots = new Dot[totalDots];

int width, height;
color fillColor;
float diameter = 12.0;
 float rectSize = 100;

void setup() {
    // initialization
    width = 480;
    height = 400;
    size(480, 400);
    // initial fill colour
    fillColor = color(255, 0, 0);
    fill(fillColor);
    noStroke();
    // array of dots
    for (int i = 0; i &lt; totalDots; i++) {
        Dot d = new Dot();
        d.x = random(width);
        d.y = random(height);
        d.vx = random(2.0) - 1.0;
        d.vy = random(2.0) - 1.0;
        dots[i] = d;
    }
    background(0);
};

void draw() {
    fill(0, 70);
    rect(0, 0, width, height);

    float r = 255;
    float g = 255;
    float b = 255;

    for (int i = 0; i &lt; totalDots; i++) {
        r = map(dots[i].x, 0, width, 0, 255);
        g = map(dots[i].y, 0, height, 0, 255);
        fill(r, g, b);
        dots[i].update();
        ellipse(dots[i].x, dots[i].y, diameter, diameter);

        { rect(0, 0, 0, height);
      rect(width-10, mouseY-rectSize/2, 10, rectSize);

        }


    }
};

class Dot {
    float x = 0.0;
    float y = 0.0;
    float vx = 0.0;
    float vy = 0.0;

    void update(){
      // update the velocity
      this.vx += random(2.0) - 1.0;
      this.vx *= .96;
      this.vy += random(2.0) - 1.0;
      this.vy *= .96;
      // update the position
      this.x += this.vx;
      this.y += this.vy;
      // handle boundary collision
      if (this.x &gt; width) { this.x = width; this.vx *= -1.0; }
      if (this.x &lt; 0) { this.x = 0; this.vx *= -1.0; }
      if (this.y &gt; height) { this.y = height; this.vy *= -1.0; }
      if (this.y &lt; 0) { this.y = 0; this.vy *= -1.0; }
    }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>making a Gameover screen when a collision occurs</title>
      <link>https://forum.processing.org/two/discussion/25014/making-a-gameover-screen-when-a-collision-occurs</link>
      <pubDate>Wed, 15 Nov 2017 00:24:32 +0000</pubDate>
      <dc:creator>CSoulo</dc:creator>
      <guid isPermaLink="false">25014@/two/discussions</guid>
      <description><![CDATA[<p>Hey I was wondering how I would be able to go about  making a gameover screen when a collision occurs. Right now i have a static wall which is called wal2 in the code below and i'm using the dist function to detect when my mouse is touching the ellipse and what I want is for it to change  to screen GAMEOVER when the collision is true.</p>

<p><code>void draw() {
  if (screen == MAIN_MENU) {
    MAIN_MENU();
  } else if (screen == INSTRUCTIONS) {
    INSTRUCTIONS();
  } else if (screen == GAMEOVER) {
    GAMEOVER();
  } else if (screen == STORY_1) {
    STORY_1();
  } else if (screen ==  LEVEL_1) {
    LEVEL_1();
  }
}</code>
`
void LEVEL_1() {</p>

<p>// wall collisions//
  boolean collision = true;</p>

<p>if (collision) {
    screen = GAMEOVER;
  } else {
    screen = LEVEL_1;
  }</p>

<p>if (dist(wal2_x, wal2_y, mouseX, mouseY)&lt;=162) {
     collision = true;
  } 
}`</p>
]]></description>
   </item>
   </channel>
</rss>