Uncaught TypeError using vectors in pjs
in
Processing with Other Languages
•
4 months ago
Hello everyone,
I'm trying to port a simple example of Node+Spring from Generative Gestaltung to processing js with no luck.
I've got rid of libraries and stuff that could cause pbs, it runs in plain java so it should work but I get an :
Uncaught TypeError: Object #<Node> has no method 'get'
So it seems that the error comes from the class Node, and as a matter of fact the class extends PVector, so I guess the issue would come from there. It's a weird error and I really don't know were to start digging to resolve this.
I already used extended class in pjs and it didn't seem to cause trouble, does anybody have an idea how to solve this ?
- Node nodeA, nodeB;
- Spring spring;
- void setup() {
- size(600, 600);
- smooth();
- fill(0);
- PVector vecA = new PVector( width/2+random(-50, 50), height/2+random(-50, 50));
- PVector vecB = new PVector( width/2+random(-50, 50), height/2+random(-50, 50));
- nodeA = new Node(vecA);
- nodeB = new Node(vecB);
- nodeA.setDamping(0.1);
- nodeB.setDamping(0.1);
- spring = new Spring(nodeA, nodeB);
- spring.setLength(100);
- spring.setStiffness(0.6);
- spring.setDamping(0.3);
- }
- void draw() {
- background(255);
- if (mousePressed == true) {
- nodeA.x = mouseX;
- nodeA.y = mouseY;
- }
- // update spring
- spring.update();
- // update node positions
- nodeA.update();
- nodeB.update();
- // draw spring
- stroke(0, 130, 164);
- strokeWeight(4);
- line(nodeA.x, nodeA.y, nodeB.x, nodeB.y);
- // draw nodes
- noStroke();
- fill(0);
- ellipse(nodeA.x, nodeA.y, 20, 20);
- ellipse(nodeB.x, nodeB.y, 20, 20);
- }
- // M_6_1_01.pde
- // Node.pde
- //
- // Generative Gestaltung, ISBN: 978-3-87439-759-9
- // First Edition, Hermann Schmidt, Mainz, 2009
- // Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
- // Copyright 2009 Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
- //
- // http://www.generative-gestaltung.de
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- class Node extends PVector{
- // ------ properties ------
- // if needed, an ID for the node
- String id = "";
- float diameter = 0;
- float minX = -60000; // those were Float.MAX_VALUE before being 6000 because pjs complained about not knowing Float with a capital F
- float maxX = 60000;
- float minY = -60000;
- float maxY = 60000;
- float minZ = -60000;
- float maxZ = 60000;
- PVector velocity = new PVector();
- PVector pVelocity = new PVector();
- float maxVelocity = 10;
- float damping = 0.5f;
- // radius of impact
- float radius = 200;
- // strength: positive for attraction, negative for repulsion (default for Nodes)
- float strength = -1;
- // parameter that influences the form of the function
- float ramp = 1.0f;
- // ------ constructors ------
- Node() {
- }
- Node(float theX, float theY) {
- x = theX;
- y = theY;
- }
- Node(float theX, float theY, float theZ) {
- x = theX;
- y = theY;
- z = theZ;
- }
- Node(PVector theVector) {
- x = theVector.x;
- y = theVector.y;
- z = theVector.z;
- }
- // ------ rotate position around origin ------
- void rotateX(float theAngle) {
- float newy = y * cos(theAngle) - z * sin(theAngle);
- float newz = y * sin(theAngle) + z * cos(theAngle);
- y = newy;
- z = newz;
- }
- void rotateY(float theAngle) {
- float newx = x * cos(-theAngle) - z * sin(-theAngle);
- float newz = x * sin(-theAngle) + z * cos(-theAngle);
- x = newx;
- z = newz;
- }
- void rotateZ(float theAngle) {
- float newx = x * cos(theAngle) - y * sin(theAngle);
- float newy = x * sin(theAngle) + y * cos(theAngle);
- x = newx;
- y = newy;
- }
- // ------ calculate attraction ------
- void attract(Node[] theNodes) {
- // attraction or repulsion part
- for (int i = 0; i < theNodes.length; i++) {
- Node otherNode = theNodes[i];
- // stop when empty
- if (otherNode == null) break;
- // not with itself
- if (otherNode == this) continue;
- this.attract(otherNode);
- }
- }
- void attract(Node theNode) {
- float d = PVector.dist(this, theNode);
- if (d > 0 && d < radius) {
- float s = pow(d / radius, 1 / ramp);
- float f = s * 9 * strength * (1 / (s + 1) + ((s - 3) / 4)) / d;
- PVector df = PVector.sub(this, theNode);
- df.mult(f);
- theNode.velocity.x += df.x;
- theNode.velocity.y += df.y;
- theNode.velocity.z += df.z;
- }
- }
- // ------ update positions ------
- void update() {
- update(false, false, false);
- }
- void update(boolean theLockX, boolean theLockY, boolean theLockZ) {
- velocity.limit(maxVelocity);
- pVelocity = velocity.get();
- if (!theLockX) x += velocity.x;
- if (!theLockY) y += velocity.y;
- if (!theLockZ) z += velocity.z;
- if (x < minX) {
- x = minX - (x - minX);
- velocity.x = -velocity.x;
- }
- if (x > maxX) {
- x = maxX - (x - maxX);
- velocity.x = -velocity.x;
- }
- if (y < minY) {
- y = minY - (y - minY);
- velocity.y = -velocity.y;
- }
- if (y > maxY) {
- y = maxY - (y - maxY);
- velocity.y = -velocity.y;
- }
- if (z < minZ) {
- z = minZ - (z - minZ);
- velocity.z = -velocity.z;
- }
- if (z > maxZ) {
- z = maxZ - (z - maxZ);
- velocity.z = -velocity.z;
- }
- velocity.mult(1 - damping);
- }
- // ------ getters and setters ------
- String getID() {
- return id;
- }
- void setID(String theID) {
- this.id = theID;
- }
- float getDiameter() {
- return diameter;
- }
- void setDiameter(float theDiameter) {
- this.diameter = theDiameter;
- }
- void setBoundary(float theMinX, float theMinY, float theMinZ,
- float theMaxX, float theMaxY, float theMaxZ) {
- this.minX = theMinX;
- this.maxX = theMaxX;
- this.minY = theMinY;
- this.maxY = theMaxY;
- this.minZ = theMinZ;
- this.maxZ = theMaxZ;
- }
- void setBoundary(float theMinX, float theMinY, float theMaxX,
- float theMaxY) {
- this.minX = theMinX;
- this.maxX = theMaxX;
- this.minY = theMinY;
- this.maxY = theMaxY;
- }
- float getMinX() {
- return minX;
- }
- void setMinX(float theMinX) {
- this.minX = theMinX;
- }
- float getMaxX() {
- return maxX;
- }
- void setMaxX(float theMaxX) {
- this.maxX = theMaxX;
- }
- float getMinY() {
- return minY;
- }
- void setMinY(float theMinY) {
- this.minY = theMinY;
- }
- float getMaxY() {
- return maxY;
- }
- void setMaxY(float theMaxY) {
- this.maxY = theMaxY;
- }
- float getMinZ() {
- return minZ;
- }
- void setMinZ(float theMinZ) {
- this.minZ = theMinZ;
- }
- float getMaxZ() {
- return maxZ;
- }
- void setMaxZ(float theMaxZ) {
- this.maxZ = theMaxZ;
- }
- PVector getVelocity() {
- return velocity;
- }
- void setVelocity(PVector theVelocity) {
- this.velocity = theVelocity;
- }
- float getMaxVelocity() {
- return maxVelocity;
- }
- void setMaxVelocity(float theMaxVelocity) {
- this.maxVelocity = theMaxVelocity;
- }
- float getDamping() {
- return damping;
- }
- void setDamping(float theDamping) {
- this.damping = theDamping;
- }
- float getRadius() {
- return radius;
- }
- void setRadius(float theRadius) {
- this.radius = theRadius;
- }
- float getStrength() {
- return strength;
- }
- void setStrength(float theStrength) {
- this.strength = theStrength;
- }
- float getRamp() {
- return ramp;
- }
- void setRamp(float theRamp) {
- this.ramp = theRamp;
- }
- }
- // M_6_1_02.pde
- // Spring.pde
- //
- // Generative Gestaltung, ISBN: 978-3-87439-759-9
- // First Edition, Hermann Schmidt, Mainz, 2009
- // Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
- // Copyright 2009 Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
- //
- // http://www.generative-gestaltung.de
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- class Spring {
- Node fromNode;
- Node toNode;
- float length = 100;
- float stiffness = 0.6;
- float damping = 0.9;
- // ------ constructors ------
- Spring(Node theFromNode, Node theToNode) {
- fromNode = theFromNode;
- toNode = theToNode;
- }
- Spring(Node theFromNode, Node theToNode, float theLength, float theStiffness, float theDamping) {
- fromNode = theFromNode;
- toNode = theToNode;
- length = theLength;
- stiffness = theStiffness;
- damping = theDamping;
- }
- // ------ apply forces on spring and attached nodes ------
- void update() {
- // calculate the target position
- // target = normalize(to - from) * length + from
- PVector diff = PVector.sub(toNode, fromNode);
- diff.normalize();
- diff.mult(length);
- PVector target = PVector.add(fromNode, diff);
- PVector force = PVector.sub(target, toNode);
- force.mult(0.5);
- force.mult(stiffness);
- force.mult(1 - damping);
- toNode.velocity.add(force);
- fromNode.velocity.add(PVector.mult(force, -1));
- }
- // ------ getters and setters ------
- Node getFromNode() {
- return fromNode;
- }
- void setFromNode(Node theFromNode) {
- fromNode = theFromNode;
- }
- Node getToNode() {
- return toNode;
- }
- void setToNode(Node theToNode) {
- toNode = theToNode;
- }
- float getLength() {
- return length;
- }
- void setLength(float theLength) {
- this.length = theLength;
- }
- float getStiffness() {
- return stiffness;
- }
- void setStiffness(float theStiffness) {
- this.stiffness = theStiffness;
- }
- float getDamping() {
- return damping;
- }
- void setDamping(float theDamping) {
- this.damping = theDamping;
- }
- }
1