We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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...
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
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:
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