<?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 sub() - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=sub%28%29</link>
      <pubDate>Sun, 08 Aug 2021 19:50:50 +0000</pubDate>
         <description>Tagged with sub() - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/taggedsub%28%29/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>Function changes the values of variables given as input.</title>
      <link>https://forum.processing.org/two/discussion/28115/function-changes-the-values-of-variables-given-as-input</link>
      <pubDate>Mon, 10 Sep 2018 09:18:35 +0000</pubDate>
      <dc:creator>OrangeFrog</dc:creator>
      <guid isPermaLink="false">28115@/two/discussions</guid>
      <description><![CDATA[<pre><code>PVector[] dots = new PVector[3];

void setup() {
  dots[0] = new PVector(100, 10);
  dots[1] = new PVector(10, 10);
  dots[2] = new PVector(10, 50);
  size(200, 200);
  fill(255);
}

void draw() {

  background(0);
  ellipse(dots[0].x, dots[0].y, 10, 10);
  ellipse(dots[1].x, dots[1].y, 10, 10);
  ellipse(dots[2].x, dots[2].y, 10, 10);
  printArray(dots);
  println(getAcute(dots[0], dots[1], dots[2]));

}

float getAcute(PVector a, PVector b, PVector c) {

  return degrees(PVector.angleBetween(a.sub(b), c.sub(b))); //Problematic line.

}
</code></pre>

<p>When using this basic function of finding angle using 3 points, the values of 'dots[0]' and 'dots[2]' are reduced by 'dots[1]' every time the getAcute function is called. I don't understand why it does this because my understanding is that the function creates 3 temporary variables, "a, b, c", and is restricted to that function. How do I make it so the dots[] values aren't changed at all? I could make new variables and set them to equal dots[] every time, but I feel like there is a simpler solution and my code should be working.</p>
]]></description>
   </item>
   <item>
      <title>drag &amp; drop images in Processing</title>
      <link>https://forum.processing.org/two/discussion/28043/drag-drop-images-in-processing</link>
      <pubDate>Wed, 06 Jun 2018 15:47:35 +0000</pubDate>
      <dc:creator>komats</dc:creator>
      <guid isPermaLink="false">28043@/two/discussions</guid>
      <description><![CDATA[<p>Hi,
I am trying to make a window where it is possible to drag and drop different images and to make them overlap with a mouse click.
Found a perfect example for it in <em>openprocessing</em>: <a href="https://www.openprocessing.org/sketch/540720" target="_blank" rel="nofollow">https://www.openprocessing.org/sketch/540720</a>
but I guess it would only work in p5.js</p>

<p>In processing library there are some examples of dragging the pictures, but they do so without mouse click and it is not possible to drop them. 
So I am wondering, <strong>is there any advanced example of drag &amp; drop img that can run in Processing</strong>, or I better have to download this p5.js?</p>

<p>Cheers,
tija</p>
]]></description>
   </item>
   <item>
      <title>pointing towards mouse snippet</title>
      <link>https://forum.processing.org/two/discussion/27854/pointing-towards-mouse-snippet</link>
      <pubDate>Thu, 26 Apr 2018 08:12:46 +0000</pubDate>
      <dc:creator>appinv</dc:creator>
      <guid isPermaLink="false">27854@/two/discussions</guid>
      <description><![CDATA[<p>i have seen many un-processonic codes for dealing with pointing towards mouse including the use of atan and PVector
here is a natural solution :</p>

<pre><code>def setup():
    size(640, 640)


def draw():
    background(255)
    translate(width/2, height/2)
    location = PVector(200, 200)
    mouse = PVector(mouseX, mouseY)

    mouse.sub(location) # vector from mouse to point
    mouse.normalize()
    mouse.mult(50)
    line(0, 0, mouse.x, mouse.y)
</code></pre>
]]></description>
   </item>
   <item>
      <title>Simple follow behaviour</title>
      <link>https://forum.processing.org/two/discussion/27087/simple-follow-behaviour</link>
      <pubDate>Fri, 23 Mar 2018 11:28:43 +0000</pubDate>
      <dc:creator>jbastow</dc:creator>
      <guid isPermaLink="false">27087@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to implement a simple follow behaviour where each object in the ArrayList follows the next object in the ArrayList. At the moment it seems like all the objects are following each other though? I'm assuming it's a problem with the loop but can't figure it out..</p>

<pre><code>ArrayList&lt;Mover&gt; movers;

void setup() {
  size(640, 360);
  movers = new ArrayList&lt;Mover&gt;();
  for (int i = 0; i &lt; 4; i++) {
    movers.add(new Mover());
  }
}

void draw() {
  background(255);

  for (int i = 0; i &lt; movers.size(); i++) {
    Mover m = movers.get(i);
    float alpha = i*85;

    m.follow(movers);
    m.update();
    m.display(alpha);
    m.boundary();
  }
}

class Mover {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float mass;
  float radius;
  float maxspeed;
  float maxforce;
  float damping;

