Hi there,
I am sure this is a stupid noob question, but I just can't seem to understand how to pass data between objects. In this particular example, I want to calculate the distance between two different objects.
Basically, I want to create a shape (obj 1) that responds to the distance from a point (obj 2). Later, there will be multiple shapes and points.
I have this working inside a draw, but when I try to convert it into objects I run into all sorts of problems. I have included the code below. The commented out part is the non-working part.
Code:Component a;
VectorPoint p;
void setup() {
size (600, 300);
a = new Component(100., 100.);
p = new VectorPoint (200., 200.);
}
void draw() {
background (160);
// a.display();
p.display();
}
void mouseDragged() {
p.update();
}
class Component {
// Methods
VectorPoint scalePoint;
PVector center;
PVector p1, p2, p3, p4;
PVector v1, v2, v3, v4;
float componentMin = 20.;
float componentMax = 60.;
// Constructor
Component (float cx_, float cy_) {
scalePoint = new VectorPoint(400,100);
center = new PVector (cx_, cy_);
p1 = new PVector (center.x + 30, center.y - 30);
p2 = new PVector (center.x - 30, center.y - 30);
p3 = new PVector (center.x - 30, center.y + 30);
p4 = new PVector (center.x + 30, center.y + 30);
v1 = new PVector (0,0);
v2 = new PVector (0,0);
v3 = new PVector (0,0);
v4 = new PVector (0,0);
}
void display() {
// Calculate the maximum size possible on the screen
float maxSize = sqrt (width*width + height*height);
// Inner square to calculate the distances
ellipse (p1.x, p1.y, 5, 5);
ellipse (p2.x, p2.y, 5, 5);
ellipse (p3.x, p3.y, 5, 5);
ellipse (p4.x, p4.y, 5, 5);
/*
// Distances from scalePoint to point on inner square
float m1 = PVector.dist (scalePoint, p1);
float m2 = PVector.dist (scalePoint, p2);
float m3 = PVector.dist (scalePoint, p3);
float m4 = PVector.dist (scalePoint, p4);
float magnitude1 = map(m1, 0, maxSize, componentMin, componentMax);
float magnitude2 = map(m2, 0, maxSize, componentMin, componentMax);
float magnitude3 = map(m3, 0, maxSize, componentMin, componentMax);
float magnitude4 = map(m4, 0, maxSize, componentMin, componentMax);
v1.set ((center.x + (magnitude1 * cos ((7*PI) / 4))), (center.y + (magnitude1 * sin ((7*PI) / 4))), 0);
v2.set ((center.x + (magnitude2 * cos ((5*PI) / 4))), (center.y + (magnitude2 * sin ((5*PI) / 4))), 0);
v3.set ((center.x + (magnitude3 * cos ((3*PI) / 4))), (center.y + (magnitude3 * sin ((3*PI) / 4))), 0);
v4.set ((center.x + (magnitude4 * cos ((PI) / 4))), (center.y + (magnitude4 * sin ((PI) / 4))), 0);
// The component - center lines
strokeWeight(0.5);
ellipse (center.x, center.y, 10, 10);
line (center.x, center.y, v1.x, v1.y);
line (center.x, center.y, v2.x, v2.y);
line (center.x, center.y, v3.x, v3.y);
line (center.x, center.y, v4.x, v4.y);
// The component - border edges
strokeWeight(1);
line (v1.x, v1.y, v2.x, v2.y);
line (v2.x, v2.y, v3.x, v3.y);
line (v3.x, v3.y, v4.x, v4.y);
line (v4.x, v4.y, v1.x, v1.y);
// Line from center to scalePoint
//line (center.x, center.y, scalePoint.x, scalePoint.y);
*/
}
}
class VectorPoint {
PVector vp;
VectorPoint (float x_, float y_) {
vp = new PVector (x_, y_);
}
void display() {
// Scale Point
strokeWeight(0.5);
ellipse (vp.x, vp.y, 10, 10);
line (vp.x - 10, vp.y, vp.x + 10, vp.y);
line (vp.x, vp.y - 10, vp.x, vp.y + 10);
}
void update() {
if (dist(mouseX, mouseY, vp.x, vp.y) < 10) {
vp.x += (mouseX - pmouseX);
vp.y += (mouseY - pmouseY);
}
}
}