<?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/p3/feed.rss?Tag=%23collisions</link>
      <pubDate>Sun, 08 Aug 2021 19:35:49 +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 Detection with Squares/Rectangles</title>
      <link>https://forum.processing.org/two/discussion/22630/collision-detection-with-squares-rectangles</link>
      <pubDate>Wed, 17 May 2017 06:11:18 +0000</pubDate>
      <dc:creator>Dreeww</dc:creator>
      <guid isPermaLink="false">22630@/two/discussions</guid>
      <description><![CDATA[<p>Hello, trying to figure out how to do collision detection with squares/Rectangles unsure how to figure out. I have a base idea on how to find the distance, however i'm unsure on what to compare it to.</p>

<pre><code>void collisionDetection(){
  for(int i = Walls.size()-1; i&gt;=0 ; i--){
    Wall current = Walls.get(i);

    for(int b1 = tank1Bullet.size()-1; b1&gt;=0; b1--){
      tank1Bullet currentBullet = tank1Bullet.get(b1);
      //this is problem the problem
      if(dist(currentBullet.x,currentBullet.y,current.X,current.Y)&lt;(current.Width)){
      tank1Bullet.remove(b1);
      //Walls.remove(i);
      }
    }

class Wall{

  float Width;
  float Height;
  float X;
  float Y;
  Wall(float x,float y,float w, float h){
    Width = w;
    Height = h;
    X = x;
    Y = y;

  }
  void display(){
   fill(0);
   rect(X,Y,Width,Height);
  }

}

  class tank1Bullet extends PVector{
    PVector location;
    float rotation, bullet_Speed;

    tank1Bullet(){
        location = new PVector(tank1Location.x, tank1Location.y);
        rotation = tank1Rotate;
        bullet_Speed = 20;
    }
    void update(){

    location.x = location.x  + sin(rotation)*bullet_Speed;
    location.y = location.y  - cos(rotation)*bullet_Speed;

    //condition to removes the bullet from the arrayList if it is off the screen
    if (location.x &gt; 0 &amp;&amp; location.x &lt; width+500 &amp;&amp; location.y &gt; 0 &amp;&amp; location.x &lt; height+500) {

    }
    else {
      tank1Bullet.remove(b1);
    }

  }
    void draw_Bullet(){
      ellipse(location.x,location.y , 10, 10);
    }
  }
</code></pre>
]]></description>
   </item>
   <item>
      <title>collision with lines</title>
      <link>https://forum.processing.org/two/discussion/22488/collision-with-lines</link>
      <pubDate>Tue, 09 May 2017 18:20:57 +0000</pubDate>
      <dc:creator>brendonnnnnn</dc:creator>
      <guid isPermaLink="false">22488@/two/discussions</guid>
      <description><![CDATA[<p>Hello I am a processing noob, i cant get a line to collide with my ball and this is my final project for my class i really need some help please.</p>
]]></description>
   </item>
   <item>
      <title>How to make a shape teleport when it hits another</title>
      <link>https://forum.processing.org/two/discussion/22503/how-to-make-a-shape-teleport-when-it-hits-another</link>
      <pubDate>Wed, 10 May 2017 16:59:41 +0000</pubDate>
      <dc:creator>zero000</dc:creator>
      <guid isPermaLink="false">22503@/two/discussions</guid>
      <description><![CDATA[<p>I'm working on a class project(frogger) and i want it so when the red squares hit the green one, the green one teleport back to the start(preferable with an array), same with when it hits the blue rectangle.
here is my code:</p>

<pre><code>int x = 250;
int y = 475;

int xPos=225;
int xDir=3;
int xPos2=200;
int xDir2=2;
int xPos3=25;
int xDir3=1;
int xPos4=25;
int xDir4=2;
void setup()
{
  size(500,500);

}
  void car(){
    fill(255,0,0);
    rect(xPos, 150, 25, 25);
  xPos=xPos+xDir;
  if (xPos&gt;width || xPos&lt;20)
  {
    xDir=-xDir;
  }
  rect(xPos2,275,25,25);
  xPos2=xPos2+xDir2;
  if (xPos2&gt;width || xPos2&lt;20)
  {
    xDir2=-xDir2;
  }
  }
  void log() {


   fill(128,64,0);
   rect(xPos3, 80, 125, 35);

  xPos3=xPos3+xDir3;
  if (xPos3&gt;width || xPos3&lt;20)
  {
    xDir3=-xDir3;
  }
  fill(128,64,0);
   rect(xPos4, 42, 125, 37);

  xPos4=xPos4+xDir4;
  if (xPos4&gt;width || xPos4&lt;20)
  {
    xDir4=-xDir4;
  }
  }
void draw()
{
    fill(0);
  rect(0,115,500,325);

  fill(0,128,0);
  rect(0,350,500,375);
  fill(0,0,255);
  rect(0,0,500,125);
  fill(1,50,32);
  rect(0,0,500,25);
  for(int a=25; a &lt; width; a=a+100)
{
  fill(255,255,0);
  rect(a,215,45,10);}

  log();

  fill(0,255,0);
  rect(x,y,25,24);
  car();

}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
      y -= 41;
    } else if (keyCode == DOWN) {
      y += 41;
    } else if (keyCode == LEFT) {
      x -= 41;
    } else if (keyCode == RIGHT) {
      x += 41;
    }  
  } 
}
</code></pre>

<p>it would also be nice if you could tell me how to make the game better.</p>
]]></description>
   </item>
   <item>
      <title>Why doesn't this ball move?</title>
      <link>https://forum.processing.org/two/discussion/22282/why-doesn-t-this-ball-move</link>
      <pubDate>Sat, 29 Apr 2017 07:49:13 +0000</pubDate>
      <dc:creator>GottfriedBoehm</dc:creator>
      <guid isPermaLink="false">22282@/two/discussions</guid>
      <description><![CDATA[<pre><code>Ball bl;

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

void draw () {
  background(0);
  bl = new Ball(300, 150);
  bl.display();
}

class Ball {

  float x;
  float y;
  float diameter = 100;
  float xspeed = 1;
  float yspeed = 1;

  Ball (float tempx, float tempy) {
    x = tempx;
    y = tempy;
  }

  void display() {
    x=x+xspeed;
    y=y+yspeed;
    if ( y &gt; height-diameter/2  || y &lt; diameter/2) {
      yspeed = -yspeed;
    } else if ( x &gt; width-diameter/2 || x &lt; diameter/2 ) {
      xspeed = -xspeed;
    }
    ellipse (x, y, diameter, diameter);
  }
}
</code></pre>

<p>Thanks for help from now. ^^</p>
]]></description>
   </item>
   <item>
      <title>How to check whether mouse pointer is currently on the line drawn inside Canvas or not?</title>
      <link>https://forum.processing.org/two/discussion/22122/how-to-check-whether-mouse-pointer-is-currently-on-the-line-drawn-inside-canvas-or-not</link>
      <pubDate>Fri, 21 Apr 2017 11:10:07 +0000</pubDate>
      <dc:creator>darshansonagara</dc:creator>
      <guid isPermaLink="false">22122@/two/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>I am doing one visualization task, where I am drawing graph inside canvas through connecting edges as line function. Now I want to check whether my mouse is over the line or not. If it is then I would like to show the distance between this two vertices. Can anyone tell me here how can I do it with P5.js ?</p>
]]></description>
   </item>
   <item>
      <title>[SOLVED] Collision detection when using translate?</title>
      <link>https://forum.processing.org/two/discussion/21904/solved-collision-detection-when-using-translate</link>
      <pubDate>Sun, 09 Apr 2017 14:48:55 +0000</pubDate>
      <dc:creator>swhizzle</dc:creator>
      <guid isPermaLink="false">21904@/two/discussions</guid>
      <description><![CDATA[<p>Hey guys, so I've been stuck for a while now..</p>

<p>I am using translate to focus the view on the player. The problem is, I'm not sure how I would then calculate the distance between the player and the objects in the world if the point of origin is always the player's x and y.</p>

<p>Without translating I would just use "dist()" to see if there's any overlap, but I'm not sure how to apply this when using translate().</p>

<p>I tried in vain to draw the objects and then calculate their position in separate variables relative to the player but it was a complete headache; I'm convinced there is an easier way.</p>

<p>I understand there are libraries for games and collisions but that wouldn't be any fun! :)</p>

<p>I've stripped my code to the relative parts and put it here:
<a href="http://codepen.io/swhizzle/full/OpKyJQ/" target="_blank" rel="nofollow">http://codepen.io/swhizzle/full/OpKyJQ/</a></p>

<p>Any advice will be really appreciated!</p>

<p>Steve</p>
]]></description>
   </item>
   <item>
      <title>Removing an object in an array upon collision</title>
      <link>https://forum.processing.org/two/discussion/21902/removing-an-object-in-an-array-upon-collision</link>
      <pubDate>Sun, 09 Apr 2017 10:45:14 +0000</pubDate>
      <dc:creator>Dreeww</dc:creator>
      <guid isPermaLink="false">21902@/two/discussions</guid>
      <description><![CDATA[<p>Hi, i was wondering if anyone could help in regards to in regards collision detection
    void collisionDetection(){
      for(int i = 0; i &lt; aesteroidBelt.size(); i++){
        Aesteroid current = aesteroidBelt.get(i);</p>

<pre><code>    for(int b = 0; b &lt; bullets.size(); b++)
    {
      Bullet currentBullet = bullets.get(b);
        if(dist()){
        bullets.remove(b);
        aesteroidBelt.remove(i);
</code></pre>

<p>Cant figure out the values to use for keeps telling me that booleans are not accept and only floats are.</p>

<p>These are the variables i have</p>

<pre><code>int n=12;

PVector ship;

//Initilaizes Variables used
float bulletSpeed = 5;
float angle = 0;
float targetAngle = 0;
float rotation = 0.05;
float xSpeed = random(0.6, 2.5);
float ySpeed = random(0.6, 2.5);
float x = random(1, 590);
float y = random(1, 390);
</code></pre>

<p>My Aesteroids are also a random PVector every time you run this code(part which i am stuck on)</p>
]]></description>
   </item>
   <item>
      <title>SyntaxError: Unexpected identifier  Expected '}' to end an object literal.</title>
      <link>https://forum.processing.org/two/discussion/21752/syntaxerror-unexpected-identifier-expected-to-end-an-object-literal</link>
      <pubDate>Sun, 02 Apr 2017 00:09:42 +0000</pubDate>
      <dc:creator>tim50</dc:creator>
      <guid isPermaLink="false">21752@/two/discussions</guid>
      <description><![CDATA[<p>I got rid of the code that didn't work, but with the intersect function below.... I tried to put it in the var bubble area like the display: function that is in there.. but I would get the error in the safari console as the title of this post above.</p>

<p>In other words, does any one know why I can't put this intersect in the var bubble area like this:</p>

<pre><code>intersect: function() {
  var d = dist(this.x, this.y, ellip.x, ellip.y);
  if (d &lt; this.r + ellip.rad) {
    return true;
  } else {
    return false;
  }
}
</code></pre>

<p>this code should work below but I'm trying to interact with all four bubble objects one at a time... but to start with I think I need the intersect function in the var bubble area, then I can write another function where I change the colors (or change the size) of the four bubbles one at a time as the ellip goes through the bubbles... I already wrote all that but it didn't work so I deleted it and may start over.</p>

<pre><code>var ellip = {
    x: 0,
    y: 200,
    rad: 10,
    show: function() {
      ellipse(this.x, this.y, this.rad); 
    }
 } 
var bubble = {
    x: 150,
    y: 200,
    r: 50,
    display: function() {
       noStroke();
       fill(col);
       ellipse(this.x, this.y, this.r, this.r);
       ellipse(this.x*2, this.y, this.r, this.r);
       ellipse(this.x*3, this.y, this.r, this.r);
       ellipse(this.x*4, this.y, this.r, this.r);
   }  
}

function intersect(){
    var d = dist(bubble.x, bubble.y, ellip.x, ellip.y);
        if (d &lt; bubble.r + ellip.rad) {
           bubble.r += .3;
             // return true;
      //} else {
             // return false;
     //}
   }
}
function setup() {
     createCanvas(800, 400);
     col = color(214, 42, 29);
}

function draw() {
      background(200);
      bubble.display();
      intersect();
      noStroke();  
      fill(0);  
      ellip.show(); 
      ellip.x += 2;
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Accessing a class through an arrayList in another class</title>
      <link>https://forum.processing.org/two/discussion/21670/accessing-a-class-through-an-arraylist-in-another-class</link>
      <pubDate>Wed, 29 Mar 2017 08:19:17 +0000</pubDate>
      <dc:creator>Arya</dc:creator>
      <guid isPermaLink="false">21670@/two/discussions</guid>
      <description><![CDATA[<p>I have a class that I want to be accessed by another class and only in there. (in a little game)
So, my structure is that I have the main program that calls the class Tile and then everything runs over Tile.
In Tile, I want to access the class Carrots (that are supposed to be collected by the player). I used normal Arrays so far for the other things that get displayed in Tile, but I want the carrots to be removed after collision and read about ArrayLists for doing so.
However, I cant even get it to show the carrots once I have them in an ArrayList.. could you help me with that? I hope that once I know how to implement them in the first place, Ill figure how to remove objects from the ArrayList.</p>

<pre><code>class Tile
{
  Platform[]platforms = new Platform[3];
  ArrayList&lt;Carrots&gt;karotten;

  float posX=0;
  float posY=0; 
  float Width=0;
  float Height=0;
  float speedX = 1;

  Tile (float x, float y, float w, float h)
  {
    posX = x;
    posY = y;
    Width = w;
    Height = h;

    for (int i=0; i&lt;platforms.length; i++) {
      platforms[i] = new Platform(100+random(100)+i*150, height-70, 30, 40);
    }

    karotten=new ArrayList&lt;Carrots&gt;();
    for (int i=0; i&lt;karotten.size(); i++) {
    karotten.add(new Carrots(random(150), random(height-200, height-150), 10, 20));
    karotten.add(new Carrots(random(150,250), random(height-200, height-150), 10, 20));
    karotten.add(new Carrots(random(250,350), random(height-200, height-150), 10, 20));
    karotten.add(new Carrots(random(350,450), random(height-200, height-150), 10, 20));
    karotten.add(new Carrots(random(450,550), random(height-200, height-150), 10, 20));
    }
  void display()
  {
    for (int i=0; i&lt;platforms.length; i++) {
      platforms[i].drawPlatform(posX);
    }

    for(int i=0; i&lt;karotten.size(); i++) {
      Carrots carrots = (Carrots) karotten.get(i);
      carrots.displayCarrots(posX);
    }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>What bracket is missing on the 6th line!?! (else if (keyCode == DOWN)</title>
      <link>https://forum.processing.org/two/discussion/21643/what-bracket-is-missing-on-the-6th-line-else-if-keycode-down</link>
      <pubDate>Mon, 27 Mar 2017 21:07:56 +0000</pubDate>
      <dc:creator>Nathan101</dc:creator>
      <guid isPermaLink="false">21643@/two/discussions</guid>
      <description><![CDATA[<pre><code>    if ( key == CODED) {

        if (keyCode == UP &amp;&amp; linkY-20 &gt; 0 &amp;&amp; (map.get((int)linkX, (int)linkY-20)== -339816 || map.get((int)linkX, (int)linkY-20)== -340072 || map.get((int)linkX, (int)linkY-20)== -206680 || map.get((int)linkX, (int)linkY-20)== -9145228 || map.get((int)linkX, (int)linkY-20)== -3650548 || map.get((int)linkX, (int)linkY-20)== -16777216 || map.get((int)linkX, (int)linkY-20)== -197380))
          linkY-=5;
          linkImg = linkUp;
        else if (keyCode == DOWN &amp;&amp; linkY+20 &lt; height &amp;&amp; (map.get((int)linkX, (int)linkY+20)== -339816 || map.get((int)linkX, (int)linkY+20)== -340072 || map.get((int)linkX, (int)linkY+20)== -206680 || map.get((int)linkX, (int)linkY+20)== -9145228 || map.get((int)linkX, (int)linkY+20)== -3650548 || map.get((int)linkX, (int)linkY+20)== -16777216 || map.get((int)linkX, (int)linkY+20)== -197380))
          linkY+=5;
          linkImg = linkDown;
        else if (keyCode == LEFT &amp;&amp; linkX-20 &gt; 0 &amp;&amp; (map.get((int)linkX-20, (int)linkY)== -339816 || map.get((int)linkX-20, (int)linkY)== -340072 || map.get((int)linkX-20, (int)linkY)== -206680 || map.get((int)linkX-20, (int)linkY)== -9145228 || map.get((int)linkX-20, (int)linkY)== -3650548 || map.get((int)linkX-20, (int)linkY)== -16777216 || map.get((int)linkX-20, (int)linkY)== -197380))
          linkX-=5;
          linkImg = linkLeft;
        else if (keyCode == RIGHT &amp;&amp; linkX+20 &lt; width &amp;&amp; (map.get((int)linkX+20, (int)linkY)== -339816 || map.get((int)linkX+20, (int)linkY)== -340072 || map.get((int)linkX+20, (int)linkY)== -206680 || map.get((int)linkX+20, (int)linkY)== -9145228 || map.get((int)linkX+20, (int)linkY)== -3650548 || map.get((int)linkX+20, (int)linkY)== -16777216 || map.get((int)linkX+20, (int)linkY)== -197380))
          linkX+=5;
          linkImg = linkRight;
      }
    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>How do you make the button disappear once you've clicked it?</title>
      <link>https://forum.processing.org/two/discussion/21541/how-do-you-make-the-button-disappear-once-you-ve-clicked-it</link>
      <pubDate>Wed, 22 Mar 2017 20:50:44 +0000</pubDate>
      <dc:creator>maryjuarez</dc:creator>
      <guid isPermaLink="false">21541@/two/discussions</guid>
      <description><![CDATA[<p>Any help is much appreciated! Here is my code:</p>

<pre><code>float x = 230;
float y = 200;
float w = 150;
float h = 80;
int state = 0; 
PImage doctor;


void setup() {
  size(1280, 836);
  background(255);
  stroke(0);
  noFill();

  doctor = loadImage("doctor office.jpg");
}


void draw() {

   //draw button 
  rect(x, y, w, h);
  fill(255);



  //hover over button to change color
  if (mouseX&gt;x &amp;&amp; mouseX &lt;x+w &amp;&amp; mouseY&gt;y &amp;&amp; mouseY &lt;y+h) {
    fill(0);
  }



  // state machine 
  if (mousePressed &amp;&amp; mouseX&gt;x &amp;&amp; mouseX &lt;x+w &amp;&amp; mouseY&gt;y &amp;&amp; mouseY &lt;y+h) {
    state = 1; 
  } else {
    state = 0;
  }


  if (state == 1) {
   background(200);
   image(doctor,0,0, width, height);

  }


}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Matter.js Collision Not Detecting</title>
      <link>https://forum.processing.org/two/discussion/21409/matter-js-collision-not-detecting</link>
      <pubDate>Wed, 15 Mar 2017 12:18:16 +0000</pubDate>
      <dc:creator>pyan83</dc:creator>
      <guid isPermaLink="false">21409@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to practice using matter.js to create top down levels Bomberman/Zelda style.</p>

<p>Right now I want to get my circle, which is controlled by arrow keys to move and bump into static squares but it is just going through them. Did I set it up incorrectly? I have been coding for three months, so I might be quite slow sorry!</p>

<pre><code>var Engine = Matter.Engine,
    World = Matter.World,
    Bodies = Matter.Bodies;

var engine = Engine.create();
var world = engine.world;

var player;
var rocks = [];
var cols = 7;
var rows = 7;

function setup() {
    createCanvas(750, 750);
    Engine.run(engine);

    player = new Player(300, 300, 25);

    var spacing = width / cols;
    for (var j = 0; j &lt; rows; j++) {
        for (var i = 0; i &lt; cols; i++) {
            var r = new Rocks(i * spacing, j * spacing);
            rocks.push(r);
        }
    }
}

function draw() {
    background(51);
    Engine.update(engine);
    for (var i = 0; i &lt; rocks.length; i++) {
        rocks[i].show();
    }
    player.show();
    player.move();
}

function Player(x, y, r) {
    this.body = Bodies.circle(x, y, r);
    this.r = r;
    World.add(world, this.body);

    this.show = function () {
        ellipse(x, y, this.r * 2);
    }

    this.move = function () {
        if (keyIsDown(RIGHT_ARROW))
            x += 10;
        if (keyIsDown(LEFT_ARROW))
            x -= 10;
        if (keyIsDown(UP_ARROW))
            y -= 10;
        if (keyIsDown(DOWN_ARROW))
            y += 10;
        x = constrain(x, this.r, height - this.r);
        y = constrain(y, this.r, width - this.r);
    }
}

function Rocks(x, y, w, h, options) {
    var options = {
        isStatic: true
    }
    this.body = Bodies.rectangle(x, y, h, w, options);
    this.w = w;
    this.h = h;
    this.size = player.r * 2;
    World.add(world, this.body);

    this.show = function () {
        rect(x, y, this.size, this.size);
    }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>TypeError: undefined is not an object (evaluating 'bunker.x')</title>
      <link>https://forum.processing.org/two/discussion/21362/typeerror-undefined-is-not-an-object-evaluating-bunker-x</link>
      <pubDate>Mon, 13 Mar 2017 01:03:23 +0000</pubDate>
      <dc:creator>tim50</dc:creator>
      <guid isPermaLink="false">21362@/two/discussions</guid>
      <description><![CDATA[<p>Hello, does anyone know how I can get the computer to understand my bunker x, y variables? I would rather use other.x, other.y. other.r than bunker.x, bunker.y, and bunker.r but either way I'm lost.</p>

<pre><code>var bunkers = [];
var b1;

function setup() {
      createCanvas(window.innerWidth, window.innerHeight);
      b1 = new Bubble(250, 200);
        for (var i = 0; i &lt; 4; i++){
           bunkers[i] = new Bunker(i*294 + 247, height-140);
   }
}

function draw() {
      background(0);

      for (var i = 0; i &lt; 4; i++){
            bunkers[i].show();
  }
      b1.update();
      b1.display();

      if (b1.intersects(bunkers[i])){
            bunkers[i].changeColor();
    }
}
function Bubble(x, y) {
      this.x = x;
      this.y = y;
      this.r = 48;
      this.col = color(255);
      this.changeColor = function() {
            this.col = color(random(255), random(255), random(255))

    }
      this.display = function() {
            stroke(255);
            fill(this.col);
            ellipse(this.x, this.y, this.r * 2, this.r * 2);

    }

       this.intersects = function(bunker) {
             var d = dist(this.x, this.y, bunker.x, bunker.y);
       if (d &lt; this.r + bunker.r) {
                  return true;
        } else {
                  return false;

        }
 }

       this.update = function() {
            this.x = this.x + random(-5, 5);
            this.y = this.y + random(-5, 5);

    }

}

function Bunker(x, y) {
       this.x = x;
      this.y = y;
      this.r = 60;
       this.show = function(){
          push();
          translate(this.x-257, this.y-68);
          noStroke();
          fill(0, 100, 0);
          beginShape();
          vertex(200, 50);
          vertex(208, 50);
          vertex(208, 42);
          vertex(216, 42);
          vertex(216, 34);
          vertex(224, 34);
          vertex(224, 26);
          vertex(290, 26);
          vertex(290, 34);
          vertex(298, 34);
          vertex(298, 42);
          vertex(306, 42);
          vertex(306, 50);
          vertex(314, 50);
          vertex(314, 110);
          vertex(290, 110);
          vertex(290, 102);
          vertex(282, 102);
          vertex(282, 94);
          vertex(274, 94);
          vertex(274, 86);
          vertex(240, 86);
          vertex(240, 94);
          vertex(232, 94);
          vertex(232, 102);
          vertex(224, 102);
          vertex(224, 110);
          vertex(200, 110);
          endShape(CLOSE);
          pop();
   } 
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Help with object collision</title>
      <link>https://forum.processing.org/two/discussion/21154/help-with-object-collision</link>
      <pubDate>Sat, 04 Mar 2017 18:49:39 +0000</pubDate>
      <dc:creator>Kang01</dc:creator>
      <guid isPermaLink="false">21154@/two/discussions</guid>
      <description><![CDATA[<p>I am currently working on a game where you shoot stuff down in space
and I cant figure out how to check if the bullets are hitting the targets
heres the code</p>

<pre><code>ArrayList &lt;Bullet&gt; bullets;
float numOfStars = 500;
PImage spaceShip;


float [] x = new float[int(numOfStars)];
float [] y = new float[int(numOfStars)];
float [] speed = new float[int(numOfStars)];
float [] bullet;
//Bullet = new Bullet();
float bulletSpeed;
boolean Bullet;










void setup() {
   bullets = new ArrayList();
  fullScreen();
  //size(500, 400);
  background(0);
  stroke(255);
  spaceShip = loadImage("Spaceship2.png");

  noCursor();



  int i = 0;
  while (i &lt; numOfStars) {
    x[i]= random(0, width);
    y[i] = random(0, height);
    speed[i] = random(1, 5);
    i = i +1;
  }
}


void draw() {
  background(0);
  removeToLimit(100);
  moveAll();
  displayAll();
  //bulletSpeed = bulletSpeed +5;







  //draw triangle
  //triangle(mouseX,mouseY-6,mouseX +20, mouseY,mouseX, mouseY +6);

  //rotate(radians(180));{
  image(spaceShip, mouseX-10, mouseY);
  //}

  int i = 0;
  while (i &lt; numOfStars) {
    float co = map(speed[i], 1, 5, 100, 255);
    stroke(co);
    strokeWeight(speed[i]);
    point(x[i], y[i]);



    x[i] = x[i] -speed[i]/ 2;

    if (x[i] &lt; 0) {
      x[i] = width;
    }
    i = i+5;
  }






  //if (mousePressed == true) {
  // Bullet = true;
  //}





  //  if (Bullet == true) {
  //    rect(mouseX -10 +bulletSpeed, mouseY, 40,10);
  //    fill(#03FC3E);


  //  }







}



class Bullet//bullet class
{
  float x;
  float y;
  float speed;
  Bullet(float tx, float ty)
  {
    x = tx;
    y = ty;
  }
  void display()
  {
     rect(x,y+20, 40,10);
     rect(x,y+80,40,10);
  fill(#03FC3E);
    //stroke(255);
    //point(x,y);
  }
  void move()
  {
    x+= 20;
  }
}

void removeToLimit(int maxLength)
{
  while(bullets.size() &gt; maxLength)
  {
    bullets.remove(0);
  }
}
void moveAll()
{
  for(Bullet temp : bullets)
  {
    temp.move();
  }
}
void displayAll()
{
  for(Bullet temp : bullets)
  {
    temp.display();
  }
}
void mousePressed()//add a new bullet if mouse is clicked
{
  Bullet temp = new Bullet(mouseX,mouseY);
  bullets.add(temp);
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>I'm trying to use minim to create a sounds when the balls collide, but i'm a bit lost. Thank you!</title>
      <link>https://forum.processing.org/two/discussion/21211/i-m-trying-to-use-minim-to-create-a-sounds-when-the-balls-collide-but-i-m-a-bit-lost-thank-you</link>
      <pubDate>Mon, 06 Mar 2017 21:21:51 +0000</pubDate>
      <dc:creator>framos</dc:creator>
      <guid isPermaLink="false">21211@/two/discussions</guid>
      <description><![CDATA[<pre><code>    import ddf.minim.*;

    import ddf.minim.analysis.*;

    import ddf.minim.effects.*;

    import ddf.minim.signals.*;

    import ddf.minim.spi.*;

    import ddf.minim.ugens.*;


    Ball[] balls =  { 
      new Ball(100, 400, 20), 
      new Ball(700, 400, 80)
    };


      Minim minim;

      Sampler b;

      AudioOutput out;

      Sampler d;

    void setup() {
      size(640, 360);
      minim = new Minim(this);
      b = new Sampler("Bell.mp3", 100, minim);
      out = minim.getLineOut();
      d = new Sampler("Door.mp3", 3, minim);

    }

    void draw() {
    background(51);
    for (Ball b : balls) {
    b.update();
    b.display();

       b.checkBoundaryCollision();
      }

  balls[0].checkCollision(balls[1]);
}

void mouseMoved () {

  b.trigger();

  d.trigger();
}

class Ball {

  PVector position;

  PVector velocity;

  float radius, m;

  Sampler sampler;


  Ball(float x, float y, float r_, Sampler a) {

    Sampler = a;

    position = new PVector(x, y);

    velocity = PVector.random2D();

    velocity.mult(3);

    radius = r_;

    m = radius*.1;
  }

  void update() {

    position.add(velocity);
  }

  void checkBoundaryCollision() {

    if (position.x &gt; width-radius) {

      position.x = width-radius;

      velocity.x *= -1;

      triggerSample();

    } else if (position.x &lt; radius) {

      position.x = radius;

      velocity.x *= -1;

    } else if (position.y &gt; height-radius) {

      position.y = height-radius;

      velocity.y *= -1;

    } else if (position.y &lt; radius) {

      position.y = radius;

      velocity.y *= -1;
    }
  }

  void checkCollision(Ball other) {


    PVector distanceVect = PVector.sub(other.position, position);

    float distanceVectMag = distanceVect.mag();


    float minDistance = radius + other.radius;

    if (distanceVectMag &lt; minDistance) {
      float distanceCorrection = (minDistance-distanceVectMag)/2.0;
      PVector d = distanceVect.copy();
      PVector correctionVector = d.normalize().mult(distanceCorrection);
      other.position.add(correctionVector);
      position.sub(correctionVector);

      float theta  = distanceVect.heading();

      float sine = sin(theta);
      float cosine = cos(theta);


      PVector[] bTemp = {
        new PVector(), new PVector()
      };


      bTemp[1].x  = cosine * distanceVect.x + sine * distanceVect.y;
      bTemp[1].y  = cosine * distanceVect.y - sine * distanceVect.x;


      PVector[] vTemp = {
        new PVector(), new PVector()
      };

      vTemp[0].x  = cosine * velocity.x + sine * velocity.y;
      vTemp[0].y  = cosine * velocity.y - sine * velocity.x;
      vTemp[1].x  = cosine * other.velocity.x + sine * other.velocity.y;
      vTemp[1].y  = cosine * other.velocity.y - sine * other.velocity.x;


      PVector[] vFinal = {  
        new PVector(), new PVector()
      };

      vFinal[0].x = ((m - other.m) * vTemp[0].x + 2 * other.m * vTemp[1].x) / (m + other.m);
      vFinal[0].y = vTemp[0].y;

      vFinal[1].x = ((other.m - m) * vTemp[1].x + 2 * m * vTemp[0].x) / (m + other.m);
      vFinal[1].y = vTemp[1].y;


      bTemp[0].x += vFinal[0].x;
      bTemp[1].x += vFinal[1].x;


      PVector[] bFinal = { 
        new PVector(), new PVector()
      };

      bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y;
      bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x;
      bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y;
      bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x;

      other.position.x = position.x + bFinal[1].x;
      other.position.y = position.y + bFinal[1].y;

      position.add(bFinal[0]);


      velocity.x = cosine * vFinal[0].x - sine * vFinal[0].y;
      velocity.y = cosine * vFinal[0].y + sine * vFinal[0].x;
      other.velocity.x = cosine * vFinal[1].x - sine * vFinal[1].y;
      other.velocity.y = cosine * vFinal[1].y + sine * vFinal[1].x;
    }
  }

  void display() {

    noStroke();

    fill(204);

    ellipse(position.x, position.y, radius*2, radius*2);

  }
  void triggerSample(){

    sampler.rate.setLastValue(10 / radius);

    sampler.trigger();
  }
}

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Collision prediction</title>
      <link>https://forum.processing.org/two/discussion/21110/collision-prediction</link>
      <pubDate>Thu, 02 Mar 2017 19:42:55 +0000</pubDate>
      <dc:creator>schotsl</dc:creator>
      <guid isPermaLink="false">21110@/two/discussions</guid>
      <description><![CDATA[<p>I've got the frame of a game working, but I wanted to add some collision between enemies, player and walls. Does anyone have any advice on how to achieve this, I've only added the code that seems essential for collections detection, since otherwise it would be 900 lines of code.</p>

<pre><code>//detect forward movement.
if (key == 'w' || key == 'W' || keyCode == UP) {
  if (currentScreen == 4) {
    directiony1 = 1;
  } else {
    menu -= 1;
  }
}

//detect backward movement.
if (key == 's' || key == 'S' || keyCode == DOWN) {
  if (currentScreen == 4) {
    directiony2 = 1;
  } else {
    menu += 1;
  }
}

/detect left movement.
  if (key == 'a' || key == 'A' || keyCode == DOWN) directionx1 = 1;

//detect right movement.
if (key == 'd' || key == 'D' || keyCode == UP) directionx2 = 1;

//detect if player wants to sprint.
if (keyCode == SHIFT) run = 1;

void keyReleased() {
  //check player movement input
  if (key == 'a' || key == 'A' || keyCode == DOWN) directionx1 = 0;
  if (key == 'd' || key == 'D' || keyCode == UP) directionx2 = 0;
  if (key == 'w' || key == 'W' || keyCode == DOWN) directiony1 = 0;
  if (key == 's' || key == 'S' || keyCode == UP) directiony2 = 0;
  if (keyCode == SHIFT) run = 0;
}
}

//calculate and place enemy
enemiescount = 0;
if (width/2 - playerx - enemies[enemiescount] &gt; -enemies[enemiescount+9] &amp;&amp; width/2 - playerx - enemies[enemiescount] &lt; enemies[enemiescount+9] &amp;&amp; height/2 - playery - enemies[enemiescount+1] &gt; -enemies[enemiescount+9] &amp;&amp; height/2 - playery - enemies[enemiescount+1] &lt; enemies[enemiescount+9] ) {
  if (width/2 - playerx - enemies[enemiescount] &gt; -5 &amp;&amp; width/2 - playerx - enemies[enemiescount] &lt; 5 &amp;&amp; height/2 - playery - enemies[enemiescount+1] &gt; -5 &amp;&amp; height/2 - playery - enemies[enemiescount+1] &lt; 5 ) {
    if (enemies[enemiescount+10] == 0) {
      enemies[enemiescount+11] = millis();
      enemies[enemiescount+10] = 1;
    }
    if (millis() - enemies[enemiescount+11] &gt; 10) {
      health = enemydamagetime * health - 0.5 * enemies[enemiescount+12] * damagemultiplyer;
      enemies[enemiescount+10] = 0;
    }
  } else {
    enemies[enemiescount] = enemymovementtime * int(cos(atan2(height/2-playery-enemies[enemiescount+1], width/2-playerx-enemies[enemiescount])) * enemies[enemiescount+8]*2 + enemies[enemiescount]);
    enemies[enemiescount+1] = enemymovementtime * int(sin(atan2(height/2-playery-enemies[enemiescount+1], width/2-playerx-enemies[enemiescount])) * enemies[enemiescount+8]*2 + enemies[enemiescount+1]);
  }
} else {
  if (enemies[enemiescount+4] == 0) {
    enemies[enemiescount+6] = int(random(-300, 300) + enemies[enemiescount]);
    enemies[enemiescount+7] = int(random(-300, 300) + enemies[enemiescount+1]);
    enemies[enemiescount+2] = int(random(1000, 2000));
    enemies[enemiescount+3] = millis();
    enemies[enemiescount+4] = 1;
  }
  if (enemies[enemiescount+5] == 0) {
    enemies[enemiescount] = enemymovementtime * int(cos(atan2(enemies[enemiescount+7]-enemies[enemiescount+1], enemies[enemiescount+6]-enemies[enemiescount])) * enemies[enemiescount+8] + enemies[enemiescount]);
    enemies[enemiescount+1] = enemymovementtime * int(sin(atan2(enemies[enemiescount+7]-enemies[enemiescount+1], enemies[enemiescount+6]-enemies[enemiescount])) * enemies[enemiescount+8] + enemies[enemiescount+1]);
  }
  if ( millis() - enemies[3] &gt; enemies[2] ) {
    enemies[4] = 0;
    enemies[5] = 0;
  }
  if (enemies[enemiescount+6] - enemies[enemiescount] &gt; -10 &amp;&amp; enemies[enemiescount+6] - enemies[enemiescount] &lt; 10 ) {
    if (enemies[enemiescount+7] - enemies[enemiescount+1] &gt; -10 &amp;&amp; enemies[enemiescount+7] - enemies[enemiescount+1] &lt; 10 ) {
      enemies[enemiescount+5] = 1;
    }
  }
}

//move player
if (run == 0) playerspeed = 0;
if (directionx1 == 1 &amp;&amp; directionx2 == 0 &amp;&amp; directiony1 == 0 &amp;&amp; directiony2 == 0) {
  playerx += (10 * (1 + playerspeed)) * playermovementtime;
}
if (directionx2 == 1 &amp;&amp; directionx1 == 0 &amp;&amp; directiony1 == 0 &amp;&amp; directiony2 == 0) playerx -= (10 * (1 + playerspeed)) * playermovementtime;
if (directiony1 == 1 &amp;&amp; directiony2 == 0 &amp;&amp; directionx1 == 0 &amp;&amp; directionx2 == 0) playery += (10 * (1 + playerspeed)) * playermovementtime;
if (directiony2 == 1 &amp;&amp; directiony1 == 0 &amp;&amp; directionx1 == 0 &amp;&amp; directionx2 == 0) playery -= (10 * (1 + playerspeed)) * playermovementtime;

if (directionx1 == 1 &amp;&amp; directionx2 == 0 &amp;&amp; directiony1 == 1 &amp;&amp; directiony2 == 0) {
  playerx += (5 * (1 + playerspeed)) * playermovementtime;
  playery += (5 * (1 + playerspeed)) * playermovementtime;
}
if (directionx2 == 1 &amp;&amp; directionx1 == 0 &amp;&amp; directiony1 == 0 &amp;&amp; directiony2 == 1) {
  playerx -= (5 * (1 + playerspeed)) * playermovementtime;  
  playery -= (5 * (1 + playerspeed)) * playermovementtime;
}
if (directiony1 == 1 &amp;&amp; directiony2 == 0 &amp;&amp; directionx1 == 0 &amp;&amp; directionx2 == 1) {
  playery += (5 * (1 + playerspeed)) * playermovementtime;
  playerx -= (5 * (1 + playerspeed)) * playermovementtime;
}
if (directiony2 == 1 &amp;&amp; directiony1 == 0 &amp;&amp; directionx1 == 1 &amp;&amp; directionx2 == 0) {
  playery -= (5 * (1 + playerspeed)) * playermovementtime;
  playerx += (5 * (1 + playerspeed)) * playermovementtime;
}

//draw player
pushMatrix();
rectMode(CENTER);
noFill();
translate(width/2, height/2);
rect(0, 0, 20, 20);
popMatrix();
translate(width/2, height/2);
</code></pre>
]]></description>
   </item>
   <item>
      <title>simple rect collision from array</title>
      <link>https://forum.processing.org/two/discussion/20944/simple-rect-collision-from-array</link>
      <pubDate>Wed, 22 Feb 2017 10:21:01 +0000</pubDate>
      <dc:creator>benniii</dc:creator>
      <guid isPermaLink="false">20944@/two/discussions</guid>
      <description><![CDATA[<p>dear all,
i'm an absolute beginner in processing and i'm struggling with a (simple?) problem. please bare with me, as I'm sure the code is catastrophic and a lot of my terminology here is wrong :)</p>

<p>basically i'm trying to achieve a simple rectangle collision, using an array and a class. it <em>almost</em> works, but sometimes, rectangles don't collide/rebound, but pass through one another.. i have no clue why this is.</p>

<p>i'd be gracious for any help!</p>

<pre><code>Bouncer[] bouncers = new Bouncer[0];
float resize = 30;
float minsize = 2;
float maxsize = 50;
float freq = 440;
float amp = 0;
float globspeed = 3.;
float boxes = 5;
float margin = 5;

void setup() {
  //frameRate(10);
  size(700, 400);
  resize = minsize;
  pmouseY = mouseY;
}

void draw() {

  background(255);
  if (mousePressed) {
    resize += 2;
    noStroke();
    rectMode(CENTER);
    fill(50);
    resize = constrain(resize, minsize, maxsize);
    rect (mouseX, mouseY, resize, resize);
  }



  for (int i = 0; i &lt; bouncers.length; i++ ) {
    bouncers[i].move(); 
    bouncers[i].create();
    bouncers[i].collide();
  }


  float onebox = ((width - ((boxes+1)*margin)) / boxes );


  for (float i = 1; i &lt;= boxes; i++ ) {
    rectMode(CORNER);
    noStroke();
    fill(50);
    rect((i-1.)*(onebox+margin)+margin, height-20, onebox, 20-margin);
  }
}

void mouseReleased() {
  Bouncer anotherone = new Bouncer(mouseX-resize/2, mouseY-resize/2, resize, resize-resize/2*(globspeed*-1), bouncers.length, 50);
  bouncers = (Bouncer[]) append(bouncers, anotherone);
  resize = minsize;
}
</code></pre>

<p>here's my class declaration:</p>

<pre><code>class Bouncer {
  float posX;
  float posY;
  float size;
  float speed;
  //SinOsc sine;
  float freq = 440;
  float gravity = 1;
  int iam;
  float col;


  Bouncer(float posX_, float posY_, float size_, float speed_, int iam_, float col_) {
    posX = posX_;
    posY = posY_;
    size = size_;
    speed = speed_/10;
    iam = iam_;
    col = col_;
  }

  void create() {
    rectMode(CORNER);
    noStroke();
    fill(col);
    rect(posX, posY, size, size);
    //println(iam);
  }


  void collide() {
    for (int io = 0; io &lt; bouncers.length; io++) {
      float x1 = bouncers[iam].posX;
      float y1 = bouncers[iam].posY;
      float s1 = bouncers[iam].size;
      float x2 = bouncers[io].posX;
      float y2 = bouncers[io].posY;
      float s2 = bouncers[io].size;
      if (x1+s1/2 &gt;= x2-s2/2 &amp;&amp; x1-s1/2 &lt;= x2+s2/2) 
      { 
        if (y1+s1 &gt;= y2 &amp;&amp; y1 &lt;= y2+s2) {
          bouncers[iam].speed *= -1;
          bouncers[io].speed *= -1;
          bouncers[io].col = 150;
        }
      }
    }
  }

  void move() {
    posY = posY + speed; 
    if (posY &gt; height-size-20) {
      speed *= -1; 
      posY = height-size-20;
    } else  if (posY &lt;= 0) {
      posY = 1;
      speed *= -1 ; 
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How do I calculate the velocities of a ball bouncing inside an equilateral triangle?</title>
      <link>https://forum.processing.org/two/discussion/20805/how-do-i-calculate-the-velocities-of-a-ball-bouncing-inside-an-equilateral-triangle</link>
      <pubDate>Wed, 15 Feb 2017 12:10:57 +0000</pubDate>
      <dc:creator>Eiroth</dc:creator>
      <guid isPermaLink="false">20805@/two/discussions</guid>
      <description><![CDATA[<p>I know how to make a ball bounce in rectangle like this:</p>

<pre><code>int x=width/2;
int y=height/2;
int xSpeed=int(random(10));
int ySpeed=int(random(10));
void setup() {
  size(300, 300);
}

void draw() {
  background(0);
  x+=xSpeed;
  y+=ySpeed;
  if (x+15&gt;=width || x-15&lt;=0) {
    xSpeed *=-1;
  }

  if (y+15&gt;=height || y-15&lt;=0) {
    ySpeed *=-1;
  }
  ellipse(x, y, 30, 30);
}
</code></pre>

<p>However, when I try to bounce it inside of an 
equilateral triangle I can't figure out how I should affect the velocities on the edges that are tilted in relation to the x and y axis...</p>
]]></description>
   </item>
   <item>
      <title>Collision detection - why not working properly?</title>
      <link>https://forum.processing.org/two/discussion/20887/collision-detection-why-not-working-properly</link>
      <pubDate>Mon, 20 Feb 2017 01:39:12 +0000</pubDate>
      <dc:creator>djevazi</dc:creator>
      <guid isPermaLink="false">20887@/two/discussions</guid>
      <description><![CDATA[<p>PeasyCam library is required.</p>

<p>I'm trying to detect a point-triangle collision using this tutorial: <a href="http://www.jeffreythompson.org/collision-detection/tri-point.php" target="_blank" rel="nofollow">http://www.jeffreythompson.org/collision-detection/tri-point.php</a> . But my sketch is 3d, so I translating a 3d coorinates in 2d using screenX() and screenY(). The problem is that when my mouse is inside of triangle, sometimes the resulting "hit" color is blinking and I have "no hit" message. Can't understand why the collision is not working properly, it must be "hit" all the time my mouse is inside of thiangle. Any suggestions? Thanks.</p>

<pre><code>import peasy.*;
PeasyCam jcam;

ArrayList &lt;Point&gt; points = new ArrayList &lt;Point&gt;();
ArrayList &lt;Triangle&gt; triangles = new ArrayList &lt;Triangle&gt;();

void setup()
{
  size(800, 800, P3D);
  smooth();
  jcam = new PeasyCam(this, 200);

  color tempColore=color(255, 255, 255);

  PVector v1= new PVector(0, 0, 0);
  Point pt1 = new Point(v1, tempColore);
  points.add(pt1);

  PVector v2= new PVector(100, 0, 0);
  Point pt2 = new Point(v2, tempColore);
  points.add(pt2);

  PVector v3= new PVector(0, 100, 0);
  Point pt3 = new Point(v3, tempColore);
  points.add(pt3);

  PVector v4= new PVector(100, 0, 0);
  Point pt4 = new Point(v4, tempColore);
  points.add(pt4);  

  PVector v5= new PVector(100, 100, 0);
  Point pt5 = new Point(v5, tempColore);
  points.add(pt5);

  PVector v6= new PVector(0, 100, 0);
  Point pt6 = new Point(v6, tempColore);
  points.add(pt6);


  //triangles initialize
  for (int i=0; i&lt;points.size(); i=i+3)
  {
    Triangle tr = new Triangle(points.get(i).pos, points.get(i+1).pos, points.get(i+2).pos);
    triangles.add(tr);
  }

}

boolean triPoint(float x1, float y1, float x2, float y2, float x3, float y3,float mx, float my) 
  {
    // get the area of the triangle
    float areaOrig = abs( (x2-x1)*(y3-y1) - (x3-x1)*(y2-y1) );

    // get the area of 3 triangles made between the point
    // and the corners of the triangle
    float area1 =    abs( (x1-mx)*(y2-my) - (x2-mx)*(y1-my) );
    float area2 =    abs( (x2-mx)*(y3-my) - (x3-mx)*(y2-my) );
    float area3 =    abs( (x3-mx)*(y1-my) - (x1-mx)*(y3-my) );

    // if the sum of the three areas equals the original,
    // we're inside the triangle!
    if (area1 + area2 + area3 == areaOrig) 
    {
      return true;
    }
    return false;
  }


void draw()
{
  background(150);

  for (int i = triangles.size ()-1; i &gt;= 0; i--)
  { 
    color tempColore=0;

    float ax = screenX(triangles.get(i).A.x,triangles.get(i).A.y);
    float ay = screenY(triangles.get(i).A.x,triangles.get(i).A.y);
    float bx = screenX(triangles.get(i).B.x,triangles.get(i).B.y);
    float by = screenY(triangles.get(i).B.x,triangles.get(i).B.y);
    float cx = screenX(triangles.get(i).C.x,triangles.get(i).C.y);
    float cy = screenY(triangles.get(i).C.x,triangles.get(i).C.y);
    float mx = mouseX;
    float my = mouseY;

    boolean hit = triPoint(ax,ay,bx,by,cx,cy,mx,my);

    if(hit==true)
    {
      tempColore = #A57066;
      println("hit");
    }
    else
    {
      tempColore = #CEBE9A;
      println("no hit");
    }
    triangles.get(i).display(tempColore);
  }


  for (int i = points.size ()-1; i &gt;= 0; i--)
  { 
    points.get(i).display();
  }

}


class Point
{
  PVector pos;
  color colore;


  Point(PVector posIn, color coloreIn)
  {
    pos = posIn;
    colore=coloreIn;
  }

  void display()
  {
    colorMode(RGB);
    noStroke();
    fill(colore);
    pushMatrix();
    translate(pos.x,pos.y,pos.z);
    box(1);
    popMatrix(); 
  }
}


class Triangle
{
  PVector A, B, C;
  color colore;

  Triangle (PVector Ain, PVector Bin, PVector Cin)
  {
    A=Ain;
    B=Bin;
    C=Cin;
  }

  void display(color coloreIn)
  {
    colore = coloreIn;
    fill(colore);
    beginShape(TRIANGLE);
    stroke(255,20);
    strokeWeight(2);
    vertex(A.x,A.y,A.z);
    vertex(B.x,B.y,B.z);
    vertex(C.x,C.y,C.z);
    endShape();
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Make an object disappear upon collision</title>
      <link>https://forum.processing.org/two/discussion/20886/make-an-object-disappear-upon-collision</link>
      <pubDate>Mon, 20 Feb 2017 00:53:23 +0000</pubDate>
      <dc:creator>Akkadian</dc:creator>
      <guid isPermaLink="false">20886@/two/discussions</guid>
      <description><![CDATA[<p>Hello friends:</p>

<p>I'm a starter (I really mean starter, I have little or no clue about programming at all), and our teacher assigned us this homework in which we basically have to make 2 snakes (more like 2 independent squares) to go move around using wasd and the arrow keys and eat coins that are generated randomly. I have pretty much all I want except the part where the coins disappear.</p>

<p>So the question is: How do I make the coins disappear when one of the snakes collide with them?</p>

<p>Additional info:
-I know the snakes don't move fluidly and since we are beginners the teacher told us it was okay.
-There are some things in Spanish because it's my native language but I hope you won't have issues with it.</p>

<p>Here's my code until now:</p>

<pre><code>Snake1 serpiente1;
Snake2 serpiente2;

Bol uno;
Bol dos;
Bol tres;
Bol cuatro;
Bol cinco;
Bol seis;
Bol siete;
Bol ocho;
Bol nueve;
Bol diez;


void setup(){
  size(600,600);
  serpiente1=new Snake1();
  serpiente2=new Snake2();

  uno= new Bol ();
  dos= new Bol ();
  tres= new Bol ();
  cuatro= new Bol ();
  cinco= new Bol ();
  seis= new Bol ();
  siete= new Bol ();
  ocho= new Bol ();
  nueve= new Bol ();
  diez= new Bol ();

}

  void draw (){
    background(255);``
    serpiente1.moverSerpiente();
    serpiente1.dibujaSerpiente(); 
    serpiente2.moverSerpiente();
    serpiente2.dibujaSerpiente();
    uno.dibujaMoneda();
    dos.dibujaMoneda();
    tres.dibujaMoneda();
    cuatro.dibujaMoneda();
    cinco.dibujaMoneda();
    seis.dibujaMoneda();
    siete.dibujaMoneda();
    ocho.dibujaMoneda();
    nueve.dibujaMoneda();
    diez.dibujaMoneda();


}

class Snake1{
float posx;
float posy;
float ancho;
float alto;
color c;
//constructor
Snake1 (){
  posx=30;
  posy=30;
  ancho=25;
  alto=25;
  c=color(random(255),random(255),random(255));
}

void moverSerpiente(){
  if (keyCode == UP){
    posy=posy-3;
    if(posy&lt;0)
    posy=600;
  }
  else if (keyCode == DOWN){
    posy=posy+3;
    if(posy&gt;600)
    posy=0;
  }
  else if (keyCode == RIGHT){
    posx=posx+3;
    if(posx&gt;600)
    posx=0;
  }
  else if (keyCode == LEFT){
    posx=posx-3;
    if(posx&lt;0)
    posx=600;
  }
}

  void dibujaSerpiente(){
    fill(c);
    rect(posx, posy, ancho, alto);
  }
}
class Snake2{
float posx;
float posy;
float ancho;
float alto;
color c;

//constructor
Snake2 (){
  posx=550;
  posy=550;
  ancho=25;
  alto=25;
  c=color(random(255),random(255),random(255));
}

void moverSerpiente(){
  if (key == 'w'){
    posy=posy-3;
    if(posy&lt;0)
    posy=600;
  }
  else if (key == 's'){
    posy=posy+3;
     if(posy&gt;600)
    posy=0;
  }
  else if (key == 'd'){
    posx=posx+3;
    if(posx&gt;600)
    posx=0;
  }
  else if (key == 'a'){
    posx=posx-3;
     if(posx&lt;0)
    posx=600;
  }
}

  void dibujaSerpiente(){
    fill(c);
    rect(posx, posy, ancho, alto);
  }
}

class Bol{
  float posx;
  float posy;
  float ancho;
  float alto;

  Bol(){
    posx=random(570);
    posy=random(570);
    ancho=15;
    alto=15;
  }

  void dibujaMoneda(){
    fill(250,250,0);
    ellipse(posx,posy,ancho,alto);
  }
}
</code></pre>

<p>I hope you guys can help me out! 
Thanks.</p>
]]></description>
   </item>
   <item>
      <title>Detecting Collision between Rectangle and Circle</title>
      <link>https://forum.processing.org/two/discussion/20769/detecting-collision-between-rectangle-and-circle</link>
      <pubDate>Sun, 12 Feb 2017 23:21:43 +0000</pubDate>
      <dc:creator>pyan83</dc:creator>
      <guid isPermaLink="false">20769@/two/discussions</guid>
      <description><![CDATA[<p>Hi all,</p>

<p>I'm trying to make Pong using Object Orientated Programming as an exercise.</p>

<p>Right now I've gotten to the point of trying to get the ball to bounce off the paddle if they intersect.</p>

<p>I'm pretty sure my logic is correct? If the current distance of the middle of rectangle and the middle of circle is less then middle of circle + middle of rectangle, then it is colliding. I separate these into x and y because the rectangle has 2 different length sides for x and y. If either x OR y is true, true.</p>

<p>Note I've kept in some commented stuff to show some other attempts... dunno if I should be using "this.other" type stuff or should I have an update function?</p>

<p>I've tried writing that out as an if statement but my current distance number seems stagnant, when it should be constantly changing. I'm very new just started learning this year, so I might need very basic laymen's terms! :P</p>

<p>Here's my code so far, I have seperated it into sketch.js and objects.js</p>

<pre><code>var p1;
var xspeed = 3;
var yspeed = 3;

function setup() {
    createCanvas(400, 400);
    p1 = new player1();
    b = new ball();
}

function draw() {
    background(51);
    p1.show();
    p1.move();
    b.show();
    b.move();
    b.edges();
    console.log(b.currentX, b.currentY);
    if (b.hit(p1)) {
        console.log("hit");
        // xspeed = xspeed * -1;
        // yspeed = yspeed * -1;
    }
}

function keyPressed() {
    if (keyCode === 87) {
        p1.setDir(-1);
    } else if (keyCode === 83) {
        p1.setDir(1);
    }
}

function keyReleased() {
    p1.setDir(0);
}

function player1() {
    this.x = 20;
    this.y = height / 2;
    this.w = 12;
    this.h = 64;

    this.ydir = 0;

    this.show = function () {
        fill(255);
        rect(this.x, this.y, this.w, this.h);
    }

    this.setDir = function (dir) {
        this.ydir = dir;
    }

    this.move = function (dir) {
        this.y += this.ydir * 5;
    }
}

function ball() {
    this.x = 100; //Start location of ball
    this.y = 25;
    this.r = 5;
    this.currentX = p1.w / 2 + this.r; //Less than this x number = colliding
    this.currentY = p1.h / 2 + this.r; //Less than this y number = colliding
    this.distX = p1.w / 2 + this.x; //Current x dist of mid of ball and rect
    this.distY = p1.h / 2 + this.y; //Current y dist of mid of ball and rect

    this.show = function () {
        fill(255);
        ellipse(this.x, this.y, this.r * 2);
    }

    this.move = function () {
        this.x = this.x + xspeed;
        this.y = this.y + yspeed;
    }

    this.edges = function () {
        if (this.y &gt; height || this.y &lt; 0) {
            yspeed = yspeed * -1;
        }
        if (this.x &gt; height || this.x &lt; 0) {
            xspeed = xspeed * -1;
        }
    }
    // this.hit = function () {
    //  var dx = dist(this.x.pos, p1.x.pos);
    //  var dy = dist(this.y.pos, p1.y.pos);
    //  if (dx &lt; this.r + p1.x / 2 || dy &lt; this.r + p1.y / 2) {
    //      return true;
    //  } else {
    //      return false;
    //  }
    // }
    this.hit = function (other) {
        if (this.distX &gt; this.currentX || this.distY &gt; this.currentY) {
            //distX, distY is current distance of middle of circle and rect
            //If dist x is more then rect.w/2 + r = true
            //If dist y is more then rect.h/2 + r = true
            return true;
        } else {
            return false;
        }
    }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Make a Ball Collide a Diagonal</title>
      <link>https://forum.processing.org/two/discussion/20653/make-a-ball-collide-a-diagonal</link>
      <pubDate>Sun, 05 Feb 2017 10:24:34 +0000</pubDate>
      <dc:creator>Gabs</dc:creator>
      <guid isPermaLink="false">20653@/two/discussions</guid>
      <description><![CDATA[<p>¡Hi everyone!</p>

<p>I'm a Processing amateur, I don't know much yet about Processing, but the thing is, I have spent the last two days trying to figure out HOW TO Make a Ball Collide a Diagonal line.</p>

<p>I used something like this to make the ball collide a straight line (x or y):</p>

<p>if (position of the ball is bigger than 'this X'){
    make the ball change direction;
  }</p>

<p>BUT i haven't been able to apply this to a diagonal.</p>

<p>PLEASE Help me.... I am very obsessed with this.</p>

<p>Thank you very much!!</p>
]]></description>
   </item>
   <item>
      <title>Need help optimizing a collision detection method.</title>
      <link>https://forum.processing.org/two/discussion/20403/need-help-optimizing-a-collision-detection-method</link>
      <pubDate>Sun, 22 Jan 2017 04:51:33 +0000</pubDate>
      <dc:creator>AgustinQ</dc:creator>
      <guid isPermaLink="false">20403@/two/discussions</guid>
      <description><![CDATA[<p>Hi, I have written a collision detection method that detects collision of arbitrary shapes made by line segmentes in a PShape that is made with beginShape() endShape() method. The program is inspired by Daniel Shiffman's circle packing tutorial but i change it so it could pack arbitrary shapes (in this case Star Wars icons :P). But as more and more shapes are added the code runs really slow.</p>

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

<pre><code>    boolean isIntersectingWith(Circle other) {
        int thisVertexCount =shp.getVertexCount();
        int otherVertexCount = other.shp.getVertexCount();

        float distanceBetweenObjets = dist(x,y, other.x,other.y);
        if(distanceBetweenObjets &lt; r + other.r){  //if the distance between centers is large don't check segment by segment collision
        for (int i = 0; i &lt; thisVertexCount; i++) {
          int iPlusOne = i+1;
          if (iPlusOne&gt;= thisVertexCount) { //wraps around to make the segment of the last element and the first.
            iPlusOne -=thisVertexCount;
          }
          for (int j = 0; j &lt; otherVertexCount; j++) {
            int jPlusOne = j+1;
            if (jPlusOne&gt;= otherVertexCount) {
              jPlusOne -=otherVertexCount;
            }

            PVector thisP1, thisP2, otherP1, otherP2;
            thisP1 = new PVector(x, y);
            thisP1.add(shp.getVertex(i).mult(shapeScale));

            thisP2 = new PVector(x, y);
            thisP2.add(shp.getVertex(iPlusOne).mult(shapeScale));

            otherP1 = new PVector(other.x, other.y);
            otherP1.add(other.shp.getVertex(j).mult(other.shapeScale));

            otherP2 = new PVector(other.x, other.y);
            otherP2.add(other.shp.getVertex(jPlusOne).mult(other.shapeScale));


            if (lineIntersect(thisP1, thisP2, otherP1, otherP2) != null) {
              return true;
            }
          }
        }
        }
        return false;
      }

      PVector lineIntersect(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
        double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
        if (denom == 0.0) { // Lines are parallel.
          return null;
        }
        double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3))/denom;
        double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3))/denom;
        if (ua &gt;= 0.0f &amp;&amp; ua &lt;= 1.0f &amp;&amp; ub &gt;= 0.0f &amp;&amp; ub &lt;= 1.0f) {
          // Get the intersection point.
          return new PVector((int) (x1 + ua*(x2 - x1)), (int) (y1 + ua*(y2 - y1)));
        }

        return null;
      }
      PVector lineIntersect(PVector p1, PVector p2, PVector p3, PVector p4) {
        return lineIntersect(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y );
      }
</code></pre>

<p>Any suggestions are appreciated</p>

<p>Thanks</p>

<p>PS: sorry for my English its my second language</p>
]]></description>
   </item>
   <item>
      <title>Circles Collision</title>
      <link>https://forum.processing.org/two/discussion/20288/circles-collision</link>
      <pubDate>Sun, 15 Jan 2017 21:15:31 +0000</pubDate>
      <dc:creator>Murata</dc:creator>
      <guid isPermaLink="false">20288@/two/discussions</guid>
      <description><![CDATA[<p>hi i need help, i have some circles and when they collision i need the bigger one eat the small and the small disappear. Like the game agario, but i dont know how to do it.</p>

<pre lang="javascript">
//Draggable draggable;
PFont p;
NullDraggableObject nullDraggableObject;
ArrayList draggables;
PImage font;

void setup(){
    size(420,500);
    smooth();
    nullDraggableObject = new NullDraggableObject();
    draggables = new ArrayList();
    for (int i=0; i &lt; 10   ;i++){
      draggables.add(new Circle(random(width), random(height/2)));
    }
    font = loadImage("universe.jpeg");
    p = createFont("Arial",18);
    textFont(p);
}

void draw(){
   image(font,0,0);
   stroke(255);
   fill(0,255,0);
   text("Objects",10,height-40);
   noFill();
   strokeWeight(3);
   draggable = nullDraggableObject;
   for(int i=0; i &lt; draggables.size() ;i++){
       Draggable d = (Draggable)draggables.get(i);
       d.draw();
       if( d.isMouseOverFigure() )
          draggable = d;
   }
}

void mousePressed(){
   draggable.mousePressed();
}

void mouseDragged(){
   draggable.mouseDragged();
}

void mouseReleased(){
   draggable.mouseReleased();
}
</pre>

<pre lang="javascript">
// public class Circle  implements Draggable {
     float x;
     float y;
     float radio;
     boolean drag;
     float dragX;
     float dragY;
         Circle(float a, float b){
         this.x = a;
         this.y = b;
         this.radio = random(20,50);
         drag = false;
         dragX = 0;
         dragY = 0;
     }
     
     boolean isMouseOverFigure(){
         return in(mouseX, mouseY);
     }
     
     boolean in(float ix, float iy){
         return ( dist(this.x, this.y, ix, iy) &lt; this.radio );
     }
     
     void draw(){
        ellipseMode(CENTER);
        ellipse(x,y,2*radio,2*radio);
        
     }
     
     void mousePressed(){
         drag = in(mouseX, mouseY);
         if(drag){
             dragX = mouseX - x;
             dragY = mouseY - y;
         }
     }
     void mouseDragged(){
        if(drag){
           x = mouseX -dragX;
           y = mouseY - dragY;
        }
     }
     
     void mouseReleased(){
        drag = false;
     }
     
     
}     
</pre>

<pre lang="javascript">
interface Draggable{
     boolean isMouseOverFigure();
     boolean in(float ix, float iy);
     void draw();
     void mousePressed();
     void mouseReleased();
     void mouseDragged();
}
</pre>

<pre lang="javascript">

class NullDraggableObject implements Draggable{
     boolean isMouseOverFigure(){
         return false;
     }
     boolean in(float ix, float iy){
         return false;
     }
     void draw(){}
     void mousePressed(){}
     void mouseReleased(){}
     void mouseDragged(){}
   
}
</pre>
]]></description>
   </item>
   <item>
      <title>if / else statement</title>
      <link>https://forum.processing.org/two/discussion/20261/if-else-statement</link>
      <pubDate>Sat, 14 Jan 2017 18:40:23 +0000</pubDate>
      <dc:creator>jaspervanblokland</dc:creator>
      <guid isPermaLink="false">20261@/two/discussions</guid>
      <description><![CDATA[<p>Hi There,</p>

<p>I want to make a function where the different elements (images and shapes) does not overlap each other. The positions of the different elements are random. So in every circumstances (positions) the elements does not reach each other. I have to do this with a if / else statement. Can somebody help me with this issue? Thanks</p>
]]></description>
   </item>
   <item>
      <title>collision between objects in classes</title>
      <link>https://forum.processing.org/two/discussion/20124/collision-between-objects-in-classes</link>
      <pubDate>Sat, 07 Jan 2017 07:31:39 +0000</pubDate>
      <dc:creator>cloudi</dc:creator>
      <guid isPermaLink="false">20124@/two/discussions</guid>
      <description><![CDATA[<p>in my game when an asteroid hits the ship the ship will die. the problem is i dont know how to make the ship and asteroid collide when the x and y of both objects are in separate classes. should i make the variables global? also i have a minor thing, when you make a object out of a class you use something like this right? (assuming asteroid is a class and it is made to do something)</p>

<p><code>Asteroid drawAsteroid;
  void setup() {
  drawAsteroid = new Asteroid();
  }</code></p>

<p>lets say i wanted two asteroids that had different x values (lets say the different x values were already coded, just focus on the two asteroids)i would do something like this right?</p>

<p><code>Asteroid drawAsteroid1;
  Asteroid drawAsteroid2;
  void setup(){
  drawAsteroid1 = new Asteroid();
  drawAsteroid2 = new Asteroid();
  }</code></p>

<p>Well i could just make a new asteroid every time i wanted 1 more, is there a simpler way to create more (like if i had to make 1000000000 asteroids and was on a time schedule meaning i couldnt code 1 million asteroids manually) how would i do that?</p>

<p>Thanks - cloudi</p>
]]></description>
   </item>
   <item>
      <title>Array of Bouncing objects</title>
      <link>https://forum.processing.org/two/discussion/20216/array-of-bouncing-objects</link>
      <pubDate>Thu, 12 Jan 2017 16:31:19 +0000</pubDate>
      <dc:creator>wjsandbe</dc:creator>
      <guid isPermaLink="false">20216@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to make a simple version of pong. My code works when I simply draw a single ellipse and code it to bounce off of the Paddle (rectangle), but when I create an array of balls (ellipses) the balls no longer bounce off of the paddle. I'm not sure where I'm going wrong, I thought I followed the P5.js intersecting objects tutorial pretty well. Again I want any Ball from the array to bounce off of the Paddle, if the objects intersect.</p>

<pre><code>var gravity = 0.1;
var balls = [];
var n = 0;


function setup() {
  createCanvas(400, 400);
  Paddle = new Paddle(180, height / 4 * 3);
  for (n = 0; n &lt; 7; n++) {
    append(balls, new Ball(random(width), random(height)));
  }
}

function draw() {
  background(51);
  Paddle.display();
  Paddle.borders();

  for (n = 0; n &lt; balls.length; n++) {
    balls[n].display();
    balls[n].move();
  }

for (i = 0; i &lt; balls.length; i++){
 if (balls[i].intersects(Paddle)) {
   balls[n].bounce();
 } 
}

}

function mouseClicked() {
  if (n &lt; 9 &amp;&amp; mouseY &lt; height - 50) {
    append(balls, new Ball(mouseX, mouseY));
  }
}

function keyPressed() {
  if (keyCode === RIGHT_ARROW) {
    Paddle.move(1);
  } else if (keyCode === LEFT_ARROW) {
    Paddle.move(-1);
  }
}

function Paddle(x, y) {
  this.x = x;
  this.y = y;
  this.r = 25;

  this.move = function(dir) {
    this.x += dir * 15;
  }
  this.borders = function() {
    if (this.x &gt; width)
      this.x = 0;
    if (this.x &lt; 0)
      this.x = width;
  }
  this.display = function() {
    stroke(0, 255, 0);
    fill(0, 255, 0);
    rectMode(CENTER);
    rect(this.x, this.y, this.r * 4, this.r * 2)
  }
}

function Ball(x, y) {
  this.x = x;
  this.y = y;
  this.r = 25;
  this.yspeed = 0.1;
  //  this.toBounce = false;

  this.move = function() {
    this.y += this.yspeed;
    this.yspeed += gravity;

    if (this.y &gt; height) {
      this.y = height;
      this.yspeed *= -1
    }
    if (this.y &lt; 0) {
      this.y = 0;
      this.yspeed *= -1
    }
  }
    this.intersects = function(Paddle) {
    var d = dist(Ball.x, Ball.y, Paddle.x, Paddle.y);
    if (d &lt; Ball.r + Paddle.y) {
      return true;
    } else {
      return false;
    }
  }
  this.bounce = function() {
    this.y = this.y *= -1
    this.yspeed *= -1;
  }

  this.display = function() {
    stroke(255)
    fill(255, 0, 0);
    ellipse(this.x, this.y, this.r * 2, this.r * 2);
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How do I make the lives go down by 1 when the green rectangle touches one of the red one?</title>
      <link>https://forum.processing.org/two/discussion/20112/how-do-i-make-the-lives-go-down-by-1-when-the-green-rectangle-touches-one-of-the-red-one</link>
      <pubDate>Sat, 07 Jan 2017 00:03:41 +0000</pubDate>
      <dc:creator>johnathan27777</dc:creator>
      <guid isPermaLink="false">20112@/two/discussions</guid>
      <description><![CDATA[<p>tab 1:
ArrayList &lt; Car &gt; cars;
PFont f; 
float x1, y1, lives1;
boolean x2;</p>

<p>void setup () {
  size(700, 700);
  cars = new ArrayList();
  for (int count=0; count &lt; 20; count=count+1) {
    cars.add(new Car());
  }
  f=createFont("Arial", 24, true);
  x1= width-690;
  y1=height-10;
  x2=true;
  lives1=10;
}</p>

<p>void draw () {
  background(255);
  fill(#0EFF03);
  rect(mouseX, mouseY, 30, 50) ;
  lives() ;
  for (int count=0; count &lt; 20; count=count+1) {
    cars.get(count).display();
    cars.get(count).move();
  }
}</p>

<p>void lives() {
  textFont(f);
  fill(245, 200, 0);
  text("lives:" + lives1, x1, y1);
}</p>

<p>tab 2:
class Car {
  float x, y;
  float speed;</p>

<p>Car() {
    x = random(10, width-10);
    y = 0; 
    speed = random(1, 5);
  }</p>

<p>void display () {
    fill(255, 0, 0);
    noStroke();
    rect(x, y, 30, 50);
  }</p>

<p>void move() {
    y = y + speed;
    if(y &gt; height) {
      y = 0;
      x = random(10, width-10);
    }
  }</p>

<p>void losinglives1(){
 if (dist(x, y, mouseX, mouseY)&lt;15) {
   lives1=lives1-1;
  }
}
}</p>
]]></description>
   </item>
   <item>
      <title>I need a simple ball collision code</title>
      <link>https://forum.processing.org/two/discussion/19916/i-need-a-simple-ball-collision-code</link>
      <pubDate>Sun, 25 Dec 2016 17:00:17 +0000</pubDate>
      <dc:creator>chrisah</dc:creator>
      <guid isPermaLink="false">19916@/two/discussions</guid>
      <description><![CDATA[<p>I am making agar.io and I need a simple collision code for the 10 by 10 food cells and my main ball in the middle of the screen that grows in size.</p>
]]></description>
   </item>
   <item>
      <title>Rectangle with Ball Collision</title>
      <link>https://forum.processing.org/two/discussion/19778/rectangle-with-ball-collision</link>
      <pubDate>Fri, 16 Dec 2016 13:45:23 +0000</pubDate>
      <dc:creator>TheLoser</dc:creator>
      <guid isPermaLink="false">19778@/two/discussions</guid>
      <description><![CDATA[<p>I am pretty new to processing and programming, so please bear with me.
I want to create a program, where there are 7 rectangles and maximum 15 balls.</p>

<p>The rectangles should not have collision with others, and the balls with rectangles too.</p>

<p>I have created the rectangles, they come down, and when they reached the end of the page, they start over, and the Ball object will be created every time the user click the mouse, till the number of balls reach 15.</p>

<p>I know the code doesn't look good, still new and learning.</p>

<p>Thanks a lot.</p>

<p>There is the code I have written so far:</p>

<pre><code>import java.util.Arrays;
import java.util.Collections;
import java.util.Vector;

float e = 1.0;
ArrayList&lt;Particle&gt; particles;
float radious = random(10, 50);
PVector loc = new PVector(random(radious, width - radious), random(radious, height - radious));
PVector currentPos;
Brick brick_1 = new Brick(random(650), random(650), 2);
ArrayList&lt;Brick&gt; bricks = new ArrayList&lt;Brick&gt;();

void setup() {
size(800, 800);
frameRate(60);
particles = new ArrayList&lt;Particle&gt;();
while (bricks.size() &lt; 7) {
bricks.add(new Brick(random(650), random(650), 2));
}
}

void draw() {
background(0);

// Ball Object START
for (Particle p : particles) {
p.display();
}
for (Particle p : particles) {
p.collide();
}
for (Particle p : particles) {
p.update();
}
for (int i = 0; i &lt; 7; i++) {
Brick one = bricks.get(i);
one.display();
}
ArrayList&lt;Float&gt; list = new ArrayList&lt;Float&gt;();
Brick first_brick = bricks.get(0);
Brick second_brick = bricks.get(1);
Brick third_b = bricks.get(2);
Brick forth_b = bricks.get(3);
Brick fifth_b = bricks.get(4);
Brick sixth_b = bricks.get(5);
Brick seventh_b = bricks.get(6);
list.add(first_brick.getPosition());
list.add(second_brick.getPosition());
list.add(third_b.getPosition());
list.add(forth_b.getPosition());
list.add(fifth_b.getPosition());
list.add(sixth_b.getPosition());
list.add(seventh_b.getPosition());
float this_is_the_min = Collections.min(list);
if (this_is_the_min &gt; height + 20) {
bricks.clear();
for (int i = 0; i &lt;7; i++) {
bricks.add(new Brick(random(650), random(650), 2));
}
}
}
void mousePressed() {
if (particles.size() &lt; 15) {
float radious = random(10, 50);
PVector loc = new PVector(random(radious, width - radious), random(radious, height - radious));
particles.add( new Particle(loc, radious));
}
}


Brick Class

class Brick {
float x;
float y;
float bx;
float by;
float vbx = -1;
float vby = -1; //vbx is fixed;
color c;
Brick (float bx, float by, float vby) {
this.bx=bx;
this.by=by;
this.vby=vby;
c=color(random(255), random(255), random(255));
}

void display() {
fill(c);
noStroke();
rect(bx, by, 100, 30);
by = by + vby;
}

float getPosition () {
PVector v1 = new PVector(0, 0, 0);
PVector buffer = new PVector (bx, by);
float d = buffer.dist(v1);
return d;
}
}

Ball Class (taken from open processing)

class Particle {

PVector loc, vel, nvel;
float radious;
float mass;
color c;
Particle(PVector _loc, float _radious) {
loc = _loc;
float velSize = random(0.5, 3);
float velAngle = random(TWO_PI);
vel = new PVector(velSize * cos(velAngle), velSize * sin(velAngle));
radious = _radious;
mass = sq(radious) * PI;
c=color(random(255), random(255), random(255));
}

void display() {
fill(c);
noStroke();
ellipse(loc.x, loc.y, radious * 2, radious * 2);
}

void collide() {
nvel = new PVector(vel.x, vel.y);
for (Particle p : particles) {
if (p != this &amp;&amp; PVector.dist(loc, p.loc) &lt; radious + p.radious) {
nvel = PVector.sub(loc, p.loc);
nvel.normalize();
nvel.mult(PVector.dot(PVector.sub(p.vel, vel), nvel));
nvel.mult(1 + e);
nvel.mult(p.mass / (mass + p.mass));
nvel.add(vel);
break;
}
}
}

void update() {
vel = nvel;
loc.add(vel);
if (loc.x &lt; radious) {
vel.x *= -1;
loc.x += vel.x;
}
if (loc.x &gt; width - radious) {
vel.x *= -1;
loc.x += vel.x;
}
if (loc.y &lt; radious) {
vel.y *= -1;
loc.y += vel.y;
}
if (loc.y &gt; height - radious) {
vel.y *= -1;
loc.y += vel.y;
}
}
}
</code></pre>
]]></description>
   </item>
   </channel>
</rss>