<?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 normalize() - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=normalize%28%29</link>
      <pubDate>Sun, 08 Aug 2021 19:58:59 +0000</pubDate>
         <description>Tagged with normalize() - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/taggednormalize%28%29/feed.rss" rel="self" type="application/rss+xml" />
   <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>How to apply a repulsion force to a particle system ?</title>
      <link>https://forum.processing.org/two/discussion/26408/how-to-apply-a-repulsion-force-to-a-particle-system</link>
      <pubDate>Sat, 17 Feb 2018 19:34:05 +0000</pubDate>
      <dc:creator>solub</dc:creator>
      <guid isPermaLink="false">26408@/two/discussions</guid>
      <description><![CDATA[<p>Hi !</p>

<p>I've recently found <a rel="nofollow" href="https://medium.com/@zachlieberman/daily-sketches-2016-28586d8f008e">this article</a> from Zachary Lieberman where he provides some insights on his work. I'm particularly interested in his "recipe" to make a blob moving "organically" (almost like a fluid).</p>

<p><img src="https://cdn-images-1.medium.com/max/800/1*gapWpxJSlBSFiXVd-2RinQ.gif" alt="" /></p>

<p>This is a 2-step process:</p>

<blockquote class="Quote">
  <ul>
  <li>connect particles with springs (and connect the last particle to the first particle, making a loop)</li>
  <li>give the particles a repulsion force from each other that is proportional to distance (stronger force when closer) and has a maximum radius for interaction (i.e., if particles are beyond this distance, they don’t have an impact on each other)</li>
  </ul>
</blockquote>

<p>I made both a particle and a spring class but got lost when trying to implement the repulsion force. What should I change (add ?) in the following snippet (a spring class using basic Hooke's law) to "give the particles a repulsion force from each other that is proportional to distance" ?</p>

<p><strong>spring class</strong></p>

<pre><code>class Spring(object):

def __init__(self, x, y, l):
    self.startpoint = PVector(x, y)
    self.constant = .2
    self.lngth = l
    self.g = 10
    self.mass = 4

def connect(self, p):    # p refers to the particle_class
    force = PVector.sub(p.location, self.startpoint)
    distance = force.mag()
    stretch = distance - self.lngth

    force.normalize()
    force *= -1 * self.constant * stretch   # Hooke's law
    force.limit(.4) 
    p.applyForce(force) 


def displayLine(self, p):  
    stroke(255)
    strokeWeight(1)
    line(p.location.x, p.location.y, self.startpoint.x, self.startpoint.y) 
</code></pre>

<p>On a side note, I'm also curious to know how to display particles following a splatter shape (GIF above).</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>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>rotating arrow depending on mouseX and mouseY</title>
      <link>https://forum.processing.org/two/discussion/20357/rotating-arrow-depending-on-mousex-and-mousey</link>
      <pubDate>Thu, 19 Jan 2017 16:30:08 +0000</pubDate>
      <dc:creator>mrfsrf</dc:creator>
      <guid isPermaLink="false">20357@/two/discussions</guid>
      <description><![CDATA[<p>Hi all,
neeedd a little help with code.
I want to rotate arrows toward the mouse postion.
Can anyone hlp pls</p>

<p>`
    PVector mouse, center;
    int scalar;</p>

<pre><code>void setup(){
  size(500,500);
  pixelDensity(2);

  background(255);
  scalar=20;
  color(255,0,0);
  strokeWeight(0.5);

}


void draw(){
    background(255);
    mouse = new PVector(mouseX,mouseY);
    center = new PVector(width/2,height/2);
    mouse.sub(center);
    mouse.normalize();
    mouse.mult(15);
    for(int i=0; i&lt;width; i+=scalar-5){
    for(int j=0; j&lt;height; j+=scalar+10){
    pushMatrix();
    translate(i,j);
  line(0, 0, mouse.x, mouse.y);
  translate(mouse.x,mouse.y);
  //float a = PVector.angleBetween(center, mouse);
   float angle = atan2(mouseY-100, mouseX-100);

  rotate(angle);
  line(0, 0, -4, -4);
  line(0, 0, +4, -4);
  popMatrix();
    }
  }

}`
</code></pre>

<p><img src="https://forum.processing.org/two/uploads/imageupload/501/WVDIQGZG0Z3Y.png" alt="Screen Shot 2017-01-19 at 17.28.58" title="Screen Shot 2017-01-19 at 17.28.58" /></p>
]]></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>Can a calculated vector be included in an object's constructor?</title>
      <link>https://forum.processing.org/two/discussion/17611/can-a-calculated-vector-be-included-in-an-object-s-constructor</link>
      <pubDate>Thu, 21 Jul 2016 18:17:37 +0000</pubDate>
      <dc:creator>brooklev</dc:creator>
      <guid isPermaLink="false">17611@/two/discussions</guid>
      <description><![CDATA[<p>Hi Sages,</p>

<p>I am trying to make a game with an ellipse (= antiballistic missile) which takes off from 1 of 3 spots and flies towards a mouse-clicked target leaving a trail and then disappears, along with the trail. I am creating a class called Amiss and was going to construct it as follows:</p>

<p>Amiss(color fillTemp, color strokeTemp, int widthTemp, int heightTemp, int speedTemp, int startXtemp, int startYtemp, int targXtemp, int targYtemp) {</p>

<p>I then figured I would use a move() method to animate the missile toward its target, something like this:</p>

<pre><code>// "vP" stands for the vector path the missile will follow.
vP = new PVector(targX - startX, targY - startY);
vP.normalize();

xPos = xPos + speed * vP.x;
yPos = yPos + speed * vP.y;
ellipse(xPos, yPos, amWidth, amHeight);
</code></pre>

<p>It strikes me that having the program calculate the animating vector each time the missile moves is a waste of processing and that each missile should calculate its own normalized vP once when it is created and then store that vector amongst its parameters for quick, repeated use whenever move() is called.</p>

<p>Is this a good impulse?</p>

<p>Can you include an empty parameter within a constructor and <em>then</em> populate it with a value calculated from its other parameters?</p>

<p>If so, how do you do that?</p>

<p>While I'm at it, am I using method/parameter correctly for Processing?</p>

<p>Thanks for any help!</p>
]]></description>
   </item>
   <item>
      <title>nature of code exercise 1.4</title>
      <link>https://forum.processing.org/two/discussion/16907/nature-of-code-exercise-1-4</link>
      <pubDate>Mon, 30 May 2016 21:42:54 +0000</pubDate>
      <dc:creator>cadavara</dc:creator>
      <guid isPermaLink="false">16907@/two/discussions</guid>
      <description><![CDATA[<p>I was following this book example and they gave this little exercise and I cant figure out what I’m supposed to do and the book never gives the solution.</p>

<p>how do I calculate the limit of a vector? here is the exercise.</p>

<pre><code>void limit(float max) {
  if (_______ &gt; _______) {
    _________();
    ____(max);
  }
}
</code></pre>

<p>i am trying to accomplish something like this</p>

<p>velocity.add(acceleration);<br />
velocity.limit(topspeed);</p>

<p>to get some background here is the link to said exercise.
<a href="http://natureofcode.com/book/chapter-1-vectors/" target="_blank" rel="nofollow">http://natureofcode.com/book/chapter-1-vectors/</a></p>

<p>ANSWER EDIT:</p>

<pre><code>void limitt(float max){
    if(max &gt; 10){
      velocity.normalize();
      velocity.mult(max);
    }
  }
</code></pre>
]]></description>
   </item>
   <item>
      <title>Programming with Vectors (Objects moving towards each other)</title>
      <link>https://forum.processing.org/two/discussion/11249/programming-with-vectors-objects-moving-towards-each-other</link>
      <pubDate>Wed, 10 Jun 2015 13:14:02 +0000</pubDate>
      <dc:creator>Bakafly</dc:creator>
      <guid isPermaLink="false">11249@/two/discussions</guid>
      <description><![CDATA[<p>I want to simulate a river stream eventually. That means I want to have a board with a vector in each field. 
 For now I'm trying to make the "red dots" go towards random vectors. Well, I know how to make the dots follow the mouse but not the vectors. I'd also like to visualize the vectors as black circles (for now, so i can check their position visually).
I also used to have a vector class, where i already visualized the vectors as i wished to. Though it was an Array List and absolute -&gt; not ideal for my goal. So i cut it out for now.</p>

<p>Thank you in advance :)</p>

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

<pre><code>ArrayList mikroplastikParticles; // set of Mikroplastik Particles
int numOfMikroplastikPerField = 1;
int boardSize = 7; // board to be filled with vectors
int board [][] = new int [boardSize][boardSize]; 
PVector vectorSet [][] = new PVector [boardSize][boardSize];
int cellWidth; // Size of the fields
final int mikroplastikSize = 10;
final int VectorSize = 10;
int randomVectorPosition;
float mikroplastikSpeed = 1.;

void setup() {
  // basic Setup
  size(700, 700);
  smooth();
  rectMode(CORNER);
  ellipseMode(CENTER);
  cellWidth = width/boardSize; // width of the cell dependens on the size of the board 
  mikroplastikParticles = new ArrayList();

  // visualization of the board (understanding purpose only)
  fill(255, 255, 255);
  stroke(0);
  for (int r = 0; r&lt;boardSize; r++) {
    for (int c = 0; c&lt;boardSize; c++) {  
      rect(r * cellWidth, c * cellWidth, cellWidth, cellWidth);
    }
  }

  // now we start to initialize the particles
  for (int r = 0; r&lt;boardSize; r++) // we want to fill every single field
    for (int c = 0; c&lt;boardSize; c++) 
      for (int anz = 0; anz &lt; numOfMikroplastikPerField; anz++) // we can choose how many particles per field we want with "numOfMikroplastikPerField"
        mikroplastikParticles.add(new Mikroplastik(r*cellWidth+random(cellWidth), c*cellWidth+ random(cellWidth))); // mikroplastik per field gets added

  // setting up the vectors
  for (int x = 0; x&lt;boardSize; x++) {
    for (int y = 0; y&lt;boardSize; y++) {
      randomVectorPosition = (int)random(2);
      if (randomVectorPosition == 0)
      vectorSet[x][y] = new PVector(0, random(cellWidth));
      else
      vectorSet[x][y] = new PVector(random(cellWidth), 0);
    }
  }
}

void draw() {
  background(255);
  for (int i=0; i &lt; boardSize*boardSize*numOfMikroplastikPerField; i++) { 
    Mikroplastik myMikroplastik = (Mikroplastik)mikroplastikParticles.get(i); // getting all vectors from the array
    myMikroplastik.display(); // assigning a "to do" for the vectors
  }
}

class Mikroplastik  { // the actual mikroplastik class

  PVector mikroplastik = new PVector(0, 0); // the mikroplastik is a vector object
  PVector sum = new PVector(0, 0);

  Mikroplastik(float tempX, float tempY) {
    mikroplastik.set(tempX, tempY);
  }

  void display() 
{ // how the mikroplastik looks like 
    fill(#FF0000);
    noStroke();
    ellipse(mikroplastik.x, mikroplastik.y, mikroplastikSize, mikroplastikSize);

    // PVector mouse = Vector.vector(tempX, tempY);
    PVector mouse = new PVector(mouseX, mouseY);
    mouse.sub(mikroplastik);
    mouse.mult(0.05);
    sum.add(mouse);
    sum.normalize();
    sum.mult(mikroplastikSpeed);
    mikroplastik.add(sum);

  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>why doesn't my sketch run in javascript mode?</title>
      <link>https://forum.processing.org/two/discussion/7294/why-doesn-t-my-sketch-run-in-javascript-mode</link>
      <pubDate>Mon, 22 Sep 2014 23:41:09 +0000</pubDate>
      <dc:creator>scott_me</dc:creator>
      <guid isPermaLink="false">7294@/two/discussions</guid>
      <description><![CDATA[<p>the following fails to run when switched to javascript.  i've tried reading the documentation for javascript but it brings no clarity.</p>

<pre><code>int num=100;
int diameter=10;
int limit=20;
float step=0.01;


PVector[] positions=new PVector[num];
float [] lookup=new float[num];

void setup() {
  size(1000, 1000);
  colorMode(HSB, 360, 100, 100);
  background(0);

  for (int i=0; i&lt;num; i++) {
    float x=random(0, width);
    float y=random(0, height);
    positions[i]= new PVector();
    positions[i].set(x, y);
  }
  makeTable(lookup);
}

void draw() {
  background(0);

  for (int m=0; m&lt;num; m++) {

    float ex=positions[m].x;
    float wy=positions[m].y;
    float h=m*360/num;
    noStroke();
    fill(h, 100, 100);
    ellipse(ex, wy, diameter, diameter);

  }

  move(positions, lookup);

}

void move(PVector[] places, float[]table) {
  for (int q=0; q&lt;places.length; q++) {

    int next=q+1;
    if (next==places.length) {
      next=0;
    }

    PVector v=new PVector();
    v=PVector.sub(places[next], places[q]);

    float adjust=(v.mag()-limit)/v.mag();

    v.mult(adjust);
    v.mult(step);

    PVector v2=new PVector();
    v.normalize(v2);

    float multiplier=table[q];

    PVector v3=new PVector();
    v3.x=v2.y*multiplier;
    v3.y=-v2.x*multiplier;
    v.add(v3);

    places[q].add(v);
    places[q].x=constrain(places[q].x, 0.01*width, 0.99*width);
    places[q].y=constrain(places[q].y, 0.01*height, 0.99*height);
  }
}

void makeTable(float[] calculations) {

  for (int r=0; r&lt;calculations.length; r++) {
    float angle=TWO_PI*float(r)/num;
    float a=sin(angle);

    calculations[r]=0.1*a;
  }
}
</code></pre>
]]></description>
   </item>
   </channel>
</rss>