<?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 #objects - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=%23objects</link>
      <pubDate>Sun, 08 Aug 2021 19:19:37 +0000</pubDate>
         <description>Tagged with #objects - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/tagged%23objects/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>How to remove object?</title>
      <link>https://forum.processing.org/two/discussion/3304/how-to-remove-object</link>
      <pubDate>Wed, 26 Feb 2014 02:49:22 +0000</pubDate>
      <dc:creator>Lunaria</dc:creator>
      <guid isPermaLink="false">3304@/two/discussions</guid>
      <description><![CDATA[<p>Hi, guys.
I want to remove object when some condition is completed.</p>

<p>For example, the rectangle moves to any direction for each second.
And when the rectangle meets the border line, the rectangle should be removed.</p>

<p>For now, I overdrew background-colored-object on the object.
But, if I do this way, the memory would be gained, so the program would be heavy.</p>

<p>So, I'm looking forward about removing the objects.
But even I googled the method, there wasn't any method about the above.</p>

<p>If anyone has good idea, please give it to me.
Thanks for reading. :)</p>
]]></description>
   </item>
   <item>
      <title>Could someone help me with my tetris game?</title>
      <link>https://forum.processing.org/two/discussion/19506/could-someone-help-me-with-my-tetris-game</link>
      <pubDate>Sun, 04 Dec 2016 15:32:57 +0000</pubDate>
      <dc:creator>Rophi</dc:creator>
      <guid isPermaLink="false">19506@/two/discussions</guid>
      <description><![CDATA[<p>I fixed the problem myself now, don't need help anymore. (Don't know hot to delete it)</p>
]]></description>
   </item>
   <item>
      <title>Pool ball jitters when hit by the cue ball</title>
      <link>https://forum.processing.org/two/discussion/19543/pool-ball-jitters-when-hit-by-the-cue-ball</link>
      <pubDate>Mon, 05 Dec 2016 22:54:25 +0000</pubDate>
      <dc:creator>BootlegFireworks</dc:creator>
      <guid isPermaLink="false">19543@/two/discussions</guid>
      <description><![CDATA[<p>I am new to Processing and new to programming in general and I am making a pool table as an end of term project. I managed to get the cue ball to hit another ball, but the cue ball only hits successfully once. When I try to hit the cue ball again, whenever it passes by the original position of the ball it jitters.</p>

<p>`class Point
{
  float x, y;
  Point (float a, float b)
  {
    x = a;
    y = b;
  }
  Point()
  {
    x = 0;
    y = 0;
  }
}</p>

<p>class Ball
{
  float rad;
  Point center;
  Point contact_point;
  color col;
  Point speed;</p>

<p>int[][] ballColor =
    {
    {255, 255, 255}, // 0 = cue ball
    {255, 255, 0}, //1 ball, 9 ball
    {0, 0, 255}, //2 ball, 10 ball
    {255, 0, 0}, //3 ball, 11 ball
    {128, 0, 128}, //4 ball, 12 ball
    {255, 165, 0}, //5 ball, 13 ball
    {0, 150, 0}, //6 ball, 14 ball
    {153, 76, 0}, //7 ball, 15 ball
    {50, 50, 50} //8 ball
  };</p>

<p>Ball (float x, float y, float r, int i)
  {
    speed = new Point();
    center = new Point (x, y);
    contact_point = new Point (mouseX, mouseY);
    rad = r;
    col = color (ballColor[i][0], ballColor[i][1], ballColor[i][2]);
  }
  void circle()
  {
    ellipse(0, 0, rad, rad);
  }
}</p>

<p>class Stick
{
  Point start_p;
  Point end_p;
  color col;
  int length;
  float distance;</p>

<p>Stick (float x1, float y1, float x2, float y2, int R, int G, int B, int L)
  {
    length = L;
    start_p = new Point (x1, y1);
    end_p = new Point (x2, y2);
    col = color(R, G, B);
    distance = 0;
  }
}</p>

<p>class Table
{
  Ball [] b_arr;
  Ball [] stripe_arr;
  Ball cue_ball;
  Stick st;
  Ball eightBall;</p>

<p>Table()
  {
    cue_ball = new Ball (500, 500, 15, 0);
    eightBall = new Ball (1060, 500, 15, 8);</p>

<pre><code>float [][] position = 
  {
  {1000, 500}, 
  {1030, 484}, 
  {1030, 516}, 
  {1060, 469}, 
  //{1060, 500},
  {1060, 531}, 
  {1090, 484}, 
  {1090, 516}, 
};

b_arr = new Ball [7];
for (int i = 0; i &lt; b_arr.length; i++)
{
  b_arr[i] = new Ball (position[i][0], position[i][1], 15, i+1);
  //println(b_arr[i]);
}

//stripe_arr =
st = new Stick (cue_ball.center.x, cue_ball.center.y, mouseX, mouseY, 0, 0, 0, 50);
</code></pre>

<p>}
}</p>

<p>PImage floor, background;
float deceleration = 0.89;
int power, gamemode = 0;
Table table = new Table();
boolean ballInHole = false, ballWall = false, cueBall = false;</p>

<p>import ddf.minim.*;
AudioSnippet wall, cue, ballBounce;
Minim minim;</p>

<p>void setup()
{
  size(1500, 1000);
  floor = loadImage("floor.jpg");
  ellipseMode(CENTER);
  frameRate(24);</p>

<p>minim = new Minim(this);
  wall = minim.loadSnippet("sport_miniture_snooker_pool_ball_hit_table_wall.mp3");
  cue = minim.loadSnippet("sport_pool_snooker_cue_strike_ball.mp3");
}</p>

<p>void draw()
{
  if (gamemode == 0)
  {
    background(255);
    fill(0);
    textSize(200);
    text("BILLIARDS", 100, 300);
    textSize(24);
    text("INSTRUCTIONS", 100, 400);
    text("Press arrow keys to change power of shot", 100, 450);
    text("Press Shift in order to choose the direction of shot", 100, 500);
    text("Click to shoot", 100, 550);
    textSize(50);
    text("Click anywhere to continue", 400, 900);
  }</p>

<p>if (gamemode == 2)
  {
    image(floor, 0, 0);
    table.st.end_p.x = mouseX;
    table.st.end_p.y = mouseY;
    poolTable();
    drawCueBall();
    tempBall(0);
    //drawBalls();
    //drawEightBall();
    drawPole();
    checkBall();
    //checkBallCollision(table.b_arr, table.cue_ball);
    checkCueBallCollision (table.b_arr);</p>

<pre><code>power = powerControl(power);
DisplayPower();

if (ballWall == true)
{
  wall.rewind();
  wall.play();
  ballWall = false;
}

if (cueBall == true)
{
  cue.rewind();
  cue.play();
  cueBall = false;
}
</code></pre>

<p>}</p>

<p>if (gamemode == 3)
  {
    background(255);
    textSize(200);
    fill(0);
    text("GAME OVER", 200, 500);
    textSize(50);
    text("The cue ball went into the pocket!", 300, 750);
    //text("Click Anywhere to continue", 400, 900);
  }
}</p>

<p>void mousePressed()
{
  if (gamemode == 0)
    gamemode = 1;
  if (gamemode == 1)
    gamemode = 2;
  if (gamemode == 3)
  {
    gamemode = 0;
    ballInHole = false;
  }
}</p>

<p>void drawCueBall()
{
  stroke(0);
  strokeWeight(1);
  fill(table.cue_ball.col);
  ellipse(table.cue_ball.center.x, table.cue_ball.center.y, table.cue_ball.rad<em>2, table.cue_ball.rad</em>2);
  table.cue_ball.center.x += table.cue_ball.speed.x;
  table.st.start_p.x += table.cue_ball.speed.x;
  table.cue_ball.center.y += table.cue_ball.speed.y;
  table.st.start_p.y += table.cue_ball.speed.y;
  table.cue_ball.speed.x *= deceleration;
  table.cue_ball.speed.y *= deceleration;</p>

<p>if (ballInHole == false)
    checkBoundaryCollision(table.cue_ball);</p>

<p>if (abs(table.cue_ball.speed.x) &lt; 0.1)
  {
    table.cue_ball.speed.x = 0;
  }
  if (abs(table.cue_ball.speed.y) &lt; 0.1)
  {
    table.cue_ball.speed.y = 0;
  }
}</p>

<p>void drawEightBall()
{
  stroke(0);
  strokeWeight(1);
  fill(table.eightBall.col);
  ellipse(table.eightBall.center.x, table.eightBall.center.y, table.eightBall.rad<em>2, table.eightBall.rad</em>2);
}</p>

<p>void tempBall (int i)
{
  stroke(0);
  strokeWeight(1);
  fill(table.b_arr[i].col);
  ellipse(table.b_arr[i].center.x, table.b_arr[i].center.y, table.b_arr[i].rad<em>2, table.b_arr[i].rad</em>2);</p>

<p>table.b_arr[i].center.x += table.b_arr[i].speed.x;
  table.b_arr[i].center.y += table.b_arr[i].speed.y;
  table.b_arr[i].speed.x *= deceleration;
  table.b_arr[i].speed.y *= deceleration;</p>

<p>checkBoundaryCollision(table.b_arr[i]);</p>

<p>if (abs(table.b_arr[i].speed.x) &lt; 0.1)
    table.b_arr[i].speed.x = 0;
  if (abs(table.b_arr[i].speed.y) &lt; 0.1)
    table.b_arr[i].speed.y = 0;
}</p>

<p>void drawBalls()
{
  stroke(0);
  strokeWeight(1);
  for (int i = 0; i &lt; table.b_arr.length; i++)
  {
    fill(table.b_arr[i].col);
    ellipse(table.b_arr[i].center.x, table.b_arr[i].center.y, table.b_arr[i].rad<em>2, table.b_arr[i].rad</em>2);
  }
}</p>

<p>void drawPole()
{
  if (keyCode == SHIFT &amp;&amp; table.cue_ball.speed.x == 0 &amp;&amp; table.cue_ball.speed.y == 0)
  {
    PVector mouse = new PVector (mouseX, mouseY);
    PVector center = new PVector (table.cue_ball.center.x, table.cue_ball.center.y);
    mouse.sub(center);
    mouse.normalize();
    mouse.mult(150);
    translate (center.x, center.y);
    stroke(table.st.col);
    line(0, 0, mouse.x, mouse.y);
    mouse.mult(0.067);
    stroke(255);
    strokeWeight(5);
    line(0, 0, mouse.x, mouse.y);
  }
}</p>

<p>void mouseClicked()
{
  if (table.cue_ball.speed.x == 0 &amp;&amp; table.cue_ball.speed.y == 0)
  {
    PVector mouse = new PVector(mouseX, mouseY);
    PVector center = new PVector(table.cue_ball.center.x, table.cue_ball.center.y);
    mouse.sub(center);
    println(mouse); //temporary
    mouse.normalize();
    mouse.mult(power);
    table.cue_ball.speed.x = -mouse.x;
    table.cue_ball.speed.y = -mouse.y;
    if (table.cue_ball.speed.x != 0 || table.cue_ball.speed.y != 0)
      cueBall = true;
  }
}</p>

<p>int powerControl (int power)
{
  if (keyCode == UP)
  {
    if (power &lt;= 95)
    {
      power += 5;
    }
    keyCode = LEFT;
  }
  if (keyCode == DOWN)
  {
    if (power &gt;= 5)
    { 
      power -= 5;
    }
    keyCode = LEFT;
  }
  return power;
}</p>

<p>void DisplayPower()
{
  textSize(24);
  fill(0);
  text("Power", 750, 32);
  text(power, 750, 64);
}</p>

<p>void poolTable()
{
  int a = 1000, b = a/2, c = width/2, d = height/2;
  rectMode(CENTER);</p>

<p>fill(102, 51, 0);
  rect(c, d, a+125, b+125, 32, 32, 32, 32);</p>

<p>fill(0, 200, 0);
  rect(c, d, a, b);</p>

<p>fill(255);
  for (int i = 0; i &lt;= a; i += (a/8))
  {
    if (i != a/2 &amp;&amp; 1 != 0 &amp;&amp; i != a)
    {
      ellipse((c-b) + i, (d/2)-23, 10, 10);
      ellipse((c-b) + i, (d+(d/2))+23, 10, 10);
    }
  }
  for (int j = 0; j &lt;= b; j += (b/4))
  {
    if (j != 0 &amp;&amp; j != b)
    {
      ellipse((c-b)-23, (d/2)+j, 10, 10);
      ellipse((c+b)+23, (d/2)+j, 10, 10);
    }
  }</p>

<p>fill(0);
  for (int k = 0; k &lt;= a; k += (a/2))
  {
    ellipse((c-b) + k, d/2, 64, 64);
    ellipse((c-b) + k, d+(d/2), 64, 64);
  }
}</p>

<p>void checkBoundaryCollision(Ball ball)
{
  if (ball.center.x &gt; 1250-ball.rad)
  {
    ball.center.x = 1250-ball.rad;
    ball.speed.x *= -1;<br />
    ballWall = true;
  } else if (table.cue_ball.center.x &lt; 250+table.cue_ball.rad)
  {
    ball.center.x = 250+ball.rad;
    ball.speed.x *=-1;
    ballWall = true;
  } else if (table.cue_ball.center.y &gt; 750-table.cue_ball.rad)
  {
    ball.center.y = 750-ball.rad;
    ball.speed.y *= -1;
    ballWall = true;
  } else if (table.cue_ball.center.y &lt; 250+table.cue_ball.rad)
  {
    ball.center.y = 250+ball.rad;
    ball.speed.y *= -1;
    ballWall = true;
  }
}</p>

<p>void checkBall()
{
  if (table.cue_ball.center.x &lt; 250+24 &amp;&amp; table.cue_ball.center.y &lt; 250+24)
  {
    ballInHole = true;
    if (ballInHole == true)
    {
      table.cue_ball.speed.x = 0;
      table.cue_ball.speed.y = 0;
      table.cue_ball.center.x = 250;
      table.cue_ball.center.y = 250;
      gamemode = 3;
    }
  }
  if ((table.cue_ball.center.x &gt; 750-24 &amp;&amp; table.cue_ball.center.x &lt; 750+24) &amp;&amp; table.cue_ball.center.y &lt; 250+24)
  {
    ballInHole = true;
    if (ballInHole == true)
    {
      table.cue_ball.speed.x = 0;
      table.cue_ball.speed.y = 0;
      table.cue_ball.center.x = 750;
      table.cue_ball.center.y = 250;
      gamemode = 3;
    }
  }
  if (table.cue_ball.center.x &gt; 1250-24 &amp;&amp; table.cue_ball.center.y &lt; 250+24)
  {
    ballInHole = true;
    if (ballInHole == true)
    {
      table.cue_ball.speed.x = 0;
      table.cue_ball.speed.y = 0;
      table.cue_ball.center.x = 1250;
      table.cue_ball.center.y = 250;
      gamemode = 3;
    }
  }
  if (table.cue_ball.center.x &lt; 250+24 &amp;&amp; table.cue_ball.center.y &gt; 750-24)
  {
    ballInHole = true;
    if (ballInHole == true)
    {
      table.cue_ball.speed.x = 0;
      table.cue_ball.speed.y = 0;
      table.cue_ball.center.x = 250;
      table.cue_ball.center.y = 750;
      gamemode = 3;
    }
  }
  if ((table.cue_ball.center.x &gt; 750-24 &amp;&amp; table.cue_ball.center.x &lt; 750+24) &amp;&amp; table.cue_ball.center.y &gt; 750-24)
  {
    ballInHole = true;
    if (ballInHole == true)
    {
      table.cue_ball.speed.x = 0;
      table.cue_ball.speed.y = 0;
      table.cue_ball.center.x = 750;
      table.cue_ball.center.y = 750;
      gamemode = 3;
    }
  }
  if (table.cue_ball.center.x &gt; 1250-24 &amp;&amp; table.cue_ball.center.y &gt; 750-24)
  {
    ballInHole = true;
    if (ballInHole == true)
    {
      table.cue_ball.speed.x = 0;
      table.cue_ball.speed.y = 0;
      table.cue_ball.center.x = 1250;
      table.cue_ball.center.y = 750;
      gamemode = 3;
    }
  }
}</p>

<p>float distance (float a, float b)
{
  float temp = sqrt(sq(a)+sq(b));
  return temp;
}</p>

<p>void checkCueBallCollision(Ball[] ball)
{
  for (int i = 0; i &lt; ball.length; i++)
  {
    float difx = ball[i].center.x - table.cue_ball.center.x;
    float dify = ball[i].center.y - table.cue_ball.center.y;
    float totalDistance = distance (difx, dify);
    if (table.cue_ball.speed.x == 0 &amp;&amp; table.cue_ball.speed.y == 0)
      println(totalDistance);</p>

<pre><code>if (totalDistance &lt;= table.cue_ball.rad*2+ball[i].rad*2)
{
  float knum = ((table.cue_ball.speed.x-ball[i].speed.x)*(table.cue_ball.center.x-ball[i].center.x))+((table.cue_ball.speed.y-ball[i].speed.y)*(table.cue_ball.center.y-ball[i].center.y));
  float kden = sq(table.cue_ball.center.x-ball[i].center.x)+sq(table.cue_ball.center.y-ball[i].center.y);
  float k = knum/kden;
  float x = table.cue_ball.center.x-ball[i].center.x;
  float y = table.cue_ball.center.y-ball[i].center.y;

  table.cue_ball.speed.x -= (k*x);
  table.cue_ball.speed.y -= (k*y);
  ball[i].speed.x += (k*x);
  ball[i].speed.y += (k*y);
}
</code></pre>

<p>}
}`</p>

<p>On an unrelated note, did I format this code right on this forum?</p>
]]></description>
   </item>
   <item>
      <title>Make objects/shapes appear on top of other objects/shapes dynamically</title>
      <link>https://forum.processing.org/two/discussion/16297/make-objects-shapes-appear-on-top-of-other-objects-shapes-dynamically</link>
      <pubDate>Wed, 27 Apr 2016 17:48:21 +0000</pubDate>
      <dc:creator>Stencilboy</dc:creator>
      <guid isPermaLink="false">16297@/two/discussions</guid>
      <description><![CDATA[<p>I have a bunch of objects that are square videos(in an array). If the mouse hovers over one of them - it will expand (and play). the problem I have is that if an object expands but there is an object next to it and that object happens to be run after the expanding one in draw then that object will always be on top of the other object.</p>

<p>Is there a way to dynamically change the order in which draw runs? So I could say if object active - run last in draw (so its on top of all other objects etc.</p>

<p>Thanks for the help! (code below - although you would need to replace the mp3 and  .mov in lines 22/23 to run this, and I have the video library and minim installed)</p>

<pre><code>import processing.video.*;

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

Minim minim;
AudioSample sample;
Tile[]Tiles = new Tile[10]; 

void setup() {
  fullScreen(2);
  //noCursor();
  imageMode(CENTER);
  minim = new Minim(this);

  for (int i = 0; i&lt; 10; i++){
    Tiles[i]= new Tile(this);
    Tiles[i].setMovie("TORCH_2_1.mov");
    Tiles[i].setAudio(minim, "Paper Rustle_1.mp3");
  }


}

void draw() {
  background(0);

  Tiles[0].presentTile(200, 200);
  Tiles[1].presentTile(225, 200);
  Tiles[2].presentTile(250, 200);
  Tiles[3].presentTile(275, 200);
  Tiles[4].presentTile(300, 200);
  Tiles[5].presentTile(325, 200);

}

class Tile {
  PApplet app;
  Movie movie = null;
  AudioSample sample = null;

  boolean overBox = false;
  boolean canTrigger = true;
  float boxMax = 100;
  float boxMin = 25;
  int boxSize;

  Tile(PApplet papp) {
    app = papp;
    boxSize = 25;
  }

  void setMovie(String fname) {
    movie = new Movie(app, fname);
    movie.loop();
    movie.pause();
  }

  void setAudio(Minim m, String fname) {
    sample = m.loadSample(fname, 512);
  }

  void isOver(float x, float y) {
    overBox = (mouseX &gt; x-(boxSize/2) &amp;&amp; mouseX &lt; x +(boxSize/2) &amp;&amp;  
    mouseY &gt; y-(boxSize/2) &amp;&amp; mouseY &lt; y+(boxSize/2)); 
  }


  void presentTile(float bx, float by) {

    isOver(bx, by);
    if (movie != null) {
      if (movie.available() == true) 
        movie.read(); 
      if (overBox &amp;&amp; (boxSize &lt; boxMax)) {
        image(movie, bx, by, boxSize++, boxSize++);
        movie.loop();
      }
      if (!overBox &amp;&amp; boxSize &gt; boxMin ) {
        image(movie, bx, by, boxSize-=1, boxSize-=1);
        movie.pause();

      }
      image(movie, bx, by, boxSize, boxSize);
    }
    if (sample != null) {
      if (canTrigger &amp;&amp; overBox) {
        sample.trigger();
        canTrigger = false;
      }
      if (!overBox) {
        sample.stop();
        canTrigger = true;
      }
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>updatePixels() for multiple objects.</title>
      <link>https://forum.processing.org/two/discussion/8900/updatepixels-for-multiple-objects</link>
      <pubDate>Tue, 06 Jan 2015 02:13:17 +0000</pubDate>
      <dc:creator>AaronR</dc:creator>
      <guid isPermaLink="false">8900@/two/discussions</guid>
      <description><![CDATA[<p>Hello all.</p>

<p>I have a bouncing ball displaying over an image of the hit 90's R&amp;B group, Boyz II Men (eh, I needed an image of anything).</p>

<p>To my ball class, there are several functions assigned. The ball collection array allows for many objects to be loaded as desired. Here, I chose 4 balls... one for each Boyz II Men singers (sure, why not?)</p>

<p>However, when I add in the brightness function to the update display, I only see one object, even though there is more than one object printing coordinates in the console.</p>

<pre><code>//int n = 1000;
PImage img;


//DECALRE
Ball [] ballCollection = new Ball [4];



void setup ()  {
  size(600,600);
  frameRate(30);
  img = loadImage("/assets/Boyz2Men.jpg");
  img.loadPixels();
  loadPixels();
  smooth();

  //INITIALIZE
  for (int i = 0; i &lt; ballCollection.length; i++)  {
  ballCollection[i] =new Ball(random(0,width), random(0,200));
  }


}

void draw()  {
  background(0);
  image(img, 0, 0);
  //CALL FUNCTIONALITY
  for (int i = 0; i &lt; ballCollection.length; i ++)  {
  ballCollection[i].run();
  print(i, "\n");
  //updatePixels();
  }


}



class Ball {
  //GLOBAL VARIABLES
  float x = 0;
  float y = 0;
  float speedX = 4;
  float speedY = 0.5;

  //CONSTRUCTOR - INITIALIZE VARIABLES
  Ball(float _x, float _y) {
    x = _x;
    y = _y;
  }

  //FUNCTIONS - 
  void run() {
    display();
    move();
    bounce();
    gravity();
  }

  void move() {
    x += speedX; 
    y += speedY;
  }

  void bounce() {
    if (x &gt; width) {
      speedX = speedX * -1;
    }
    if (x &lt; 0) {
      speedX = speedX * -1;
    }
    if (y &gt; height) {
      speedY = speedY * -1;
    }
    if (y &lt; 0) {
      speedY = speedY * -1;
    }
  }

  void gravity() {
    speedY += 0.2;
  }


  void display() {
    print("X is ", x, " ", "Y is ", y, "\n");
    for (int x2 = 0; x2 &lt; img.width; x2++) {
      for (int y2 = 0; y2 &lt; img.height; y2++ ) {
        // Calculate the 1D location from a 2D grid
        int loc = x2 + y2*img.width;
        // Get the R,G,B values from image
        float r, g, b;
        r = red (img.pixels[loc]);
        //g = green (img.pixels[loc]);
        //b = blue (img.pixels[loc]);
        // Calculate an amount to change brightness based on proximity to the mouse
        float maxdist = 50;//dist(0,0,width,height);
        float d = dist(x, y, x2, y2);
        //print(d, "\n");
        float adjustbrightness = 255*(maxdist-d)/maxdist;
        r += adjustbrightness;
        //g += adjustbrightness;
        //b += adjustbrightness;
        // Constrain RGB to make sure they are within 0-255 color range
        r = constrain(r, 0, 255);
        //g = constrain(g, 0, 255);
        //b = constrain(b, 0, 255);
        // Make a new color and set pixel in the window
        //color c = color(r, g, b);
        color c = color(r);
        pixels[y2*width + x2] = c;

      }
    }
    updatePixels();
    ellipse(x, y, 20, 20);
  }
}
</code></pre>

<p>What is the reason as to why only one ball shows up? If I comment out the last for loop, I can see the picture clearly. Do I need to make another function called void bright()?</p>

<p>Thanks for any suggestions, I am stumped.</p>

<p>-Aaron</p>
]]></description>
   </item>
   <item>
      <title>Gravity help?</title>
      <link>https://forum.processing.org/two/discussion/13444/gravity-help</link>
      <pubDate>Sun, 08 Nov 2015 21:01:54 +0000</pubDate>
      <dc:creator>OreoEating</dc:creator>
      <guid isPermaLink="false">13444@/two/discussions</guid>
      <description><![CDATA[<p>Hey, so I have a question about the gravity of two objects acting on one object. I heard that you just need to find the net gravitational force, but it's not working for me, as the ball just goes in a different direction. Help, please?</p>

<pre><code>PVector location1;
PVector velocity1;
PVector acceleration1;
PVector locationSun;
PVector locationSun2;
PVector distance;
float r;
float r1;
float vSun = 1;

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

  location1 = new PVector(350, 30);
  velocity1 = new PVector(6, 0);
  acceleration1 = new PVector(0, 0);

  locationSun = new PVector(100, 300);
  locationSun2 = new PVector(500, 300);
}

void draw() {
  background(0);
  r = dist(location1.x, location1.y, locationSun.x+locationSun2.x+100, locationSun.y+locationSun2.y+100);

  if ( r &lt; 50 ) {
    r = 50;
  }

 /* println(acceleration1.x);
  println(locationSun.x);
  println(acceleration1.x);
  println(acceleration1.x);*/

  acceleration1.x = ((locationSun.x - location1.x)*100000/pow(r, 3)) + (locationSun2.x - location1.x)*100000/pow(r, 3);
  acceleration1.y = ((locationSun.y - location1.y)*100000/pow(r, 3)) + (locationSun2.y - location1.y)*100000/pow(r, 3);

  velocity1.add(acceleration1);
  location1.add(velocity1);

  //locationSun.x += vSun;

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

  ellipse(locationSun.x, locationSun.y, 50, 50);
  ellipse(locationSun2.x, locationSun2.y, 50, 50);
  ellipse(location1.x, location1.y, 20, 20);

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Make only parts of an object move</title>
      <link>https://forum.processing.org/two/discussion/11971/make-only-parts-of-an-object-move</link>
      <pubDate>Tue, 04 Aug 2015 22:48:20 +0000</pubDate>
      <dc:creator>stuffinprocess</dc:creator>
      <guid isPermaLink="false">11971@/two/discussions</guid>
      <description><![CDATA[<p>Hello to everyone, I am new to programming and I thought that what I wanted to do would be easy to realize but I'm having difficulties. 
I would like to draw kind of flowers and having their "seeds" moving up and down, what's wrong in my code?</p>

<pre><code>Flower[] _flowerArr={};

void setup() {
    size(800, 600);
    smooth();
    drawAll();
    background(255);
}

void draw(){
    // background(255);
    for (int i = 0; i &lt; _flowerArr.length; ++i) {
        _flowerArr[i].drawFlower();
        _flowerArr[i].moveFlower();
        // println(frameCount);
    }
}

void drawAll(){
    for (int i = 0; i &lt; 50; ++i) {
    Flower newFlower = new Flower(random(width));
    _flowerArr=(Flower[])append(_flowerArr, newFlower);
    }
}

//// DEFINE CLASS  /////////////////////////////////////////////////////

class Flower {
    float x1,y1,y2,ang,v;

    Flower(float a){
        x1=a;
        y1=0;
            y2=random(100, 500);
        ang=0;
        v=0;
    }

    void drawFlower(){

        line(x1, y1, x1, y2);

        for (y1 =y1; y1 &lt; y2; y1+=random(2, 8)) { // HOW CAN I MAKE THEM MOVING ????
            float x2 = x1+random(-10, 10);
            line(x1, y1, x2, (y1+v));
            ellipse(x2, (y1+v), 10, 10);
        }
    }

    void moveFlower(){
        v=sin(radians(ang))*50;
        // v=map(sin(radians(ang)), -1, 1, -50, +50);
        ang+=5;
    }
}
</code></pre>

<p>Also, if I activate the background in the draw() I get basically an empty canvas, why is that? Thanks for you help.</p>
]]></description>
   </item>
   <item>
      <title>Collision avoidance and hit test - Need help creating a minigame</title>
      <link>https://forum.processing.org/two/discussion/11257/collision-avoidance-and-hit-test-need-help-creating-a-minigame</link>
      <pubDate>Thu, 11 Jun 2015 03:02:46 +0000</pubDate>
      <dc:creator>Pixels</dc:creator>
      <guid isPermaLink="false">11257@/two/discussions</guid>
      <description><![CDATA[<p>Hi everyone!</p>

<p>I'm quite in a rush at the moment but I am trying to create a small game where the player (the mouse pointer) is chased by several enemies.</p>

<p>So far, 5 enemies spawn randomly on the map and start chasing the pointer at a regular speed. However, since they quickly converge towards the same target, the objects tend to overlap until a "single object" is left on the screen. They didn't merge or anything, they simply find themselves in the same position as it's the what the code tells them to do.</p>

<p>Picture below for your reference.</p>

<p><img src="http://i.stack.imgur.com/GvwUG.gif" alt="" /></p>

<p>However, by doing that, it looses the goal of having <strong>several</strong> enemies. Thus my question, how can I make each of the enemies maintain a distance between each other when they come close to one another?</p>

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

<pre><code>Enemy[] enemies = {};

void setup() {
 size(800, 800);
 background(#2c3e50);

 for (int i = 0; i &lt; 5; i++) {
   Enemy newEnemy = new Enemy(i * random(25,150), i * random(25,150));
   enemies = (Enemy[]) append(enemies, newEnemy);
 }
}

void draw() {
 background(#2c3e50);

  for (int i = 0; i &lt; enemies.length; i++) {
   enemies[i].move();
   enemies[i].display();
  }
}

// ///// Enemy class /////

class Enemy {

  float x;
  float y;

  PVector location;
  PVector speed;
  int enemySize = 20;

  Enemy[] otherEnemies = {};

  Enemy(float x, float y) {
    this.x = x;
    this.y = y;
    location = new PVector(random(800), random(800));
    speed = new PVector(2, -2);
  }

  void move() { //Or void update()
    PVector target = new PVector(mouseX, mouseY);
    PVector movement = PVector.sub(target, location);
    speed.add(movement);
    speed.limit(3);
    //Updating the location of the enemy
    location.add(speed);
  }

  void display() {
    fill(255);
    noStroke();
    ellipse(location.x, location.y, enemySize - 16, enemySize - 16);
    noFill();
    stroke(255, 100);
    strokeWeight(2);
    ellipse(location.x, location.y, enemySize - 10, 10);
    noFill();
    stroke(255, 100);
    strokeWeight(1);
    ellipse(location.x, location.y, 20, 20);
    checkBounds();
  }

  //Function to invert the velocity vector values
  //So that the ball stays within the frame
  void checkBounds() {
    if ((location.x &gt; width) || (location.x &lt; 0)) {
      speed.x = speed.x * -1;
    }

    if ((location.y &gt; height) || (location.y &lt; 0)) {
      speed.y = speed.y * -1;
    }
  }
}
</code></pre>

<p>I know there is something I can do with the dist() function and/or arrays, but being new to programming I can't put it down properly.
Any hint or advice on how to proceed from there?</p>

<p>Thanks!</p>

<p>P.S.: Spirit, an iOS game - <a href="http://iappsin.com/arcade-game-spirit-for-iphone-and-ipad.htm" target="_blank" rel="nofollow">http://iappsin.com/arcade-game-spirit-for-iphone-and-ipad.htm</a> - has been a good source of inspiration. Different types of enemies including green ones that follow the player and never overlap with their peers.</p>
]]></description>
   </item>
   <item>
      <title>Understanding *.obj files and the loadShape function</title>
      <link>https://forum.processing.org/two/discussion/11047/understanding-obj-files-and-the-loadshape-function</link>
      <pubDate>Fri, 29 May 2015 03:49:15 +0000</pubDate>
      <dc:creator>subject117</dc:creator>
      <guid isPermaLink="false">11047@/two/discussions</guid>
      <description><![CDATA[<p>Can someone explain to me in what situation the loadShape example code in the documentation:</p>

<p><a href="https://www.processing.org/reference/loadShape_.html" target="_blank" rel="nofollow">https://www.processing.org/reference/loadShape_.html</a></p>

<p>Would work to load an obj file? When I export an obj file from tinkercad I cannot load it. Do I need to use the OBJLoader library? Why does the OBJLoader library need to exist when there is a loadShape function? Or why does the loadShape function documentation discuss obj files when it doesn't work?</p>

<p>I suspect that obj files are more complicated than I think, but can't figure it out. Any help would be appreciated.</p>

<p>Thanks!</p>
]]></description>
   </item>
   <item>
      <title>Please help!! Putting Array into an If Statement?</title>
      <link>https://forum.processing.org/two/discussion/10469/please-help-putting-array-into-an-if-statement</link>
      <pubDate>Thu, 23 Apr 2015 08:59:29 +0000</pubDate>
      <dc:creator>dazz</dc:creator>
      <guid isPermaLink="false">10469@/two/discussions</guid>
      <description><![CDATA[<p>I'm making a simple game where a bunch of objects in an array appear at the same time. Pressing the mouse will remove the array of objects in a class (sample code below). When it hits 0 want it to head into another game State.</p>

<pre><code>
void mousePressed() {
  
    if (gameState == RUN_GAME) {
    objects.remove(0);
  }
}
</code></pre>

<p>When it hits 0 and theres no more objects in the sketch and you mouse press, processing automatically freezes. I've tried something like this (below) but it obviously doesn't work. How could I tell the if statement to change the gameState once the array list of objects hit 0? I hope I'm making sense.</p>

<pre><code>
void runGame(){
  
    if (objects.length == 0) {
    gameState == WINNER);
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Changing Size of Object Slowly</title>
      <link>https://forum.processing.org/two/discussion/10302/changing-size-of-object-slowly</link>
      <pubDate>Tue, 14 Apr 2015 02:33:59 +0000</pubDate>
      <dc:creator>RifleGirl</dc:creator>
      <guid isPermaLink="false">10302@/two/discussions</guid>
      <description><![CDATA[<p>How would you go about changing the size of an object when you click the page. I realize there are many ways to do this but I specifically want the shapes to gradually increase/decrease given an array of numbers.</p>
]]></description>
   </item>
   <item>
      <title>Need some help with dynamic object creation.</title>
      <link>https://forum.processing.org/two/discussion/9559/need-some-help-with-dynamic-object-creation</link>
      <pubDate>Tue, 24 Feb 2015 14:11:10 +0000</pubDate>
      <dc:creator>Ruzzle</dc:creator>
      <guid isPermaLink="false">9559@/two/discussions</guid>
      <description><![CDATA[<p>Hey guys, im not really that far with processing yet so bear with me!</p>

<p>So I have this code:</p>

<pre><code>// create an empty array for 10 MovingCircle objects
MovingCircle[] myCircleArray = new MovingCircle[10];

void setup() {
  size(400, 400);
  smooth();




}

void draw() {
     for(int i=0; i&lt;myCircleArray.length; i++) {
    myCircleArray[i] = new MovingCircle(10,10+i*10,10);  
  }
  background(0);

  // iterate through every moving circle
  for(int i=0; i&lt;myCircleArray.length; i++) {

    myCircleArray[i].update();
    myCircleArray[i].drawCircle();


  }


}

void mousePressed() { // if mouse pressed
println("changed");
myCircleArray = new MovingCircle[4]; //create new circles
}


class MovingCircle {

  float x;
  float y;


  float circleSize;

  MovingCircle(float xpos, float ypos, float csize) {
    x = xpos;
    y = ypos;
    circleSize = csize;
  }

  void update() {
  }


  void drawCircle() {

    fill(255);
    ellipse(x, y, circleSize, circleSize);

  }


}
</code></pre>

<p>Basically what is does is that it's an object based (I used an example and fitted it to my needs) script, it can generate the amount of circles I put in on the beginning at "new MovingCircle[10];"<br />
I already moved this piece of code</p>

<pre><code>for(int i=0; i&lt;myCircleArray.length; i++) {
myCircleArray[i] = new MovingCircle(10,10+i*10,10);
</code></pre>

<p>from setup to draw so it can check it every time.</p>

<p>Im asking for help on the part at void mousePressed(), I figured out how to change the amount of objects but I want it to be one more each time I press the mouse button. Like "++".</p>

<p>I hope you guys understand, im not a native speaker! 
Thanks for the help,
Ruzzle.</p>
]]></description>
   </item>
   <item>
      <title>How to get the width and x values of a custom object?</title>
      <link>https://forum.processing.org/two/discussion/9293/how-to-get-the-width-and-x-values-of-a-custom-object</link>
      <pubDate>Wed, 04 Feb 2015 15:43:31 +0000</pubDate>
      <dc:creator>danielle_nl</dc:creator>
      <guid isPermaLink="false">9293@/two/discussions</guid>
      <description><![CDATA[<p>Hi all,</p>

<p>I've created an object called Cloud that consists of may circles. The Cloud is animated and moves out of the screen at some point. I want to check if it has left the screen completely so I can remove it from my arrayList. But I don't know how to get to the width and x values of the <strong>whole</strong> Cloud object. Is there a way to do this?</p>

<p>Thanks very much in advance for your help, best, Danielle.</p>
]]></description>
   </item>
   <item>
      <title>Using keyPressed() events within objects - is this 'done'?</title>
      <link>https://forum.processing.org/two/discussion/9022/using-keypressed-events-within-objects-is-this-done</link>
      <pubDate>Tue, 13 Jan 2015 15:06:53 +0000</pubDate>
      <dc:creator>sjon</dc:creator>
      <guid isPermaLink="false">9022@/two/discussions</guid>
      <description><![CDATA[<p>I've just been trying to make a basic pong game. I have the paddle working and now I'm trying to create a 'bat' object for this.</p>

<p>Display and that is cool, but  I was thinking that I'd be able to copy paste the <code>keyPressed()</code> and <code>keyReleased()</code> event functions into the objects method area, I'm not sure whether this is possible or not. Do they have to be global? As in, outside all other { } braces?</p>

<p>The bottom bat works as expected, the top one doesn't. The top one is the object, bottom one the one declared in <code>draw()</code>. I'm not sure how to handle this, here's the code :</p>

<pre><code>Bat bat = new Bat();

float directionX = 1;
float x = 100;
boolean lefting, righting;

void setup() {
  size(700, 500);
  background(0);
}

void draw() {
  background(0);

  if (lefting) {
    directionX -= 7.3;
  }
  if (righting) {
    directionX += 7.3;
  }
  x += directionX;

  rect(x, height-50, 100, 40);

  if ((x&gt;width-100) || (x&lt;0)) {
    directionX *=-1;
  }
  directionX = 0;
  bat.display();
  bat.move();
}

void keyPressed() {
  if (key==CODED) {
    if (keyCode==LEFT) {
      lefting=true;
    } else if (keyCode==RIGHT) {
      righting = true;
    }
  }
}

void keyReleased() {
  if (key==CODED) {
    if (keyCode==LEFT) {
      lefting = false;
    } else if (keyCode==RIGHT) {
      righting=false;
    }
  }
}

class Bat {

  float batDirectionX;
  float batX;
  boolean batLefting, batRighting;


  Bat() {
    batDirectionX = 1;
    batX = 100;
  }

  void display() {
    rect(batX, 100, 100, 100);
  }

  void move() {
    if (lefting) {
      batDirectionX -= 7.3;
    }
    if (righting) {
      batDirectionX += 7.3;
    }
    batX += batDirectionX;
  }

  void keyPressed() {
    if (key==CODED) {
      if (keyCode==LEFT) {
        batLefting=true;
      } else if (keyCode==RIGHT) {
        batRighting = true;
      }
    }
  }

  void keyReleased() {
    if (key==CODED) {
      if (keyCode==LEFT) {
        batLefting = false;
      } else if (keyCode==RIGHT) {
        batRighting=false;
      }
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Why does this give me a null pointer exception</title>
      <link>https://forum.processing.org/two/discussion/7369/why-does-this-give-me-a-null-pointer-exception</link>
      <pubDate>Sat, 27 Sep 2014 17:08:22 +0000</pubDate>
      <dc:creator>spamynator_1</dc:creator>
      <guid isPermaLink="false">7369@/two/discussions</guid>
      <description><![CDATA[<p>My code is below, My apologize i do not know how to show it in code format.<br />
why does "s2.sizewidth *= number;" return a null pointer exception. This isnt actually my project its just a sample i made to reconstruct the problem without posting 1100 lines of code</p>

<pre><code>void setup() {
  noLoop();

  sizewidth = 100;
  sizeheight = 200;
  s2 = new Size2(sizewidth, sizeheight);

  number = 3;
  aproduct = new Product(number, s2);
  aproduct.multiply();
}

void draw() {
  println(s2.sizewidth);
}

int sizewidth;
int sizeheight;

class Size {
  int sizewidth;
  int sizeheight;
  Size(int w, int h) {
    w = sizewidth;
    h = sizeheight;
  }
}

Size2 s2;

class Size2 extends Size {
  Size2(int w, int h) {
    super(w, h);
  }
}

int number;

Product aproduct;

class Product {
  int number;
  Size2 s2;

  Product(int n, Size2 si2) {
    n = number;
    si2 = s2;
  }

  void multiply() {
    s2.sizewidth *= number;
    s2.sizeheight *= number;
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Can't seem to fade out individual particle</title>
      <link>https://forum.processing.org/two/discussion/7090/can-t-seem-to-fade-out-individual-particle</link>
      <pubDate>Mon, 08 Sep 2014 20:23:49 +0000</pubDate>
      <dc:creator>Hendeca</dc:creator>
      <guid isPermaLink="false">7090@/two/discussions</guid>
      <description><![CDATA[<p>Hello!</p>

<p>I'm going to try to get the gist of my problem across without having to paste a novel. I'm using 2 classes, one called Emitter and one called Particle. Each Emitter emits Particles, instantiating a new Particle and appending it to a Particle ArrayList every n number of frames. Each draw() cycle, the Emitters iterate through all of the Particles and call their .update() and .display() functions.</p>

<p>Everything was working as expected until I tried to make the individual Particles fade out over time. Here are the display and update methods from the Particle class;</p>

<pre><code>  void display() {
    pushMatrix();
    strokeWeight(strokeWeight);
    translate(position.x, position.y);
    stroke(colorVals[0], colorVals[1], colorVals[2], colorVals[3]);
    point(0, 0);
    popMatrix();
    t++;
  }

  void update() {

    colorVals[3] -= 5;
    position.add(velocity);
    if(friction &gt; 0) {
      position.mult(friction);
    }

  }
</code></pre>

<p>Unfortunately, when I try to use this in conjunction with an Emitter, I can't get individual Particles to fade out after they've been emitted. Instead, the Emitter just emits particles with a lower and lower alpha value until none of the Particles being emitted are visible. Here's the Emitter.display method that loops through the Emitter's  List Array:</p>

<pre><code>void display() {

    if(particles.size() &gt; 0) {

      for(i = 0; i &lt; particles.size() - 1; i++) {

        Particle particle = particles.get(i);

        if(particle.position.x &gt; (width / 2) || particle.position.x &lt; (width / -2)) {
          particles.remove(i);
        }

        if(particle.position.y &gt; (height / 2) || particle.position.y &lt; (height / -2)) {
          particles.remove(i);
        }
        pushMatrix();
        translate(position.x, position.y);
        particle.update();
        particle.oscillate();
        particle.draw();
        popMatrix();
      }

   }

}
</code></pre>

<p>Can anyone help me achieve the desired result of each Particle fading out at its own rate after it is emitted? If more code is needed to track down the problem, I'd be happy to provide more. Just didn't want to paste too much code. Thanks in advance!</p>
]]></description>
   </item>
   <item>
      <title>Can't figure out how to deal with nested object for loops. In over my head.</title>
      <link>https://forum.processing.org/two/discussion/5544/can-t-figure-out-how-to-deal-with-nested-object-for-loops-in-over-my-head</link>
      <pubDate>Mon, 02 Jun 2014 21:16:43 +0000</pubDate>
      <dc:creator>Dieselroar</dc:creator>
      <guid isPermaLink="false">5544@/two/discussions</guid>
      <description><![CDATA[<p>So first of all this is NOT for the most part my own work; it's a cobbled together hack of code from other people (all public, but just want to put it out there for ethics/wtf does it look like this is three different people)?</p>

<p>I've been working with processing for a few months and I've been messing around with progressively more complex 3d systems and I really want to get this working.</p>

<p>My issue is with the behavior of my particles and planets; I want a set of X particles moving around each planet; I've done this before but can't find my old code <em>sheepish shrug</em>. I know I need a nested for loop, calling each planet position and size, then putting that into the constructor for each array of particles, but I'm in over my head and I'm getting nowhere.</p>

<p>I'd be just as happy to be pointed in the right direction as I would to get a line by line solution. Thank you all for your time and your assistance. Loving this stuff more and more every day :)</p>

<p>And again..sorry this is some ugly ugly shit...not offended if everyone's like "nawwwwww"....</p>

<pre><code>  // Star parameters
  int         maxStarSpeed        = 5;
  int         nbMaxStars          = 15000;
  int         depth               = 10;
  Stars[]     stars               = new Stars[nbMaxStars];

  // Planet parameters

  int         maxPlanetSpeed      = 5;
  int         nb2MaxPlanets       = 20;
  int         pdepth              = 10;
  Planets[]   planets             = new Planets[nb2MaxPlanets];

  // Particles parameters

  int         nb3MaxParticles        = 30;   
  Particle [] particles               = new Particle[nb3MaxParticles];

  // Drawing parameters
  int       sizeX = 1920;
  int       sizeY = 1080;
  boolean   clearScreen = true;
  int       tail = 1;
  int       transparency = 255;


  // Rotation variable
  int         rotationMode = 3;
  float       angle = 0;
  float       delta = radians(0.10);

void setup() {

  String strX, strY;
  size(sizeX, sizeY, P3D);
  float fov = PI/3.0;
  float cameraZ = (height/2.0) / tan(fov/2.0);
  perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*10.0);
  colorMode(RGB, 255);
  loop();
  strokeWeight(tail);

for(int nb = 0; nb &lt; nbMaxStars; nb++) {
  stars[nb] = new Stars(random(-2*width, 2*width), random (-2*height, 2*height),
                               -random(depth*255), random(1, maxStarSpeed));                     
  } 
pushMatrix();
for (int nb2 = 0; nb2 &lt; nb2MaxPlanets; nb2++) {

  planets[nb2] = new Planets(random(-2*width, 2*width), random (-2*height, 2*height),
                               -random(depth*255), random(1, maxPlanetSpeed));   

/* need particle location based on each planet location, movement area determined by size of planet                             

  particles[nb3] = new Particle(
*/

}
popMatrix();


}



void draw() {
  if(clearScreen == true) {
    background(0);
  }
  //  to do; include easing! 

  translate(width/2+((mouseX-(width/2))*10)/(width/2),
        height/2+((mouseY-(height/2))*10)/(height/2)
        ,0);
  rotateY(-((mouseX-(width/2))*radians(30)) / (width/2));
  rotateX(((mouseY-(height/2))*radians(30)) / (width/2));
  if(rotationMode == 1) {
    angle += delta;
  }
  if(rotationMode == 2) {
    angle -= delta;
  }
  rotateZ(angle);

  for(int nb = 0; nb &lt; nbMaxStars; nb++) {
    stars[nb].position();
    stars[nb].anim();
  }

  for (int nb2 = 0; nb2 &lt; nb2MaxPlanets; nb2++) {
    planets[nb2].position();
    planets[nb2].anim();
  } 
/*  
  for (int nb3 =0; nb3 &lt; nb3MaxParticles; nb3++) {
    particles[nb3].draw;
  }
*/

}



void keyPressed() {
  if (key == 'x') {
    clearScreen = !clearScreen;
  }

  if (key == 'c') {
    if (transparency&lt;255);
    transparency +=5;
  } 

  if (key == 'v') {
    if(transparency&gt;2)
    transparency -= 5;
  }

  if (key == 'b') {
    background(0);
  }

 if (keyCode == UP) {
   translate(0,(angle += (5*delta)),0);
 }

 if (keyCode == DOWN) {
   translate(0, (angle -= (5*delta)),0);
 }

 if(keyCode == RIGHT) {
   translate((angle -= (5*delta)),0,0);
 }

 if(keyCode == LEFT) {
  translate((angle += (5*delta)),0,0);
 }

} 




void mousePressed() {
  if(mouseButton==LEFT)
    rotationMode = 1;
  if(mouseButton==RIGHT)
    rotationMode = 2;
  if(mouseButton==CENTER)
    rotationMode = 3;
}



class Particle {

  PVector pos;
  float movingArea;
  float moveSize;

  PVector tail[];
  int tailPosIndex;
  int tailLength;

  float psize;

  float inc1, inc2, inc3;
  float inc1Increment = random(0.01, 0.05);
  float inc2Increment = random(0.01, 0.05);
  float inc3Increment = random(0.01, 0.05);

  Particle( PVector tempPos, float tempMoveSize) {
    tempMoveSize = moveSize;
    tempPos = pos = new PVector();
    psize = random(0.1, 2.0);

    tailPosIndex = 0;
    tailLength = int(psize * 50);
    tail = new PVector[tailLength];

    inc1 = random(TWO_PI);   
    inc2 = random(TWO_PI);
    inc3 = random(TWO_PI);

    movingArea = moveSize - tailLength;
  }

  void draw() {
    updatePosition();
    drawTail();
  }

  void updatePosition() {
    inc1 += inc1Increment;
    inc2 += inc2Increment;
    inc3 += inc3Increment;

    pos.x = sin(inc1) * movingArea;
    pos.y = cos(inc2) * movingArea;
    pos.z = sin(inc3) * movingArea;
    tail[tailPosIndex] = new PVector(pos.x, pos.y, pos.z);
  }

  void drawTail() {
    float xp, yp, zp;
    float xOff, yOff, zOff;
    int nullPos = 0;   
    beginShape(QUAD_STRIP);
    for (int i = 0; i &lt; tailLength; i++) {
      int index = (i + tailPosIndex + 1) % tailLength;
      if ( i &lt; tailLength - 1 &amp;&amp; tail[index] != null) {
        float per = (i - nullPos) / float(tailLength - nullPos);
        xp = tail[index].x;
        yp = tail[index].y;
        zp = tail[index].z;
        int nextIndex = (i + tailPosIndex + 2) % tailLength;
        PVector v0 = PVector.sub(tail[index], tail[nextIndex]);
        PVector v1 = v0.cross(new PVector(0, 1, 0));
        v1.normalize();
        PVector v2 = v0.cross(v1);
        v2.normalize();
        v1 = v0.cross(v2);
        v1.normalize();
        xOff = v1.x * psize * per;
        yOff = v1.y * psize * per;
        zOff = v1.z * psize * per;


        fill(200, 255 * per);

        vertex(xp - xOff, yp - yOff, zp - zOff);
        vertex(xp + xOff, yp + yOff, zp + zOff);
      }
      else nullPos++;
    }
    endShape();

    tailPosIndex++;
    tailPosIndex %= tailLength;
  }
}

class Planets {
  float pX, pY, pZ;
  float dZ;
  float pSize= random(20,60);

  Planets(float tempPX, float tempPY, float tempPZ, float tempDZ) {
    pX = tempPX;
    pY = tempPY;
    pZ = tempPZ;
    dZ = tempDZ;
  }

  void position() {
    pushMatrix();
    translate(pX,pY,pZ);
    noStroke();
    fill(255,255-map(pX,0,width,0,128));
    sphere(pSize);


    popMatrix();  
}


  void anim() {
    pZ = pZ + dZ;
    if(pZ &gt;=0)
    pZ = -1058.0;
  }
 }




class Stars {
  float x,y,z;
  float dZ;

  Stars(float coordX, float coordY, float coordZ, float speedZ) {
    x  = coordX;
    y  = coordY;
    z  = coordZ;
    dZ = speedZ;
  }

  void position() {
    stroke(250+z/depth, transparency);
    point(x,y,z);
  }

  void anim() {
    z = z + dZ;
    if(z &gt;= 0)
       z = -1058.0;
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Sliding Puzzle - Arrays or Objects?</title>
      <link>https://forum.processing.org/two/discussion/4978/sliding-puzzle-arrays-or-objects</link>
      <pubDate>Thu, 08 May 2014 08:56:25 +0000</pubDate>
      <dc:creator>HappyCamper</dc:creator>
      <guid isPermaLink="false">4978@/two/discussions</guid>
      <description><![CDATA[<p>Hey im gonna try to create a sliding puzzle. So a game where i have a grid of some sort, and square pieces on top that can be moved around the grid. But only in the up,down,left,right directions. 
Ive already created something similar by using a 2d array which stores the state of every cell in the grid. so if i change the value of a specific entry in the array it reflects on the grid by drawing a rectangle there. however using this method, if i move the rectangle it just jumps from one position to the next, if that makes sense. it doesn't animate from say left to right instead it just jumps left, seeing as im just making the entry at the current position 0 and then putting the entry in a different position in the array.</p>

<p>Would using object be better in this situation or can i work away around it with the arrays?</p>

<p>i hope this makes sense...</p>
]]></description>
   </item>
   <item>
      <title>Problems undrawing objects</title>
      <link>https://forum.processing.org/two/discussion/4965/problems-undrawing-objects</link>
      <pubDate>Wed, 07 May 2014 14:06:22 +0000</pubDate>
      <dc:creator>AceSlowman</dc:creator>
      <guid isPermaLink="false">4965@/two/discussions</guid>
      <description><![CDATA[<p>Looking through the documentation, it looks like noFill(); and noStroke(); are all I should have to call to "undraw" my object. Right now, on click, I am displaying a child node of my tree, but on another click on the parent, I want those children to collapse and "undraw". Could anybody let me know what might be going wrong here? Much appreciated. Running in javascript.</p>

<p>MAIN SKETCH:</p>

<pre><code>//SFD Tree Visualization
//Austin Slominski 2014

Node root;
Node current;
XML data;

void setup(){
    root = new Node();
    data = loadXML("nodes.xml");

    size(750,750);
    smooth(true);
    background(255);

    loadChild(root, data, null);
    current = root;
}

void loadChild(Node node, XML data, Node parent) {
    XML title = data.getChild("title");
    String xmlTitle = title.getContent();
    node.title = xmlTitle;

    XML[] XMLchildren = data.getChildren("child");
    node.children = new Node[XMLchildren.length];

    for(int i=0;i&lt;XMLchildren.length;i++){
      node.children[i] = new Node();
    }

    //DEBUG
        println("Current Node: " + node.title);
      if(parent!=null){
        println("Parent: " + parent.title);
        node.parent = parent;
      }
      if(node.children!=null){
          println("Number of Children:" + XMLchildren.length);
      }
        println("");

    for (int i = 0; i &lt; XMLchildren.length; i++) {
      loadChild(node.children[i], XMLchildren[i], node);
    }
}

void draw(){
    current.display();//starts inactive, null collapsed

    if(current.collapsed == false) {
          for (int i = 0; i &lt; current.children.length; i++) {
        current.children[i].display();
    }
    }

}

void mousePressed(){
    current.clickedActive();

    for (int i = 0; i &lt; current.children.length; i++) {
      current.children[i].clickedActive();
    }
}
</code></pre>

<p>NODE CLASS:</p>

<pre><code>/* <a href="/two/profile/pjs">@pjs</a> font="Junction-light.otf"; */
PFont f;

class Node {

      Node parent;
      Node[] children;
      public String title;

      int x=120;
      int y=140;
      int tWidth;
      int tHeight;
      int titleColor = 0;
      float textSize=70;
      public boolean active = false;
      public boolean collapsed = true;

    Node(){
      f = createFont("Junction-light.otf",100,true);
      textFont(f,textSize);  
    }

    void display(){
      if(parent == null) {
        fill(titleColor);
        text(title,x,y);
        noFill();
      } else {
        if(parent.collapsed == true){
          noFill();
          noStroke();
          text(title,x,y);
        }else{
          fill(titleColor);
          text(title,x,y);
          noFill();
        }
      }


      tHeight = textAscent();
      tWidth  = textWidth(title)+8;
      //rect(x,y-tHeight,tWidth,tHeight);
    }

    void clickedActive(){
      if(mouseX &gt; x &amp;&amp; mouseX &lt; x+tWidth &amp;&amp; mouseY &gt; y-tHeight &amp;&amp; mouseY &lt; y){

         if(active == false){      //if already inactive (default)
           titleColor = 140;       //make gray
           active = true;          //set active
           collapsed = false;
           expandChildren();
         }else if(active == true){ //if already active
           titleColor = 0;         //make black
           active = false;         //set inactive
           collapsed = true;
           collapseChildren();
         }

      }
    }

    void expandChildren(){
       for (int i = 0; i &lt; children.length; i++) {
         //this.children[i].collapsed = false;
         this.children[i].y = y + 150 + 150*i;
         println("expand");
       }
    }

    void collapseChildren(){
       for (int i = 0; i &lt; children.length; i++) {
         //this.children[i].collapsed = true;
         println("collapse");
       }
    }

}
</code></pre>

<p>NODES.XML:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;root&gt;

    &lt;type&gt;&lt;/type&gt;
    &lt;title&gt;ROOT&lt;/title&gt;
    &lt;description&gt;&lt;/description&gt;
    &lt;image&gt;&lt;/image&gt;

    &lt;child&gt;
        &lt;type&gt;&lt;/type&gt;
        &lt;title&gt;Test Child 1&lt;/title&gt;
        &lt;description&gt;&lt;/description&gt;
        &lt;image&gt;&lt;/image&gt;

        &lt;child&gt;
            &lt;type&gt;&lt;/type&gt;
            &lt;title&gt;Test Child 1b&lt;/title&gt;
            &lt;description&gt;&lt;/description&gt;
            &lt;image&gt;&lt;/image&gt;
        &lt;/child&gt;
    &lt;/child&gt;

    &lt;child&gt;
        &lt;type&gt;&lt;/type&gt;
        &lt;title&gt;Test Child 2&lt;/title&gt;
        &lt;description&gt;&lt;/description&gt;
        &lt;image&gt;&lt;/image&gt;

        &lt;child&gt;
            &lt;type&gt;&lt;/type&gt;
            &lt;title&gt;Test Child 2b&lt;/title&gt;
            &lt;description&gt;&lt;/description&gt;
            &lt;image&gt;&lt;/image&gt;
        &lt;/child&gt;
    &lt;/child&gt;

&lt;/root&gt;
</code></pre>
]]></description>
   </item>
   <item>
      <title>Collision Question</title>
      <link>https://forum.processing.org/two/discussion/4399/collision-question</link>
      <pubDate>Tue, 15 Apr 2014 10:41:36 +0000</pubDate>
      <dc:creator>Jeremy_Ortiz88</dc:creator>
      <guid isPermaLink="false">4399@/two/discussions</guid>
      <description><![CDATA[<p>Hey guys, I'm doing a simple billiards game in processing for my class and was wondering how I go about getting the cue ball and the eight ball to collide with each other.</p>

<pre><code>int cueBall;
int eightBall;
int mouseClick=0;
void setup(){
background(0,153,0);
  size(600,300);

}

void draw(){
}


void mousePressed() {
  //background(0,153,0);
  mouseClick++;
  if (mouseClick==1) {
    stroke(0);
    fill(255,255,255);
    ellipseMode(CENTER);
    ellipse(mouseX, mouseY, 50,50);
  }

  if (mouseClick==2) {
    stroke(0);
    fill(0,0,0);
    ellipseMode(CENTER);
    ellipse(mouseX, mouseY, 50,50);
  }

  }
</code></pre>
]]></description>
   </item>
   <item>
      <title>VBasic and P5 code</title>
      <link>https://forum.processing.org/two/discussion/3461/vbasic-and-p5-code</link>
      <pubDate>Wed, 05 Mar 2014 04:22:20 +0000</pubDate>
      <dc:creator>AngusAnsoret</dc:creator>
      <guid isPermaLink="false">3461@/two/discussions</guid>
      <description><![CDATA[<p>I am developing an idea for a game made in P5, is completely based on the idea of another person, only it was not programmed in Processing. 
The problem is that the code is based on Visual Basic and I find it very complex to understand. 
I have no problems with Java and even gave some Processing course, but Visual Basic is too complicated for me. 
Can anyone recommend me a good tutorial to understand the differences between these two languages​​.</p>
]]></description>
   </item>
   <item>
      <title>How to fix error: The method... in the type... is not applicable for the arguments( )</title>
      <link>https://forum.processing.org/two/discussion/3541/how-to-fix-error-the-method-in-the-type-is-not-applicable-for-the-arguments</link>
      <pubDate>Fri, 07 Mar 2014 17:00:44 +0000</pubDate>
      <dc:creator>ian0choi</dc:creator>
      <guid isPermaLink="false">3541@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I am new to Processing and am trying to create objects for the first time.</p>

<p>I keep getting the error message:
The method drawSynapse(float, float) in the type sketch_2D_Circles.Synapse is not applicable for the arguments( )</p>

<p>What am I doing wrong? I couldn't find any reason why...</p>

<pre><code>Synapse syn; //declaring the object syn

void setup(){
  size(500,500);
  background(0);
  smooth();
  ellipseMode(RADIUS);
  syn = new Synapse(10.0, 15.0); // making a new object called syn and passing in the initial variables
}

void draw(){
  syn.drawSynapse();
}

class Synapse { //the class is the blueprint for each Synapse object
  float x;
  float y;
  //float angle;

  Synapse(float tempX, float tempY){ //only add fields here that you want changed for each new synapse. 
    x = tempX;
    y = tempY;
    //angle = tempAngle;
  }

  void drawSynapse(float x, float y){
    ellipse(x, y, 10, 10); 
  }  
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Object declaration: is it possible to do it in a second file?</title>
      <link>https://forum.processing.org/two/discussion/3259/object-declaration-is-it-possible-to-do-it-in-a-second-file</link>
      <pubDate>Mon, 24 Feb 2014 12:42:07 +0000</pubDate>
      <dc:creator>valerio_sperati</dc:creator>
      <guid isPermaLink="false">3259@/two/discussions</guid>
      <description><![CDATA[<p>Hi all, 
I'm a newbie to Processing and just signed to the forum.
My question is the following: is it possible to split the code in different files?
I mean: I wrote a code in a single file, let's say "main.pde".
In this code there is the declaration of the object "Foo".
Now, is it possible to write the declaration of "Foo" in another file,
for example "Foo.pde", and then include this file in "main.pde"?
I googled for this information, but I always find examples using a single pde sketch.
As you can understand, my background is C++  :)
Thanks a lot, 
Valerio</p>
]]></description>
   </item>
   <item>
      <title>Can some one please help me finish the rest of the beginning of this zombie apocalypse simulator</title>
      <link>https://forum.processing.org/two/discussion/1660/can-some-one-please-help-me-finish-the-rest-of-the-beginning-of-this-zombie-apocalypse-simulator</link>
      <pubDate>Thu, 28 Nov 2013 09:41:40 +0000</pubDate>
      <dc:creator>AlexChavez</dc:creator>
      <guid isPermaLink="false">1660@/two/discussions</guid>
      <description><![CDATA[<p>I have started making my class but I don't know where to go from there, any help would would be appreciated. Thank You!</p>

<p>Create a Survivor class; a survivor will have a name, a location on the screen, an "infected"
boolean, an "injured" boolean, and a number of bullets carried (0.5 marks). Add more data if you
think it is warranted.
Create an array of survivors and draw them on the screen (ellipses with names as labels are
sufficient); draw them color coded to indicate whether they are healthy, infected, or injured</p>

<p>You want to know how viable your population of survivors is. Write two functions that take the
array of survivors as arguments:
● one that returns a percentage, the proportion of survivors who are completely healthy vs.
being sick or injured 
● one that returns the number of bullets in the possession of healthy survivors (1 mark)
Write code that calls both of these functions and draws the numbers with appropriate labels
along with the survivors </p>

<pre><code>class Survivor {
  static final int DIAM = 050, RAD = DIAM&gt;&gt;1;

  static final color HEALTHY  = #0000FF;
  static final color INFECTED = #A08000;
  static final color INJURED  = #FF0000;

  static final color LABEL = 0300;

  final String name;

  boolean isInfected = random(1) &lt; .1? true:false;
  boolean isInjured  = random(1) &lt; .3? true:false;

  byte bullets = (byte) random(11);

  short x  = (short) random(RAD, width  - RAD);
  short y  = (short) random(RAD, height - RAD);

  short vx = (short) (random(1, 6)*(random(1) &lt; .5? -1:1));
  short vy = (short) (random(1, 6)*(random(1) &lt; .5? -1:1));

  Survivor(String nick) {
    name = nick;
  }

  void update() {
    if ((x += vx) &gt; width  - RAD | x &lt; RAD)   vx *= -1;
    if ((y += vy) &gt; height - RAD | y &lt; RAD)   vy *= -1;
  }

  void display() {
    fill(whichColor());
    ellipse(x, y, DIAM, DIAM);

    fill(LABEL);
    text(name, x, y);
  }

  private color whichColor() {
    if (isInjured)   return INJURED;
    if (isInfected)  return INFECTED;

    return HEALTHY;
  }
}

void setup() {

  size(500,400);
  background(255);
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How to change the sphere into 3d models. (obj models) (augmented reality)</title>
      <link>https://forum.processing.org/two/discussion/1686/how-to-change-the-sphere-into-3d-models-obj-models-augmented-reality</link>
      <pubDate>Fri, 29 Nov 2013 09:00:07 +0000</pubDate>
      <dc:creator>thekleine</dc:creator>
      <guid isPermaLink="false">1686@/two/discussions</guid>
      <description><![CDATA[<p>Hello,</p>

<p>how do i change the spheres into 3d models?</p>

<p>I have an example that works but when I copy the files and lines it doesn't work in my project.</p>

<p>this is where I am at the moment --&gt;</p>

<p>.
.
.
.
.
.</p>

<pre><code>// we want to use the gsvideo camera support stack

 import codeanticode.gsvideo.*;
// multiple marker tracking
import jp.nyatla.nyar4psg.*;
 // only necessary if you're using the OPENGL renderer (see the size() call in setup())
 import processing.opengl.*;
 // import obj
 import saito.objloader.*;
 import javax.media.opengl.*;





 //activeerd GSCapture
 GSCapture cam;
 //activeerd ar
 NyARMultiBoard nya;
 //importeerd het model
 OBJModel model ;
 float rot = 0.0;
 // importeerd 2 soorten fonts.
 PFont font, font2d;
 //fullscreen shizzle
 import fullscreen.*; 
 FullScreen fs; 







      void setup() {




   // our display is 640x480
   size(640,480,P3D);
     // the same size as the display, which should be 640x480
     // Dit is alleen voor p3d. want hij gaat nu naar fullscreen mode
    fs = new FullScreen(this); 
   // enter fullscreen mode
   fs.enter(); 



   //maakt de fonts aan.
   colorMode(RGB, 100);
    background(20);
   font=createFont("FFScala", 32);
   font2d = createFont("FFScala", 10);


 //start de webcam
   cam=new GSCapture(this,width,height);
   cam.start();


   //PATRONEN:
   // array of pattern file names, these have to be in the data subdir of this sketch
   // patt.hiro =string0 patt.kanji=string1 etc etc. 
   String[] patts = {"patt.hiro", "patt.kanji"};
   // array of corresponding widths in mm
   double[] widths = {80,80};


   // initialise the NyARMultiBoard
   // the camera parameter file is also in the data subdir
   nya=new NyARMultiBoard(this,width,height,"camera_para.dat",patts,widths);
   print(nya.VERSION);



   // marker detection algorithm parameter
   nya.gsThreshold=110;//(0&lt;n&lt;255) default=110
   // a marker has to be detected with a confidence greater than
   // this threshold for it to be considered a true detection
   nya.cfThreshold=0.2;//(0.0&lt;n&lt;1.0) default=0.4
 }







 // draw marker corners and also position in text
 void drawMarkerPos(int[][] pos2d)
 {
   textFont(font,10.0);
   stroke(100,0,0);
   fill(100,0,0);

   // draw ellipses at outside corners of marker
   for(int i=0;i&lt;4;i++){
     ellipse(pos2d[i][0], pos2d[i][1],5,5);
   }

   fill(0,0,0);
   for(int i=0;i&lt;4;i++){
     text("("+pos2d[i][0]+","+pos2d[i][1]+")",pos2d[i][0],pos2d[i][1]);
   }
 }






 // standard processing event handler that's called for display updates
 void draw() {
   // we only do something when the camera input is avail able
   if (cam.available() !=true) {
     return;
   }

   // get an image from the camera
   cam.read();

   // need to put camera image on screen, so temporarily disable depth testing
   hint(DISABLE_DEPTH_TEST);
  // put webcam image on screen
   image(cam,0,0);
   hint(ENABLE_DEPTH_TEST);

   // if *any* markers have been detected this will be true
   if (nya.detect(cam))
   {
     // going to be doing 2D drawing (drawMarkerPos) so temporarily disable depth testing
     hint(DISABLE_DEPTH_TEST);

     // for all detected markers, draw corner points
     for (int i=0; i &lt; nya.markers.length; i++)
     {
       if (nya.markers[i].detected)
       {
         drawMarkerPos(nya.markers[i].pos2d);
       }
     }

     // depth test back on, we're going to draw 3D YEAH!!
     hint(ENABLE_DEPTH_TEST);

     // for all detected markers:
     for (int i=0; i &lt; nya.markers.length; i++)
     {
       if (nya.markers[i].detected)
       {
   // set the model-view transform to that of the marker
   // this will adapt automatically to P3D or OPENGL renderers
   nya.markers[i].beginTransform();

   translate(5,5,0);
   // 5 pixels naar rechts en 5 pixels naar boven. dit bepaald de locatie. 
  // van ALLE afbeeldingen. 

   // ALs het eem hiro marker is(want case is 0), draw a 3D sphere van 40
   switch(i) {
     case 0:
       stroke(255,100,0);
       sphere(40);
     break;  
     // als het een kanji marker is (want case is 1) draw a sphere van 10 
     case 1:
       stroke(5,200,255);
       sphere(10);
     break;  

     default:
       println("maak een case aan");
     break;
   }

   // after drawing marker-relative 3D geometry, we
   // HAVE to end the transform (so now we're back in
   // world space)
   nya.markers[i].endTransform();

  }
}
</code></pre>

<p>}
  else {
    //geen markers gevonden.
  }
}</p>
]]></description>
   </item>
   <item>
      <title>Moving objects to desired direction using keyPressed</title>
      <link>https://forum.processing.org/two/discussion/844/moving-objects-to-desired-direction-using-keypressed</link>
      <pubDate>Thu, 31 Oct 2013 09:01:42 +0000</pubDate>
      <dc:creator>poopjoopx</dc:creator>
      <guid isPermaLink="false">844@/two/discussions</guid>
      <description><![CDATA[<p>Hi, I would like to know how do I move an object by keyPressed and set the object to move to a specific speed and stop at a position and being able to move back slowly to its position.</p>
]]></description>
   </item>
   <item>
      <title>problem with Object array's size</title>
      <link>https://forum.processing.org/two/discussion/687/problem-with-object-array-s-size</link>
      <pubDate>Sat, 26 Oct 2013 12:57:06 +0000</pubDate>
      <dc:creator>alless0</dc:creator>
      <guid isPermaLink="false">687@/two/discussions</guid>
      <description><![CDATA[<p>Hi , I've got a strange problem with my code :</p>

<p>it's one of Nature of Code (Forces section) sketches , I just made an array of these Spheres .
it works fine with 10 as arraylength but when i make it more than 10 the circles get crazy movements !</p>

<p>the Code :</p>

<pre><code>Sphere[] spheres;

void setup(){
 // frameRate(60);
  size(800,720);
//  smooth();
  spheres = new Sphere[10];
  for (int i=0;i&lt; spheres.length ; i++){
  spheres[i] = new Sphere();
  }
}



void draw(){
  background(255);
  PVector gravity = new PVector(0,0.2);
  PVector wind = new PVector(0.3,0);

  for(Sphere s:spheres){
  gravity.mult(s.mass);

  s.applyForce(gravity);

  if(mousePressed){
  s.applyForce(wind);
  }

  s.update();
  s.display();
  s.checkEdges();

  }  
}

class Sphere {
 PVector location ;
 PVector velocity ;
 PVector acceleration ;
 float mass ;
 color c ;
 float topSpeed ;
 float radius ;


Sphere (){
 location = new PVector (random(width), random(height/2));
 velocity = new PVector (0,0);
 acceleration = new PVector (0,0);
 mass = random (.5 , 4);
 c = color (random(255));
 topSpeed = 3 ;
 radius = mass*30 ;
} 

  void update(){
  velocity.add(acceleration);
  location.add(velocity); 
//  velocity.limit(topSpeed);   
  acceleration.mult(0);
  }

  void display(){
   fill(c);
   strokeWeight(mass);
  // ellipse(location.x,location.y,mass*40,mass*40);
      ellipse(location.x,location.y,radius,radius);
  }

  void applyForce(PVector force){
   PVector  f = PVector.div(force,mass);
   acceleration.add(f);
  }


    void checkEdges() {

    if (location.x &gt; width-(radius/2)) {
      location.x = width-(radius/2);
      velocity.x *= -1;
    } else if (location.x &lt; 0+(radius/2)) {
      velocity.x *= -1;
      location.x = 0+(radius/2);
    }
    if (location.y &gt; height -(radius/2)) {
      velocity.y *= -1;
      location.y = height -(radius/2);
    }
  }
</code></pre>

<p>}</p>
]]></description>
   </item>
   <item>
      <title>Question about Objects and Images</title>
      <link>https://forum.processing.org/two/discussion/478/question-about-objects-and-images</link>
      <pubDate>Thu, 17 Oct 2013 22:15:07 +0000</pubDate>
      <dc:creator>profjere</dc:creator>
      <guid isPermaLink="false">478@/two/discussions</guid>
      <description><![CDATA[<p>I've created a class which contains an image:</p>

<p>If I include the loadimage in the display function, it works:</p>

<pre><code>class Hand {
  int x;
  int y;
  PImage thehand;
  Hand() {
   x=100;
   y=100;
  }
  void display(){
 thehand=loadImage("hand.png"); 
   image(thehand,x,y); 
  }
}
</code></pre>

<p>But that's wrong, since the loadimage keeps happening with each cycle through display (as opposed to once when the object is created.)</p>

<p>Putting:
thehand=loadImage("hand.png") in</p>

<pre><code>    Hand() {
           x=100;
           y=100;
           thehand=loadImage("hand.png")
          }
</code></pre>

<p>leads to an error that the image can't be found with the line: "image(thehand,x,y)" highlighted.</p>

<p>What am I missing?</p>
]]></description>
   </item>
   </channel>
</rss>