using PVectors in JS mode

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:

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);
}
}

Answers

  • I have tough times now to create and run such object in JavaScript mode.

    JS Mode automatically transpiles Java to JS syntax! Your code above is regular JS, not JS Mode!
    If you wanna write in JS syntax, you should look at P5.JS project: http://p5js.org

  • GoToLoop, the OP correctly posted in a P5.js category already...

  • edited October 2014

    Oops! I almost never read categories! I go for title and description! 8-X
    I'll re-peruse OP's request under this new "data light"! :D

  • "I almost never read categories!"
    That's probably why I am perhaps the only one to move topics to proper categories... ;-)

  • @rackatansky if you are using p5.js, it is p5.Vector, not PVector (see http://p5js.org/reference/#p5.Vector). if you are using javascript mode for Processing you can post this question under Using Processing > Question About Modes > JavaScript Mode

  • edited October 2014 Answer ✓

    JS' way of doing "classes" are very diff. than Java's. And there are various patterns for it.
    Easiest way is define a function(), which'll be the "constructor", and then use its name w/ prototype to define the rest, like methods & constants:

    // forum.processing.org/two/discussion/7609/using-pvectors-in-js-mode
    
    function Mover(x, y, m) {
      this.pos  = createVector(x, y);
      this.vel  = createVector(), this.acc = createVector();
      this.mass = m;
    
      Object.freeze(this);
    }
    
    Mover.prototype.DIAM = 50;
    
    Mover.prototype.applyForce = function(force) {
      this.acc.add(force);
    }
    
    Mover.prototype.update = function() {
      this.pos.add(this.vel.add(this.acc.div(this.mass)));
      this.acc.set(0, 0, 0);
    }
    
    Mover.prototype.display() {
      ellipse(this.pos.x, this.pos.y, Mover.prototype.DIAM, Mover.prototype.DIAM);
    }
    
    Object.freeze(Mover), Object.freeze(Mover.prototype);
    

    Another example w/ prototype:
    http://forum.processing.org/two/discussion/5615/coding-noob-needs-help-how-do-i-draw-an-expanding-circle-that-triggers-with-a-mouse-click

    And 1 which uses Vector:
    http://forum.processing.org/two/discussion/7294/why-doesnt-my-sketch-run-in-javascript-mode

Sign In or Register to comment.