  Mover() {
    location = new PVector(random(width), random(height));
    velocity = new PVector();
    acceleration = new PVector();
    radius = 20;
    maxspeed = 2;
    maxforce = 0.3;
    damping = 0.98;
  }

  void follow(ArrayList&lt;Mover&gt; movers) {
    for (int i = 0; i &lt; movers.size()-1; i++) {
      Mover other = movers.get(i+1);
      PVector neighbour = PVector.sub(other.location, location);
      neighbour.setMag(maxspeed);
      PVector steer = PVector.sub(neighbour, velocity);
      steer.limit(maxforce);
      applyForce(steer);
    }
  }
  void applyForce(PVector force) {
    force.copy();
    acceleration.add(force);
  }

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

  void display(float alpha) {
    fill(alpha);
    stroke(0);
    strokeWeight(1);
    ellipse(location.x, location.y, radius, radius);
  }

  void boundary() {
    if (location.x &lt; 0) { 
      location.x = width;
    }
    if (location.x &gt; width) { 
      location.x = 0;
    }  
    if (location.y &lt; 0) { 
      location.y = height;
    }
    if (location.y &gt; height) { 
      location.y = 0;
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Interactivity</title>
      <link>https://forum.processing.org/two/discussion/26411/interactivity</link>
      <pubDate>Sat, 17 Feb 2018 21:01:19 +0000</pubDate>
      <dc:creator>byurur</dc:creator>
      <guid isPermaLink="false">26411@/two/discussions</guid>
      <description><![CDATA[<p>Hi everyone</p>

<p>I need help to add interactivity to a work I've done, can you help me with this?</p>

<pre><code>class Particle {

  PVector location;
  PVector velocity;
  PVector acceleration;
  PVector force,friction;
  float mass,r,distance,loc;
  float maxForce, maxSpeed,strength;
  int id,val;
  color c;
  float k = 0.02;
  int age = 0;
  int spawnAge = (int)random(80,120);
  float minr = 0;
  float maxr = 5.0;
  boolean spawned = false;

  Particle(float x, float y,int _id) {
    mass = 0.1;
    id = _id;
    r = 0.1;
    maxSpeed = 1;
    maxForce = 10;
    location = new PVector(constrain(x,r,width-r), constrain(y,r,height-r));
    velocity = new PVector(0, 0);
    acceleration = new PVector(0, 0);
  }

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

  void update() {
    velocity.add(acceleration);
    velocity.limit(maxSpeed);
    location.add(velocity);
    acceleration.mult(0);
    friction();
    borders();
    age+=1;
  }

  void friction(){
     friction = velocity.copy();
     friction.mult(-1);
     friction.normalize();
     friction.mult(k);

     applyForce(friction);
  }

  void display() {

    ellipse(location.x,location.y,r*2,r*2);
  }

  PVector attract(Particle p) {
    force = PVector.sub(p.location,location);
    distance = force.magSq(); 

      distance = constrain(distance, 1.0, 5000.0);
      force.normalize();

      strength = (g) / (distance*4*PI);
      force.mult(strength);n
      return force;


  }

  void setRadius (PImage img){

      loc = (int)location.x + (int)location.y*img.width;
      //c= img.get((int)location.x,(int)location.y);
      //c =  img.pixels[int(loc)];
      val = img.pixels[int(loc)] &amp; 0xFF;
      r=  lerp(r,map(val,0.0,255.0,minr,maxr),0.01);

  }

  void borders() {
      if ( location.x &lt; r || location.x &gt; width-r){
          velocity.x = -velocity.x;
      }
      if ( location.y &lt; r || location.y &gt; height-r){
          velocity.y = -velocity.y;
      }

      if (location.x&gt; width-r) location.x = width-r;
      if (location.x&lt; r) location.x = r;
      if (location.y&gt; height-r) location.y = height-r;
      if (location.y&lt; r) location.y = r;

  }

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>PVector and mouse</title>
      <link>https://forum.processing.org/two/discussion/24926/pvector-and-mouse</link>
      <pubDate>Thu, 09 Nov 2017 11:33:58 +0000</pubDate>
      <dc:creator>Alfio</dc:creator>
      <guid isPermaLink="false">24926@/two/discussions</guid>
      <description><![CDATA[<p>hello, I am a beginner in coding with processing
I'm following Shiffman channel on youtube, great guy!!</p>

<p>I got stuck with the following tutorial
1.4: Vector Math II - The Nature of Code</p>

<p>I wanted to expand the script but didn't succeed
I can't normalize the length of the lines</p>

<p>here is the code
below what I want to obtain
and what I get so far
any idea?</p>

<pre><code>void setup() {
  size(800,600);
}

void draw(){
    background(0);

  for(int x = 0; x &lt; width; x = x+20) {
    for(int y = 0; y &lt; height; y = y+20) {
      stroke(255);
      PVector mouse = new PVector(mouseX,mouseY);
      PVector loc = new PVector(x,y);
      line(x,y-20,mouse.x,mouse.y);
      mouse.sub(loc);
      mouse.normalize();
      mouse.mult(10);
    }
  }
}
</code></pre>

<p><img src="https://forum.processing.org/two/uploads/imageupload/813/G8ONCUB6LYQZ.png" alt="Screen Shot 2017-11-09 at 12.14.55" title="Screen Shot 2017-11-09 at 12.14.55" /></p>

<p><img src="https://forum.processing.org/two/uploads/imageupload/854/CM1JBNAY6ATL.png" alt="Screen Shot 2017-11-09 at 12.14.00" title="Screen Shot 2017-11-09 at 12.14.00" /></p>
]]></description>
   </item>
   <item>
      <title>How to preserve distance between 3d objects in order that they won't collide?!</title>
      <link>https://forum.processing.org/two/discussion/22999/how-to-preserve-distance-between-3d-objects-in-order-that-they-won-t-collide</link>
      <pubDate>Fri, 09 Jun 2017 14:18:08 +0000</pubDate>
      <dc:creator>lolonulu</dc:creator>
      <guid isPermaLink="false">22999@/two/discussions</guid>
      <description><![CDATA[<p>Hi, I was wondering if someone could check this method based on shiffman's nature of code's one. In my sketch some spheres rotate in a 3d space and I try to force them to stay at a certain distance that they won't collide and stick together... Here is just the method below...Thanks a lot in advance ;))</p>

<pre><code>void separate (Sphere[]planets) {
    float desiredDist = radius*6;
    PVector sum = new PVector();
    int count = 0;
    if (planets != null) {
      if (level &gt; 0 ) {
        for (int i = 0; i &lt;planets.length; i++) {
          int j=1;
          for (j+=i; j&lt;planets.length; j++) {
            float d = PVector.dist(planets[i].location, planets[j].location);
            if ((d &gt;0) &amp;&amp; (d&lt;desiredDist)) {
              PVector diff = PVector.sub(planets[i].location, planets[j].location);
              diff.normalize();
              diff.div(d);
              sum.add(diff);
              count++;
            }

            if (count &gt; 0) {
              sum.div((float)count);
            }
            if (sum.mag() &gt; 0) {
              sum.setMag(maxSpeed);
              PVector steer = PVector.sub(sum, velocity);
              steer.limit(maxForce);
              applyForce(steer);
            }
          }
        }
      }
    }
  }
</code></pre>
]]></description>
   </item>
   <item>
      <title>Move to target (PVector confusion)</title>
      <link>https://forum.processing.org/two/discussion/22696/move-to-target-pvector-confusion</link>
      <pubDate>Sun, 21 May 2017 09:22:55 +0000</pubDate>
      <dc:creator>freshfish</dc:creator>
      <guid isPermaLink="false">22696@/two/discussions</guid>
      <description><![CDATA[<p>Hi,
I am trying to wrap my brain around PVectors and using them to move objects. I simply wanted my circles to start in the center and move to a random location on the canvas. I get the movement part but not the target or the subtract functions.</p>

<pre><code>void update() {
target.sub(location);
target.normalize();
target.mult (.1);

velocity.add(acceleration);
velocity.limit(20);
location.add(velocity);
</code></pre>

<p>}</p>

<p>target is a PVector that contains a random x/y
the velocity add and the location add get the circle moving but I cannot get it to move to the target :(
Help thanks</p>
]]></description>
   </item>
   <item>
      <title>can´t find error :-?</title>
      <link>https://forum.processing.org/two/discussion/21805/can-t-find-error</link>
      <pubDate>Tue, 04 Apr 2017 17:31:53 +0000</pubDate>
      <dc:creator>jozze</dc:creator>
      <guid isPermaLink="false">21805@/two/discussions</guid>
      <description><![CDATA[<p>I have an array of point ("stalkers") that are going to pass from state 1 to state 2 once the condition is reached. 
The thing is that the, in theory static, object "ball" gets also affected by this when it shouldnt and changes its position. 
:-??</p>

<pre><code>Ball b; 
ArrayList &lt;Target&gt; stalkers;

boolean fCounter=true;
float hCounter;
int pop;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  size(1200, 900);
  pop= int (random(5, 15));
  hCounter=300;

  b= new Ball();
  stalkers= new ArrayList &lt;Target&gt;();

  for (int i=0; i&lt;pop; i++) {
    Target other= new Target();
    stalkers.add(other);
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void draw() {
  background(11);

  b.showUp();

  if (fCounter) {
    hCounter--;
    for (int i=0; i&lt;pop; i++) {
      Target coorn= stalkers.get(i);
      coorn.crawl();
      coorn.display();//if counter (something) store state 1 in vectors
    }
  } else {//else state 2
    for (int i=0; i&lt;pop; i++) {
      Target tg= stalkers.get(i);    
      PVector feed = b.sniff(tg);
      tg.update(feed); 
      tg.stalk();
    }
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Ball {
  PVector org;

  Ball() {
    org=new PVector(600, 450);
  }

  void showUp() {
    display();
  }

  void display() {
    fill(255, 0, 0);
    noStroke();
    ellipse(org.x, org.y, 20, 20);
  }

  PVector sniff(Target msk) {//new Vector(force) from human to global tStalker
    PVector force=org.sub(msk.loc);   
    float dist=force.mag();
    // println(dist);
    dist=constrain(dist, 0., 1.);
    force.normalize();
    force.mult(1 );
    println(force.x, force.y);
    return force;
  }
}
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Target {
  PVector loc, vel, acc, state1;
  PVector sniff;

  Target() {
    loc= new PVector(random(1200), random(900));
    vel= new PVector(random(-1, 1), random(-1, 1));
    acc= new PVector();
    state1= new PVector(random(-.1, .1), random(-.1, .1));
  }

  void stalk() {
    move();
    display();
    limits();
  }
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  void display() {
    noStroke();
    fill(255, 0, 0);
    rect(loc.x, loc.y, 2, 2);
  }

  void update(PVector force) {
    acc.add(force);
  }

  void move() {
    vel.add(acc);
    vel.limit(2.);
    loc.add(vel);
  }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  void crawl() {
    if (hCounter&gt;=0) {
      loc.add(state1);
      limits();
    } else { 
      fCounter = false;
    }
  }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  void limits() {
    if (loc.x&lt;0 || loc.x &gt;1200) {
      vel.x = -vel.x;
    } else if (loc.y&lt;0 || loc.y &gt;900) {
      vel.y = -vel.y;
    }
  }
}
</code></pre>

<p>:-?</p>
]]></description>
   </item>
   <item>
      <title>Return to an original position</title>
      <link>https://forum.processing.org/two/discussion/21662/return-to-an-original-position</link>
      <pubDate>Tue, 28 Mar 2017 17:40:33 +0000</pubDate>
      <dc:creator>jozze</dc:creator>
      <guid isPermaLink="false">21662@/two/discussions</guid>
      <description><![CDATA[<p>I wonder how I can return the walker to its original position once 
the counter condition is reached, no matter what the new position is.</p>

<pre><code>PVector walker;
PVector start;
PVector seek;
float count;


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

  walker= new PVector (width/2, height-10);
  start= new PVector(0, -1);
  seek=new PVector(random(-1, 1), random( 1));

  count=0;
}

void draw() {
  background(53, 111);

  count++;

  stroke(0, 0, 255);
  rect(width/2, height-10, 10, 10);

  noStroke();
  fill(255, 0, 0);
  ellipse(walker.x, walker.y, 5, 5);

  startt();
  limits();
  println(count);
}

void startt() {
  if (walker.y&gt;=150 &amp;&amp; count&lt;50) {
    walker.add(start);
  } else {
    walker.add(seek);
  }
}
// once the condition is reached (count=1000) ,  walker "walks back" to its original position.


void limits() {
  if (walker.x&gt;width || walker.x &lt;0) {
    seek.x = -seek.x;
  } else if (walker.y&gt;height || walker.y &lt;0) {
    seek.y = -seek.y;
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>angleBetween Pvector member function inaccurate angle that is only between 0 and 180 degrees! Help!</title>
      <link>https://forum.processing.org/two/discussion/19336/anglebetween-pvector-member-function-inaccurate-angle-that-is-only-between-0-and-180-degrees-help</link>
      <pubDate>Sun, 27 Nov 2016 18:59:47 +0000</pubDate>
      <dc:creator>MyName</dc:creator>
      <guid isPermaLink="false">19336@/two/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>In this situation, I am trying to calculate the angle between a stick and a ball. However, first off, the angle is only between 0 and 180 degrees when using a PVector for the points and the angleBetween() function. Secondly, the degrees don't correspond to my expectations, since 180 degrees is more like northwest than straight, and zero degrees is southeast etc.</p>

<p>Calculating a precise angle is important because I am creating this pool game, and I am using the angle between the stick and pool ball to calculate the x and y components of velocity using cos and sin respectively. I've been trying to do a myriad of things but it just doesn't work.</p>

<p>This is a slightly modified version of the "VectorMath" example provided on Porcessing's website. While this isn't my pool program, it uses the exact same processes as this example. So the fixes to this program to get it to work will be directly applicable to my code. 
What can be done to get the angles to correspond properly to conventional direction? (180 degree is west, 90 is south for processing... etc. ), and just in general, how can I get cos and sin in this situation to get the exact results that would be needed in a pool program, when calculating the x component with cos and y component with sin?
Would atan2() offer a better solution?</p>

<pre><code>float angle;
void setup() {
  size(640,360);
}

void draw() {
  background(0);

  // A vector that points to the mouse location
  PVector mouse = new PVector(mouseX,mouseY);
  // A vector that points to the center of the window
  PVector center = new PVector(width/2,height/2);
  // Subtract center from mouse which results in a vector that points from center to mouse
  mouse.sub(center);

  // Normalize the vector
  mouse.normalize();

  // Multiply its length by 150 (Scaling its length)
  mouse.mult(150);

  angle = PVector.angleBetween(center,mouse);
  println(degrees(angle));

  translate(width/2,height/2);
  // Draw the resulting vector
  stroke(255);
  strokeWeight(4);
  line(0,0,mouse.x,mouse.y);

}

Thank you!
</code></pre>
]]></description>
   </item>
   <item>
      <title>How do I make my 'spaceship' shoot projectiles?</title>
      <link>https://forum.processing.org/two/discussion/18867/how-do-i-make-my-spaceship-shoot-projectiles</link>
      <pubDate>Thu, 03 Nov 2016 17:09:18 +0000</pubDate>
      <dc:creator>junkpot</dc:creator>
      <guid isPermaLink="false">18867@/two/discussions</guid>
      <description><![CDATA[<p>I want to make a game about space battles and I need your help. 
Basically, all I need is a part of code, that makes my object shoot projectiles that fly for ~500 pixels when pressing spacebar. Here's the code.</p>

<pre lang="processing">
PVector location = new PVector (400,400);
PVector velocity = new PVector (0,0);
PVector acceleration = new PVector (0,0);
PVector mouse = new PVector (0,0);
float accelerationMax = 0.5;
int speedMax = 5;
int a = 40;
int b = 80;

void setup() {
size (800,800);
noCursor();
}
void draw() {
background (40);
fill(255,0,0);
ellipse(mouseX,mouseY,10,10);
line(mouseX,mouseY-15,mouseX,mouseY+15);
line(mouseX+15,mouseY,mouseX-15,mouseY);
mouse.set(mouseX, mouseY);
acceleration = PVector.sub(mouse,location);
acceleration.limit(accelerationMax);
location.add(velocity);
velocity.limit(speedMax);
velocity.add(acceleration);
if (mousePressed) {
accelerationMax = 0.01;
speedMax = 0;
}
else {
  accelerationMax = 0.5;
  speedMax = 5;
} 
pushMatrix();
rectMode(CENTER);
translate (location.x, location.y);
rotate (PI/2 + velocity.heading());
fill(0,0,0);
beginShape();
vertex(30,10);
vertex(45,35);
vertex(35,30);
vertex(38,40);
vertex(45,45);
vertex(15,45);
vertex(22,40);
vertex(25,30);
vertex(15,35);
endShape();
popMatrix();
}
</pre>
]]></description>
   </item>
   <item>
      <title>Problems with translating a p5js sketch into processing</title>
      <link>https://forum.processing.org/two/discussion/17911/problems-with-translating-a-p5js-sketch-into-processing</link>
      <pubDate>Fri, 19 Aug 2016 20:14:16 +0000</pubDate>
      <dc:creator>pundes</dc:creator>
      <guid isPermaLink="false">17911@/two/discussions</guid>
      <description><![CDATA[<p>hey coders,<br />
i translate a p5js scetch to processing because i want to generate a mov file. the scetch is based on a tutorial from daniel shiffman on kadenze. but i´ve got a problem now with a boolean output. how can i code this piece of p5js code in processing</p>

<pre lang="javascript">
function draw() {
  for( var i = 0; i &lt; particles.length; i++) {
    if(particles[i].isDead()) {
      //code
    }
  }
}

function Particles() {
   this.isDead = function() {
      var distance = p5.Vector.sub(attractor.pos, this.pos);
      var d = distance.mag();
    
      if(d &lt; 5) {
        return true;
      } else {
        return false;
      }
    }
}
</pre>

<p>first i tried it with void, but void hasn´t got an output.
then i tried something like this with boolean but it also doesn´t work.</p>

<pre lang="javascript">
void setup() {
   //code
}

void draw () {
   for (int i = 0; i &lt; particles.length; i++) {
        if(particles[i].isDead()) {
          //code
        }
    }
}

Class Particle {
   Particle() {
        //code
    }

    boolean isDead() {
        PVector distance = PVector.sub(a.location, location);
        float d = distance.mag();
   
        if(d &lt; 5) {
          return true;
        } else {
          return false;
        }
   }
}
</pre>

<p>it will be great if somebody can help me.</p>

<p>regards
mattias</p>
]]></description>
   </item>
   <item>
      <title>NaN return in code when computing normal vector</title>
      <link>https://forum.processing.org/two/discussion/17622/nan-return-in-code-when-computing-normal-vector</link>
      <pubDate>Sat, 23 Jul 2016 02:16:41 +0000</pubDate>
      <dc:creator>nuh</dc:creator>
      <guid isPermaLink="false">17622@/two/discussions</guid>
      <description><![CDATA[<p>Hey im having trouble figuring out why my code returns NaN when i set m=0 or when i give it the angle =0 . Help would be greatly appreciated i've been trying to figure this out for a while now.</p>

<pre><code>    PVector nvector(float angle){
      float sinc = (m/8)*sin((m/2)*angle);
      float cosc= ((pow(m,2))/16)*cos((m/2)*angle); 

      float g =(-n2*pow(abs(1/a),n2))*pow(cos((m/4)*angle),n2-2)
        +(n3*pow(abs(1/b),n3))*pow(sin((m/4)*angle),n3-2) ;

      float dg = (n2*pow(abs(1/a),n2))*((n2-2))*sinc*pow(cos((m/4)*angle),n2-4)
        +(n3*pow(abs(1/b),n3))*((n3-2))*sinc*pow(sin((m/4)*angle),n3-4);

      float f = pow(abs((1/a)*cos((m/4)*angle)),n2)+ pow(abs((1/b)*sin((m/4)*angle)),n3);

      float df = sinc*g ;

      float d2f = dg*sinc+g*cosc ; 

      float r =pow(f,(-1/n1)); // this is taken care of by the shape function
      float dr = (-1/n1)*pow(f,((-1/n1)-1))*df;

      float d2r =  (1/n1)*((1/n1)+1)*pow(f,((-1/n1)-2))*pow(df,2)+(-1/n1)*pow(f,(-1/n1)-1)*d2f;

      PVector v1 = new PVector(cos(angle),sin(angle)); 
      PVector v3 = new PVector(cos(angle),sin(angle));
      PVector v2 = new PVector(-sin(angle),cos(angle));

      v1.mult(d2r);
      v2.mult((2*dr));
      v3.mult(r);

      PVector M = v1.add(v2);
      PVector N = M.sub(v3);

       return N;
    } 
</code></pre>
]]></description>
   </item>
   <item>
      <title>Declaring Objects</title>
      <link>https://forum.processing.org/two/discussion/14650/declaring-objects</link>
      <pubDate>Thu, 28 Jan 2016 17:14:53 +0000</pubDate>
      <dc:creator>chrisgoc</dc:creator>
      <guid isPermaLink="false">14650@/two/discussions</guid>
      <description><![CDATA[<p>Hello,</p>

<p>I'm currently reading <em>The Nature of Code</em> and I noticed that in some of the examples PVector is defined in draw. Take Example 1.4:</p>

<pre><code>void setup() {
    size(640,360);
}

void draw() {
    background(255);

    PVector mouse = new PVector(mouseX,mouseY);
    PVector center = new PVector(width/2,height/2);
    mouse.sub(center);

    //Multiplying a vector! The vector is now half its original size (multiplied by 0.5).
    mouse.mult(0.5);

    translate(width/2,height/2);
    line(0,0,mouse.x,mouse.y);

}
</code></pre>

<p>I've been using Processing for a while but I'm not a Java expert. I was wondering if the compiler does something to save the PVector because it seems like declaring new ones in each draw frame would be really inefficient.</p>

<p>Generally I layout my sketches like this:</p>

<pre><code>MyObject myObject;
PVector myPVector;

void setup() {
    myObject = new myObject();
    myPVector = new PVector();
}

void draw() {
    myObject.update();
    myPVector.update();
}
</code></pre>

<p>In general, is it costly to define objects in draw? I know that I gained a lot of speed in an Android game I made just by replacing ArrayLists with arrays of game objects so I figured it would be.</p>
]]></description>
   </item>
   <item>
      <title>Compare a list of vecs against a angle</title>
      <link>https://forum.processing.org/two/discussion/11104/compare-a-list-of-vecs-against-a-angle</link>
      <pubDate>Mon, 01 Jun 2015 17:44:02 +0000</pubDate>
      <dc:creator>clankill3r</dc:creator>
      <guid isPermaLink="false">11104@/two/discussions</guid>
      <description><![CDATA[<p>I have a line from the center to the mouse. That line has a certain angle.
For the rest I have random points on the screen.
I want to order those points in such a way that they are compared to the angle from the center to the mouse.</p>

<p><img src="http://forum.processing.org/two/uploads/imageupload/099/1NH4F4F6KY3Y.png" alt="Screen Shot 2015-06-01 at 7.40.22 PM" title="Screen Shot 2015-06-01 at 7.40.22 PM" /></p>

<p>So 0 stays 0. 11 becomes 1, 6 becomes 2, 10 becomes 3 etc.
In other words, if the line would move clockwise then they get numbered in that order.</p>

<p>I have no idea how to make a Comparator for that. I hope someone can help.</p>

<p>O yeah, if you want to run you need eclipse or IntelliJ cause Processing will give a scope problem.</p>

<pre><code>import processing.core.PApplet;
import processing.core.PVector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


public class CompareToAngle extends PApplet {

     public static void main(String[] args) {
         PApplet.main("CompareToAngle", args);
     }


    public void settings() {
        size(1024, 1024);

    }

    public void setup() {

    }


    public void draw() {
        background(255);

        PVector a = new PVector(width/2, height/2);
        PVector b = new PVector(mouseX, mouseY);

        line(a.x, a.y, b.x, b.y);

        // compare against this angle!!
        // compare against this angle!!
        // compare against this angle!!
        // compare against this angle!!
        // compare against this angle!!
        float angle = atan2(b.y-a.y, b.x-a.x);

        fill(0);
        text(angle, b.x, b.y);

        ArrayList&lt;PVector&gt; vecs = new ArrayList&lt;PVector&gt;();

        randomSeed(1);
        for (int i = 0; i &lt; 15; i++ ) {
            PVector v = new PVector(random(50, width-50), random(50, height-50));
            vecs.add(v);
        }

        Collections.sort(vecs, new Comparator&lt;PVector&gt;() {

            <a href="/two/profile/Override">@Override</a>
            public int compare(PVector o1, PVector o2) {

                float angleO1 = atan2(b.y-o1.y, b.x-o1.x);
                float angleO2 = atan2(b.y-o2.y, b.x-o2.x);

                if (angleO1 &gt; angleO2) {
                    if (angleO1 &gt; angle) {
                        return 1;
                    }
                    if (angleO1 &lt; angle) {
                        return -1;
                    }
                }
                else if (angleO1 &lt; angleO2) {
                    if (angleO2 &gt; angle) {
                        return 1;
                    }
                    if (angleO2 &lt; angle) {
                        return -1;
                    }

                }

                return 0;
            }
        });

        int c = 0;
        for (PVector v : vecs) {
            ellipse(v.x, v.y, 5, 5);
            text(c++, v.x+20, v.y);
        }





    }

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Trying to set the normal of uneven ground:shading gets messed up when far away</title>
      <link>https://forum.processing.org/two/discussion/6341/trying-to-set-the-normal-of-uneven-ground-shading-gets-messed-up-when-far-away</link>
      <pubDate>Tue, 15 Jul 2014 12:28:29 +0000</pubDate>
      <dc:creator>heyilovepie</dc:creator>
      <guid isPermaLink="false">6341@/two/discussions</guid>
      <description><![CDATA[<p>So I'm trying to set the custom normal for my ground and I think I did something wrong with the formula.
Here is the code for the ground:</p>

<pre><code>PeasyCam cam;
Ground ground;
PImage grassTexture;

void setup(){
  size(displayWidth-100, displayHeight-100, OPENGL);
  grassTexture=loadImage("grass.jpg");
  ground=new Ground(new PVector(0,200,0), 12000, 15, 6000, 50, grassTexture);

  cam = new PeasyCam(this, 100);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(500);
}

void draw(){
  background(0,172,255);
  ambientLight(120, 120, 120);  //regular light that reaches everywhere

  lightFalloff(1, .001, .0001);
  //lightSpecular(5, 5, 5); //gives tiny glow to ground in darkness, might take out when I make ground with more segments
  directionalLight(170, 170, 170, .5, 1, 0);  //creates shadow

  ground.render();
}

class Ground {

  PVector pos;
  float gWidth, gHeight, gLength;
  float squareDim;

  PImage t;
  float[][] pointHeight;
  ArrayList&lt;ArrayList&lt;PVector&gt;&gt; normals;

  PShape ground;

  Ground(PVector pos, float gWidth, float gHeight, float gLength, float squareDim, PImage t) {
    this.pos=pos;
    this.gWidth=gWidth;
    this.gHeight=gHeight;
    this.gLength=gLength;
    this.squareDim=squareDim;
    this.t=t;
    pointHeight=new float[(int)(gWidth/squareDim)][(int)(gLength/squareDim)];
    for (int i=0; i&lt;(int)(gLength/squareDim); i++) {
      for (int j=0; j&lt;(int)(gWidth/squareDim); j++) {
        pointHeight[j][i]=pos.y+random(-gHeight, gHeight);
      }
    }
    ground=createShape(GROUP);
    addHills();
    assignNormals();
    draw();
  }

  void assignNormals(){
    normals=new ArrayList&lt;ArrayList&lt;PVector&gt;&gt;();
    float leftTop, leftMiddle, leftBottom, middleTop, middleMiddle, middleBottom, rightTop, rightMiddle, rightBottom;
    for (int i=0; i&lt;gLength/squareDim; i++) {
      ArrayList&lt;PVector&gt; rowNormals=new ArrayList&lt;PVector&gt;();
      middleMiddle=pointHeight[0][i];
      leftMiddle=middleMiddle+random(-gHeight, gHeight);

      if (i&gt;0) {
        middleTop=pointHeight[0][i-1];
        leftTop=middleTop+random(-gHeight, gHeight);
      } else {
        middleTop=middleMiddle+random(-gHeight, gHeight);
        leftTop=(middleTop+middleMiddle)/2+random(-gHeight, gHeight);
      }
      if (i&lt;(int)gLength/squareDim-1) {
        middleBottom=pointHeight[0][i+1];
        leftBottom=middleBottom+random(-gHeight, gHeight);
      } else {
        middleBottom=middleMiddle+random(-gHeight, gHeight);
        leftBottom=(middleBottom+middleMiddle)/2+random(-gHeight, gHeight);
      } 
      for (int j=0; j&lt;gWidth/squareDim; j++) {
        if (j&gt;gWidth/squareDim-2) {
          println("last in column");
          rightMiddle=middleMiddle+random(-gHeight, gHeight);
          rightTop=(rightMiddle+middleTop)/2+random(-gHeight, gHeight);
          rightBottom=(rightMiddle+middleBottom)/2+random(-gHeight, gHeight);
        } else {
          //rightMiddle=middleMiddle+random(-gHeight, gHeight);
          rightMiddle=pointHeight[j+1][i];
          if (i&gt;0) {
            //rightTop=rightMiddle+random(-gHeight, gHeight);
            rightTop=pointHeight[j+1][i-1];
          } else { 
            rightTop=rightMiddle+random(-gHeight, gHeight);
          }
          if (i&lt;(int)gLength/squareDim-1) {
            //rightBottom=rightMiddle+random(-gHeight, gHeight);
            rightBottom=pointHeight[j+1][i+1];
          } else {
            rightBottom=rightMiddle+random(-gHeight, gHeight);
          }
        }
        float middle=(leftTop+leftMiddle+leftBottom+middleTop+middleBottom+rightTop+rightMiddle+rightBottom)/8;
        float left=(leftTop+leftMiddle+leftBottom+middleTop/2+middleBottom/2)/4;
        float top=(leftTop+middleTop+rightTop+leftMiddle/2+rightMiddle/2)/4;


        //is something wrong in here??????
////////////////////////////////////////////////////////////////////////////////////
       PVector normal=new PVector(-(left-middle), -squareDim/2, -(top-middle));
       normal.normalize();
//////////////////////////////////////////////////////////////////////////////////////


        rowNormals.add(normal);

        leftTop=middleTop;
        leftMiddle=middleMiddle;
        leftBottom=middleBottom;

        middleTop=rightTop;
        middleMiddle=rightMiddle;
        middleBottom=rightBottom;
      }
      normals.add(rowNormals);
    }
  }

  void render() {
    shape(ground);
  }

  void addHills() {
  }

  void Hill(PVector loc) {
    PVector placement=PVector.sub(loc, pos);
    placement.div(squareDim);
  }

  void draw() {
    PShape strip;
    for (int i=0; i&lt;(int)(gLength/squareDim-1); i++) {
      strip=createShape(GROUP);
      ArrayList&lt;PVector&gt; top=new ArrayList&lt;PVector&gt;();
      ArrayList&lt;PVector&gt; bottom=new ArrayList&lt;PVector&gt;();
      top=normals.get(i);
      bottom=normals.get(i+1);
      PShape quad;
      for (int j=0; j&lt;(int)(gWidth/squareDim-1); j++) {
        quad=createShape();
        quad.beginShape(QUAD);
        quad.textureMode(NORMAL);
        quad.texture(t);
        quad.fill(116, 74, 39);
        quad.noStroke();
        //lightVarShape(quad);
        PVector normal1=top.get(j);
        PVector normal2=bottom.get(j);
        PVector normal3=top.get(j+1);
        PVector normal4=bottom.get(j+1);
        float point1=pointHeight[j][i];
        float point2=pointHeight[j][i+1];  
        float point3=pointHeight[j+1][i];
        float point4=pointHeight[j+1][i+1];
        quad.normal(normal1.x, normal1.y, normal1.z);
        quad.vertex(pos.x-gWidth/2+squareDim*j, point1, pos.z-gLength/2+squareDim*i, 0, 0);
        quad.normal(normal3.x, normal3.y, normal3.z);
        quad.vertex(pos.x-gWidth/2+squareDim*(j+1), point3, pos.z-gLength/2+squareDim*i, 1, 0);
        quad.normal(normal4.x, normal4.y, normal4.z);
        quad.vertex(pos.x-gWidth/2+squareDim*(j+1), point4, pos.z-gLength/2+squareDim*(i+1), 1, 1);
        quad.normal(normal2.x, normal2.y, normal2.z);
        quad.vertex(pos.x-gWidth/2+squareDim*j, point2, pos.z-gLength/2+squareDim*(i+1), 0, 1);

        quad.endShape();
        strip.addChild(quad);
      }
      ground.addChild(strip);
    }
  }
}
</code></pre>

<p><img src="http://forum.processing.org/two/uploads/imageupload/767/C36E55OK04BZ.jpg" alt="groundShading" title="groundShading" />
Does anyone know how to fix this? Is it the normal that is facing into the ground?</p>
]]></description>
   </item>
   </channel>
</rss>