<?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 createvector() - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=createvector%28%29</link>
      <pubDate>Sun, 08 Aug 2021 20:07:10 +0000</pubDate>
         <description>Tagged with createvector() - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/taggedcreatevector%28%29/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>Help with p5.js solar system sketch</title>
      <link>https://forum.processing.org/two/discussion/27973/help-with-p5-js-solar-system-sketch</link>
      <pubDate>Mon, 14 May 2018 22:05:58 +0000</pubDate>
      <dc:creator>Mojo</dc:creator>
      <guid isPermaLink="false">27973@/two/discussions</guid>
      <description><![CDATA[<p>Hey everyone,
I am doing this sketch which is following up dan's shiffman tutorial in processing, however I am doing it in p5.js
for some reason I can's seem to do the final step and I think it has to do with the vectors.
If anyone can help me, I would really appreciate it.
Here is the code in processing:</p>

<pre><code>import peasy.*;
Planet sun;

PeasyCam cam;

void setup() {
  size(600, 600, P3D);
  cam = new PeasyCam(this, 500);
  sun = new Planet(50, 0, 0);
  sun.spawnMoons(4, 1);
}

void draw() {
  background(0);
  lights();
  sun.show();
  sun.orbit();
}
</code></pre>

<p>// planet class</p>

<pre><code>class Planet {
      float radius;
      float distance;
      Planet[] planets;
      float angle;
      float orbitspeed;
      PVector v;

      PShape globe;

      Planet(float r, float d, float o) {

    v = PVector.random3D();

    radius = r;
    distance = d;
    v.mult(distance);
    angle = random(TWO_PI);
    orbitspeed = o;
  }

  void orbit() {
    angle = angle + orbitspeed;
    if (planets != null) {
      for (int i = 0; i &lt; planets.length; i++) {
        planets[i].orbit();
      }
    }
  }

  void spawnMoons(int total, int level) {
    planets = new Planet[total];
    for (int i = 0; i &lt; planets.length; i++) {
      float r = radius/(level*2);
      float d = random((radius + r), (radius+r)*2);
      float o = random(-0.1, 0.1);
      planets[i] = new Planet(r, d, o);
      if (level &lt; 2) {
        int num = int(random(0, 3));
        planets[i].spawnMoons(num, level+1);
      }
    }
  }

  void show() {
    pushMatrix();
    noStroke();
    PVector v2 = new PVector(1, 0, 1);
    PVector p = v.cross(v2);
    rotate(angle, p.x, p.y, p.z);
    stroke(255);
    //line(0, 0, 0, v.x, v.y, v.z);
    //line(0, 0, 0, p.x, p.y, p.z);

    translate(v.x, v.y, v.z);
    noStroke();
    fill(255);
    sphere(radius);
    //ellipse(0, 0, radius*2, radius*2);
    if (planets != null) {
      for (int i = 0; i &lt; planets.length; i++) {
        planets[i].show();
      }
    }
    popMatrix();
  }
}
</code></pre>

<p>And this is the p5.js code</p>

<pre><code>var easycam;

function setup() {
    createCanvas(windowWidth,windowHeight,WEBGL);
    easycam = createEasyCam();
    sun = new Planet(20, 0, 0, random(TWO_PI));
    sun.spawnMoons(2, 1);
}

function draw() {
    background(0);
    cursor(HAND);

    sun.show();
    sun.orbit();
}
</code></pre>

<p>//planet class</p>

<pre><code>        class Planet {
            constructor(radius, distance, orbitspeed, angle,mass) {
            this.radius = radius;
            this.distance = distance;
            this.orbitspeed = orbitspeed;
            this.angle = angle;
            this.planets = [];
            this.mass=mass;

            // this should be correct
            // this is the vector coming out of the planet itself.

            this.v = createVector();
            this.v = p5.Vector.random3D();
            this.v. mult(this.distance);
            // park this for now.
            // this.v = createVector();
            // this.v = p5.Vector.random3D();
            // this.v.mult(this.distance);
            // var v = new p5.Vector.random3D();
            // v. mult(this.distance);


        }


    orbit() {
        this.angle += this.orbitspeed/this.mass;
        for (let i in this.planets) {
            this.planets[i].orbit();
        }
    }


    spawnMoons(total, level) {
        for (let i = 0; i &lt; total; i++) {
            let r = this.radius/(level*2);
            let d = random(50, 150);
            let o = random(-0.1, 0.1);
            let a = random(TWO_PI);
            this.planets.push(new Planet(r, d/level, o, a));
            if (level &lt; 1) {
                let num = Math.floor(random(0, 4));
                this.planets[i].spawnMoons(num, level+1);
            }
        }
    }


    show() {
      push();
      noStroke(0)
      // fill(255);
      // let v2 = createVector(1,0,1);
      // let p = createVector(v.cross);
      // this.v2 = createVector(0, 0, 1);
      // this. p = this.v.cross(this.v2);
      // rotateY(this.angel);
      // translate(v.x,v.y,v.z);
      this.v2= createVector(1,0,1);
      this.p= this.v.cross(this.v2);
      rotate(this.angel,this.p.x,this.p.y,this.p.z);
      translate(this.v.x,this.v.y,this.v.z);
      //lights
          ambientLight(5,100);
          directionalLight(255, 255, 255, 0.2, 0.2, 0.7);
          specularMaterial(255);
          sphere(this.radius);

        for (let i in this.planets) {
            this.planets[i].show();
        }
        pop();
    }

}
</code></pre>

<p>I would really really appreciate some help, its quite difficult to find references on vectors in p5.js.</p>

<p>Thanks loads in advance</p>
]]></description>
   </item>
   <item>
      <title>P5 back and forth animation</title>
      <link>https://forum.processing.org/two/discussion/25782/p5-back-and-forth-animation</link>
      <pubDate>Tue, 02 Jan 2018 06:07:48 +0000</pubDate>
      <dc:creator>Amorris</dc:creator>
      <guid isPermaLink="false">25782@/two/discussions</guid>
      <description><![CDATA[<p>I have this sketch:</p>

<pre><code>let h = 0;
let k = 0;
let step = 0.05;
let r = 200;
let points = [];
let i = 0;
let forward = true;

function setup() {
  createCanvas(windowWidth, windowHeight);
}

function draw() {
  background(81);
  translate(width / 2, height / 2);
  noFill();
  strokeWeight(1);
  stroke(255);

  beginShape();
  for (let theta = TWO_PI; theta &gt; PI; theta -= step) {
    let x = h + r * Math.cos(theta);
    let y = k - r * Math.sin(theta);
    vertex(x, y);
    points.push(createVector(x, y));
  }
  endShape();

  strokeWeight(10);
  stroke(0);
  point(points[i].x, points[i].y);
}
</code></pre>

<p>That shows a dot moving from one side of an arc to another. When it gets to the end, however it teleports back to the start. I want to make it swing back and forth on the arc. I have tried it using the length of the points array, but that seems to be massively overflowed and it contains duplicate points. Is there a way to do this, or a better way to do what I have done?</p>
]]></description>
   </item>
   <item>
      <title>Limit MouseX &amp; MouseY Movement</title>
      <link>https://forum.processing.org/two/discussion/25486/limit-mousex-mousey-movement</link>
      <pubDate>Sun, 10 Dec 2017 04:33:13 +0000</pubDate>
      <dc:creator>jspanier925</dc:creator>
      <guid isPermaLink="false">25486@/two/discussions</guid>
      <description><![CDATA[<p>I was wondering what the best way to limit mouse movement, for example if I put mouseX and mouseY on these pupils</p>

<pre><code>ellipse(280,165,28,28); //Left Eye
ellipse(310,165,28,28); //Right Eye
fill(0);
ellipse(278,162,3,3);   //Left Pupil
ellipse(315,167,3,3);   //Right Pupil
</code></pre>

<p>how would I limit movement to within the eyes?</p>
]]></description>
   </item>
   <item>
      <title>Transforming PVectors into p5.vector (or createVector?)</title>
      <link>https://forum.processing.org/two/discussion/24074/transforming-pvectors-into-p5-vector-or-createvector</link>
      <pubDate>Sat, 09 Sep 2017 06:17:16 +0000</pubDate>
      <dc:creator>paul_young1984</dc:creator>
      <guid isPermaLink="false">24074@/two/discussions</guid>
      <description><![CDATA[<p>Hi Folks,</p>

<p>I've been reading through every 'convert Processing into P5' document I can get a hold of, and I still can't figure out how to translate PVector's (processing) into p5.vector syntax.</p>

<p>Beneath is code in Processing (working). Beneath that is my code for p5 (not working) I'm pretty sure it's my Vector syntax that is out of wack. Can someone please provide direction? Saying that, there might be other things wrong, and I'm open to suggestions.</p>

<p>PROCESSING:</p>

<pre><code>PImage img;
ArrayList pts;


void setup() {
  img = loadImage("ASKEW A.jpg");

    size(500, 500, P3D);

  pts = new ArrayList();
  findPoints();
}

void draw () {
  background(255);


for (int i = pts.size()-1; i &gt;= 0; i --){
      PVector pt = (PVector)pts.get(i);

      fill(pt.z);

      noStroke();

      stroke(0, 30);
      for (int j = pts.size()-1; j &gt;= 0; j --){
      if(i&gt;1){  
        PVector pt2 = (PVector)pts.get(j);
        if (pt.dist(pt2) &lt; 20){

          int r = 10;
          float x = r * tan(radians(i*(360.0/ (mouseX * tan(3)) )));
          float y = r * sin(radians(i*(360.0/ (mouseY*  tan(3)) )));

          int r1 = 2;
          float x1 = r1 * tan(radians(i*(360.0 / (mouseX* tan(3)))));
          float y1 = r1 * sin(radians(i*(360.0 / (mouseY* tan(3)))));


          stroke(0, 30);
          line(pt.x + x, pt.y + y, 
          pt2.x + x1, pt2.y + y1);
        }
      }
    }
  }
}

void findPoints(){

  img.loadPixels();
  for (int i = img.width-1; i &gt;= 0; i -= 3){
    for (int j = img.height; j &gt;=0; j -= 3){

      color c = img.get(i, j);
      if (brightness(c) &lt; 255){
        pts.add(new PVector(i, j, brightness(c)));
      }
    }
  }
}
</code></pre>

<p>p5 :</p>

<pre><code>var img;
var pts = [];
var i, j;


function setup() {

  createCanvas(500, 500);

    img = loadImage("ASKEW A.jpg");
    findPoints();
    }


function draw () {
  background(255);

for (i = pts.length()-1; i &gt;= 0; i --){
      var pt = new p5.Vector.pts.get(i);
      fill(pt.z);

      noStroke();

      stroke(0, 30);
      for (j = pts.length()-1; j &gt;= 0; j --){
      if(i&gt;1){  
        var pt2 = new p5.Vector.pts.get(j);
        if (pt.dist(pt2) &lt; 20){

          var r = 20;
          var x = r * tan(radians(i*(360.0/ (mouseX) )));
          var y = r * sin(radians(i*(360.0/ (mouseY) )));

          var r1 = 2;
          var x1 = r1 * tan(radians(i*(360.0 / (mouseX))));
          var y1 = r1 * sin(radians(i*(360.0 / (mouseY))));


          stroke(0, 30);
          line(pt.x + x, pt.y, 
          pt2.x + x1, pt2.y + y1);
        }
      }
    }
  }
}


function findPoints(){

  img.loadPixels();
  for (var i = img.width-1; i &gt;= 0; i -= 3){
    for (var j = img.height; j &gt;=0; j -= 3){

      var c = img.get(i, j);
      if (brightness(c) &lt; 255){
        pts.push(new p5.Vector(i, j, brightness(c)));
      }
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Drawing multiple rect() while have clear() in the draw()</title>
      <link>https://forum.processing.org/two/discussion/24083/drawing-multiple-rect-while-have-clear-in-the-draw</link>
      <pubDate>Sun, 10 Sep 2017 09:31:28 +0000</pubDate>
      <dc:creator>UsamaRehan</dc:creator>
      <guid isPermaLink="false">24083@/two/discussions</guid>
      <description><![CDATA[<p>I have a rect() which moves from bottom to top which is making the y postion of the rect -1 each time the draw() is called.
That works fine.</p>

<p><code>function setup(){
    createCanvas(600,600);
    var x = random(0,width);
    var y = height-50;
    var speed = 1;
}
function draw(){
    clear();
    y -= speed;
    rect(x,y,200,20);
}</code></p>

<p>Now,as the rect() keeps moving to the top,I want new rect() to appear from the bottom after a certain time say 1000ms. I tried to keep track of the history of the first rect() in a vector and I drew another rect from the vector. but it draw rect() but none of the rect() is moving to top. I am sure I am missing something here,and would like to someone to point that out for me.</p>

<p>Here is what I have tried so far.</p>

<pre><code>function rectangles(x,y,w,h,speed){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.speed = 1;

this.records = [];

    this.getAllPaths = function(){
       this.records.push(createVector(this.x,this.y));
    }

    this.drawRectangles = function(){
      this.y -= this.speed;
      rect(this.x,this.y,this.w,this.h);
    }
}

function setup(){
  createCanvas(window.innerWidth,window.innerHeight);
 }

function draw(){
   background(255);
   var r = new rectangles(random(0,width) , height-50 , random(50,300) , 10);
   r.getAllPaths();
   r.drawRectangles();
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How could I make my Mitosis cells explode?</title>
      <link>https://forum.processing.org/two/discussion/22077/how-could-i-make-my-mitosis-cells-explode</link>
      <pubDate>Wed, 19 Apr 2017 09:51:03 +0000</pubDate>
      <dc:creator>JVE10</dc:creator>
      <guid isPermaLink="false">22077@/two/discussions</guid>
      <description><![CDATA[<p>I am working on creating a mitosis cell that can split using a function rather than having to click on it. I want to try and create a ball that is in the centre of the screen and will split out in a radial fashion.</p>

<p>This is my Cell function</p>

<p>function Cell(pos, r, c){</p>

<pre><code>if (pos) {
  this.pos = pos.copy();
} else {
  this.pos = createVector(700, 360, 700, 160);
}

this.r = r || 60;
this.c = c || color(random(100, 200), 0, random(100, 200), 100);

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

}

this.mitosis = function() {
  this.pos.x += random(-10, 10);    
  var cell = new Cell(this.pos, this.r*0.8, this.c);
  return cell;
}

this.move = function () {
  var vel = p5.Vector.random2D();
  this.pos.add(vel);
}

this.show = function () {
    noStroke();
    fill(this.c);
    ellipse(this.pos.x, this.pos.y, this.r, this.r)
}
</code></pre>

<p>}</p>

<p>Any help would be really appreciated. Thank you</p>
]]></description>
   </item>
   <item>
      <title>Given two points (vectors), plot a 3rd so all three can be bisected by a straight line</title>
      <link>https://forum.processing.org/two/discussion/21445/given-two-points-vectors-plot-a-3rd-so-all-three-can-be-bisected-by-a-straight-line</link>
      <pubDate>Fri, 17 Mar 2017 01:44:40 +0000</pubDate>
      <dc:creator>noponies</dc:creator>
      <guid isPermaLink="false">21445@/two/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>I have small issue with creating linked beziers in that the link between each bezier is not that smooth. I'm using bezierVertex to draw the connected beziers.</p>

<p>What I'm trying to do is smooth out this join, and from what I understand I need to do the following "In order to make two curves A and B smoothly continuous, the last control point of A, the last point of A, and the first control point of B have to be on a straight line."</p>

<p>So, given two vectors like so;</p>

<pre><code>function setup() {
    createCanvas(windowWidth, windowHeight);
    background(255);
    noLoop();

    rectMode(CENTER);


    var v1 = createVector(random(200, 300), random(200, 300));
    var v2 = createVector(random(100, 400), random(300, 400));

    ellipse(v1.x, v1.y, 20, 20);
    ellipse(v2.x, v2.y, 20, 20);


    var angle = p5.Vector.angleBetween(v1, v2);

    console.log(angle, radians(angle));

    ellipse( 200 * sin(angle) , 200 * cos(angle), 30, 30);
}
</code></pre>

<p>I want to be able to plot the 3rd, so that I can set the control point of the next bezier in my in a straight line, and smooth the bezier connection point.</p>

<p>Thanks in advance.</p>

<p>First time using p5. I'm converting an old Director project as a learning exercise. The Director version looks like this; <a href="https://goo.gl/photos/K6zmabEyPUgTsmYMA" target="_blank" rel="nofollow">https://goo.gl/photos/K6zmabEyPUgTsmYMA</a></p>
]]></description>
   </item>
   <item>
      <title>cannot read property 'x' of undifined</title>
      <link>https://forum.processing.org/two/discussion/21024/cannot-read-property-x-of-undifined</link>
      <pubDate>Sun, 26 Feb 2017 22:05:06 +0000</pubDate>
      <dc:creator>smackgreen</dc:creator>
      <guid isPermaLink="false">21024@/two/discussions</guid>
      <description><![CDATA[<p>hello guys im new here and i learn some of p5 programming and i have a problem with some function
i wrote this program but i have a error on line 38</p>

<p>var d = dist(v1.x, v1.y ,v2.x, v2.y);
i got a error message 
cannot read property 'x' of undifined</p>

<pre><code>        var bubles=[]; 
        var reached = [];
        var unreached=[];
        function setup() {
           createCanvas(410,410);  
        }

        function mousePressed(){
          var v = createVector(mouseX,mouseY);
          ellipse(v.x,v.y,20,20);
          bubles.push(v);
        }

        function draw() {
            background(200,200,200);

            for(var i=0;i&lt;bubles.length;i++){
              unreached.push(bubles[i]);
            }

            reached.push(unreached[0]);
            unreached.splice(0,1); 

            while(unreached.length&gt;0){
               var min=1000;
              var rIndex;
              var uIndex;
              for(var i=0; i&lt;reached.length;i++){
                for(var j=0; j&lt;unreached.length;j++){
                  var v1 = reached[i];
                  var v2 = unreached[j];
                  var d = dist(v1.x, v1.y ,v2.x, v2.y);
                  if(d&lt;min){
                    min=d;
                    rIndex=i;
                    uIndex=j; 
                  }
                }
              }
              line(reached[rIndex].x,reached[rIndex].y,unreached[uIndex].x,unreached[uIndex].y);  
              reached.push(unreached[uIndex]);
              unreached.splice(0,uIndex);
            }
        }
</code></pre>
]]></description>
   </item>
   <item>
      <title>p5 method createVector not working..? o.O</title>
      <link>https://forum.processing.org/two/discussion/20520/p5-method-createvector-not-working-o-o</link>
      <pubDate>Sat, 28 Jan 2017 20:10:00 +0000</pubDate>
      <dc:creator>Wadddup</dc:creator>
      <guid isPermaLink="false">20520@/two/discussions</guid>
      <description><![CDATA[<p>Hi!
I'm currently trying to make a vector with the p5 method createVector using defined variables (which change with user input)  in conjuction with the random() method, but all I'm getting is a p5.Vector with an x- and y-coordinate in brackets (in red font and for example "84.324567") when I look at it in the webpage console. The result seems to be that it becomes and "invalid" vector and thus not showing up when I try to draw it. Everything works fine though when I hard-code numbers into random() (like "random(20, 380)". Code below.</p>

<pre><code>function setup() {

    paLAmount = createInput("Amount of parallel lines");
      nAmount = createInput("Amount of needles");
      nLength = createInput("Length of needles");
}


    var makeNeedles = function() {

      var setNAmount = nAmount.value();
      var setNLength = nLength.value();

      //Creating random vectors
      for(var j = 0; j &lt; setNAmount; j++) {

         var cx = random(setNLength + 1, (width - setNLength - 1));
         var cy = random(setNLength + 1, (height - setNLength - 1));
         var a = createVector(cx, cy);

         console.log(cx, cy);

         coIndex1.push(a);
   };
}
</code></pre>

<p>All help is appreciated!:)</p>
]]></description>
   </item>
   <item>
      <title>How to call static p5.Vector methods in instance mode</title>
      <link>https://forum.processing.org/two/discussion/20509/how-to-call-static-p5-vector-methods-in-instance-mode</link>
      <pubDate>Fri, 27 Jan 2017 23:58:33 +0000</pubDate>
      <dc:creator>wigwam</dc:creator>
      <guid isPermaLink="false">20509@/two/discussions</guid>
      <description><![CDATA[<p>Hi so in instance mode with instance <code>p= new p5(..)</code>, I can make a vector <code>v1 = p.createVector()</code> and can then call <code>v1.dist(v2)</code> but I cannot call <code>p5.Vector.dist(v1,v2)</code> nor <code>p.p5.Vector</code>. This may be a general js question but I was wondering how to call p5.Vector functions like <code>angleBetween()</code> or <code>random2D()</code> etc.</p>
]]></description>
   </item>
   <item>
      <title>Is it better to create vectors using the createVector() function or as a new object?</title>
      <link>https://forum.processing.org/two/discussion/19734/is-it-better-to-create-vectors-using-the-createvector-function-or-as-a-new-object</link>
      <pubDate>Tue, 13 Dec 2016 20:27:04 +0000</pubDate>
      <dc:creator>Tsskyx</dc:creator>
      <guid isPermaLink="false">19734@/two/discussions</guid>
      <description><![CDATA[<p>I currently use them interchangeably. When I need a vector with specific parameters already in place, I use createVector(). When I don't specify any parameters, I do it this way: var v = new p5.Vector();</p>

<p>I don't really know if there's any huge advantage of using the createVector() function, but I use vectors regularly to shorten my code, so I'm curious.</p>
]]></description>
   </item>
   <item>
      <title>Event in canvas triggers html heading style changes question/problem.</title>
      <link>https://forum.processing.org/two/discussion/18131/event-in-canvas-triggers-html-heading-style-changes-question-problem</link>
      <pubDate>Sat, 10 Sep 2016 19:27:06 +0000</pubDate>
      <dc:creator>AtheistApe</dc:creator>
      <guid isPermaLink="false">18131@/two/discussions</guid>
      <description><![CDATA[<p>Hello,</p>

<p>I've been playing with p5js by coming up with ideas and trying to implement them in p5js. I've had some success but have an issue with one of my examples that I'm hoping someone can help with.</p>

<p>My example is creating a ball with a random color in the canvas with a mousePressed event which bounces off the edges of the canvas. When the ball hits an edge, it changes the color of the h1 heading on the page to the balls color. Multiple mouse clicks create multiple balls.</p>

<p>Everything seems to work fine except when I click the mouse outside the canvas onto the page. When I do this the heading color no longer changes color when the different colored balls strike the edges of the canvas.</p>

<p>I'm hoping someone can help me trouble-shoot my code as well as offering advice on other ways to improve it.</p>

<p>Also, I tried including my code into code-blocks using ``` but it doesn't seem to work too well. I ended up indenting every line four spaces to get it to render correctly. Any hints here?</p>

<p><strong>sketch.js:</strong></p>

<pre><code>var balls = [];
var heading;
var blip;

function preload() {
  blip = loadSound('blip.wav');
}

function setup() {
  createCanvas(600, 400);
  heading = select('#color_txt');
}

function draw() {
  background(220);
  for (i = 0; i &lt; balls.length; i++) {
   balls[i].move();
   balls[i].display();
 }
}

function mousePressed() {
  ball = new Ball();
  balls.push(ball);
}

function hex_color(r, g, b) {
  var hx = "#" + hex(r,2) + hex(g,2) + hex(b,2);
  return hx;
}

function Ball() {
  this.position = createVector(mouseX, mouseY);
  this.velocity = createVector(random(-4,4), random(-4,4));
  this.col = hex_color(random(255), random(255), random(255));
  this.diam = random(15,50);
  this.rad = 0.5 * this.diam;

  this.move = function() {
    this.position.add(this.velocity);

    if (this.position.x + this.rad &gt; width || this.position.x - this.rad &lt; 0) {
      this.velocity.x *= -1;
      blip.play();
      heading.style('color', this.col);
    }

    if (this.position.y + this.rad &gt; height || this.position.y - this.rad &lt; 0) {
      this.velocity.y *= -1;
      blip.play();
      heading.style('color', this.col);
    }
  }

  this.display = function() {
    fill(this.col);
    ellipse(this.position.x, this.position.y, this.diam, this.diam);
  }
}
</code></pre>

<p><strong>index.html:</strong></p>

<pre><code>&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;Vectors&lt;/title&gt;
    &lt;script src="libraries/p5.js" type="text/javascript"&gt;&lt;/script&gt;

    &lt;script src="libraries/p5.dom.js" type="text/javascript"&gt;&lt;/script&gt;
    &lt;script src="libraries/p5.sound.js" type="text/javascript"&gt;&lt;/script&gt;

    &lt;script src="sketch.js" type="text/javascript"&gt;&lt;/script&gt;

    &lt;style&gt; body {padding: 0; margin: 0;} canvas {vertical-align: top;} &lt;/style&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;h1 id='color_txt'&gt;Random balls bouncing around the canvas&lt;/h1&gt;
    &lt;p&gt;Click on the canvas to generate a ball or random radius, color
      and velocity&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
]]></description>
   </item>
   <item>
      <title>multiple canvases on one page</title>
      <link>https://forum.processing.org/two/discussion/17306/multiple-canvases-on-one-page</link>
      <pubDate>Sun, 26 Jun 2016 08:23:53 +0000</pubDate>
      <dc:creator>allonestring</dc:creator>
      <guid isPermaLink="false">17306@/two/discussions</guid>
      <description><![CDATA[<p>I can't get more than one sketch to run on a webpage. Each works fine when it's on its own. I have in the sketches</p>

<pre><code>var c1 = createCanvas(200, 200);
c1.parent('firstcanvas');
</code></pre>

<p>and</p>

<pre><code>var c2 = createCanvas(300, 300);
c2.parent('secondcanvas');
</code></pre>

<p>and, in the html</p>

<pre><code>&lt;div id = "firstcanvas"&gt;&lt;/div&gt;
&lt;script src="pathto/firstscript.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;div id = "secondcanvas"&gt;&lt;/div&gt;
&lt;script src="pathto/secondscript.js" type="text/javascript"&gt;&lt;/script&gt;
</code></pre>

<p>Inspect shows</p>

<pre><code>&lt;div id="firstcanvas"&gt;&lt;/div&gt;
&lt;script src="pathto/firstscript.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;canvas id="defaultCanvas0" width="200" height="200" style="width: 200px; height: 200px;"&gt;&lt;/canvas&gt;
&lt;div id = "secondcanvas"&gt;&lt;/div&gt;
&lt;script src="pathto/secondscript.js" type="text/javascript"&gt;&lt;/script&gt;
</code></pre>

<p>and the webpage just shows the first sketch. I've tried various combos of div and canvas, with no luck.</p>

<pre><code>&lt;div id="firstcanvas"&gt;
&lt;script src="pathto/firstscript.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;canvas id="firstcanvas" width="200" height="200" style="width: 200px; height: 200px;"&gt;&lt;/canvas&gt;
&lt;/div&gt;
</code></pre>

<p>and</p>

<pre><code>&lt;canvas id="firstcanvas"&gt;
&lt;script src="pathto/firstscript.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;/canvas&gt;
</code></pre>

<p>with no luck. If I put ordinary javascript animations on the page, as long as I target the right canvas id, they work fine together.
Any ideas?</p>
]]></description>
   </item>
   <item>
      <title>How to use p5.Vector?</title>
      <link>https://forum.processing.org/two/discussion/15934/how-to-use-p5-vector</link>
      <pubDate>Sat, 09 Apr 2016 13:50:08 +0000</pubDate>
      <dc:creator>nick3499</dc:creator>
      <guid isPermaLink="false">15934@/two/discussions</guid>
      <description><![CDATA[<p>Using p5.js library, unable to get the following code to run. am sure that the problem has to do with coding the p5.Vector, but troubleshooting has been unsuccessful. A red ball displays at the center of the canvas, but does not move. Trying to simulate velocity.</p>

<pre><code>var l;
var v;

function setup() {
  createCanvas(1152, 648);
}

function draw() {
  background(255);
  l = new p5.Vector(576, 324);
  v = new p5.Vector(16, 9);
  l.add(v);
  if ((l.x &gt; width) || (l.x &lt; 0)) {
    v.x = v.x * -1;
  }
  if ((l.y &gt; height) || (l.y &lt; 0)) {
    v.y = v.y * -1;
  }
  noStroke();
  fill(255, 0, 0);
  ellipse(l.x, l.y, 100, 100);
  frameRate(30);
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Change offset of vector</title>
      <link>https://forum.processing.org/two/discussion/15917/change-offset-of-vector</link>
      <pubDate>Fri, 08 Apr 2016 09:03:50 +0000</pubDate>
      <dc:creator>giorgiomartini</dc:creator>
      <guid isPermaLink="false">15917@/two/discussions</guid>
      <description><![CDATA[<p>Hi!
I want to create something like a wartptime:
<img src="http://www.borongaja.com/data_images/out/15/630114-light-speed-warp-speed.jpg" alt="" /></p>

<p>I already have an array of vectors wich start from the center of the image and spread outwards: <a href="http://codepen.io/giorgiomartini/pen/groeRm?editors=0010" target="_blank" rel="nofollow">http://codepen.io/giorgiomartini/pen/groeRm?editors=0010</a></p>

<p>Also everytime you click, the oouter vectors change randomly.</p>

<p>Now I want that instead that the line starts at the center (which is where the starting vector is) that it starts somewhere further away from the center, but along that vector.</p>

<p>something like this:
<img src="https://dl.dropboxusercontent.com/u/12886551/p5warp.jpg" alt="" /></p>

<p>As you can see they are the same lines but their start point is offseted through the line direction.</p>

<p>Any idea how to acomplish this ?</p>

<p>Thanks!</p>
]]></description>
   </item>
   <item>
      <title>p5.Vector feature request: unit + magnitude method</title>
      <link>https://forum.processing.org/two/discussion/15126/p5-vector-feature-request-unit-magnitude-method</link>
      <pubDate>Thu, 25 Feb 2016 20:59:43 +0000</pubDate>
      <dc:creator>fearless_fool</dc:creator>
      <guid isPermaLink="false">15126@/two/discussions</guid>
      <description><![CDATA[<p>When creating physics models, I often find myself converting a distance vector (between two points) into a unit vector AND the magnitude.  This bugs me simply because I know the computation of the unit vector required the magnitude as well.</p>

<p>One possible design would be give mag() an optional p5.Vector argument, which if provided, fills in the vector with the unit vector:</p>

<pre><code>var v = createVector(20, 10);
var u = createVector();
var m = v.mag(u);

// m has value 22.360679774997898
// u has components [0.8944271909999159, 0.4472135954999579]
</code></pre>

<p>Would this be useful to the community at large?</p>
]]></description>
   </item>
   <item>
      <title>Using heading() to generate angles</title>
      <link>https://forum.processing.org/two/discussion/14302/using-heading-to-generate-angles</link>
      <pubDate>Wed, 06 Jan 2016 15:45:51 +0000</pubDate>
      <dc:creator>Robbert</dc:creator>
      <guid isPermaLink="false">14302@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to use heading to position a row of squares with an angle in direction to the mouse.
Just as a test for using vector's and angles. I wrote a function for the squares, and generate an angle with mouseX and mouseY. Though P5 calculates the angles from the 0,0 point of the canvas, so i tried to create the vector in a matrix, but this doesn't seem to change much. So the question is; is there a way to generate the angle with mouseX/mouseY relative to the position of the object that has been created. or do I have to use if statements to check whether the X/Y of the mouse is &lt;/&gt; relative to the objects position.</p>

<p>Code:</p>

<pre><code>var rectArray = [];

var mouseVector;

function setup() {
  createCanvas(windowWidth,windowHeight);

  for (i = 0;i &lt; 100; i++) {
  rectArray.push(new Square(10*i,40));
  }
}

function draw() {

  for (i = 0; i &lt; rectArray.length; i++) {
    rectArray[i].rotation();
    rectArray[i].update();
    rectArray[i].display();
  }

}

function Square(x,y) {
  this.x = x;
  this.y = y;
  this.size = 20;
  this.color = color(200);
  this.target = 0;
  this.rotationValue = 0;

  this.rotation = function() {
    push();
    translate(this.x,this.y);
    mouseVector = createVector(mouseX,mouseY);
    this.target = mouseVector.heading();
    this.rotationValue = this.target;
    pop();
  }

  this.update = function() {
    //this.x = this.x + 1;
  }

  this.display = function() {
    push();
    translate(this.x,this.y);
    rectMode(CENTER);
    rotate(this.rotationValue);
    fill(this.color);
    rect(0,0,this.size,this.size);
    pop();
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>return from angleBetween()</title>
      <link>https://forum.processing.org/two/discussion/14319/return-from-anglebetween</link>
      <pubDate>Thu, 07 Jan 2016 14:52:15 +0000</pubDate>
      <dc:creator>Robbert</dc:creator>
      <guid isPermaLink="false">14319@/two/discussions</guid>
      <description><![CDATA[<p>Yesterday I've asked a question on the forum about calculating angles between different objects. The function angleBetween() seemed to be a perfect solution. I've been trying to figure it out and even though I do get it to work, I can't get the right values out of it. The function always returns a value between 0 and 0.78 which is the 90 degree radians. Do we have to use an if/else statement to determine where the position of 1 objects is in relation to the other one, or am I missing something out on this function? I hope some of you could help me out :)</p>

<pre><code>function setup() {
createCanvas(200,200);
}

function draw() {
  background(255);
  var v1 = createVector(width/2,height/2);
  var v2 = createVector(mouseX,mouseY);

  var angle = p5.Vector.angleBetween(v1,v2);
  print(angle);

  push();
  translate(width/2,height/2);
  rotate(angle);
  rectMode(CENTER);
  rect(0,0,10,10);
  pop();
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Creating vectors as global variables</title>
      <link>https://forum.processing.org/two/discussion/8259/creating-vectors-as-global-variables</link>
      <pubDate>Wed, 19 Nov 2014 23:00:36 +0000</pubDate>
      <dc:creator>drawcult</dc:creator>
      <guid isPermaLink="false">8259@/two/discussions</guid>
      <description><![CDATA[<p>When I use createVector() to assign a vector object to a global variable I get an object error.  I've been working around the issue by creating a custom object that contains the vector objects I need and assigning the custom object to a global variable.  I'm curious why it isn't possible to assign vector objects to global variables, any ideas?</p>
]]></description>
   </item>
   <item>
      <title>using PVectors in JS mode</title>
      <link>https://forum.processing.org/two/discussion/7609/using-pvectors-in-js-mode</link>
      <pubDate>Tue, 14 Oct 2014 12:26:58 +0000</pubDate>
      <dc:creator>rackatansky</dc:creator>
      <guid isPermaLink="false">7609@/two/discussions</guid>
      <description><![CDATA[<p>hello, I created object composed of few PVectors (position, velocity, accleration and else, based on Nature of Code book). I have tough times now to create and run such object in Java Script mode. Can anyone please tell me how I can assemble an object from other objects (PVectors) in JS? Example which I thought would have worked:</p>

<pre><code>function Mover(x, y, mass) {
   this.position = new PVector(x,y);
   this.velocity = new PVector();
   this.acceleration = new PVector();
   this.mass = mass;

this.display = function() {
   ellipse(position.x, position.y, 50,50);
}

this.update = function() {
   this.velocity.add(this.acceleration);
   this.position.add(this.velocity);
   this.acceleration.mult(0);
}

this.applyForce = function(force) {
   var f = PVector.div(force.get(), this.mass);
   this.acceleration.add(f);
}
}
</code></pre>
]]></description>
   </item>
   </channel>
</rss>