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.applyForce = function(force) {
   var f = PVector.div(force.get(), this.mass);


  • 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:

  • 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 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:

    function Mover(x, y, m) {
      this.pos  = createVector(x, y);
      this.vel  = createVector(), this.acc = createVector();
      this.mass = m;
    Mover.prototype.DIAM = 50;
    Mover.prototype.applyForce = function(force) {
    Mover.prototype.update = function() {
      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:

    And 1 which uses Vector:

Sign In or Register to comment.