<?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 magsq() - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=magsq%28%29</link>
      <pubDate>Sun, 08 Aug 2021 19:50:53 +0000</pubDate>
         <description>Tagged with magsq() - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/taggedmagsq%28%29/feed.rss" rel="self" type="application/rss+xml" />
   <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>
   </channel>
</rss>