Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
sylvain.us
sylvain.us's Profile
1
Posts
1
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
What is the syntax to call class A object into class B
[3 Replies]
14-Mar-2013 07:42 AM
Forum:
Programming Questions
Hi everybody!
I will start with an example,
One class A define a ball bouncing around the screen. This ball is call several times (say 20).
An other class B define another ball. This ball is call 3 times.
I want ball B to interact with ball A (attraction, repulsion, etc.). But I can't find a way to call the object B in the class A.
Anyone understand?
Now, here is what I have done:
import toxi.geom.*;
Particule [] Environment = new Particule [300];
void setup() {
size(600, 600, P3D);
smooth();
for (int i = 0; i < Environment.length; i++) {
Vec3D originLoc = new Vec3D(random(width), random(height),0);
Environment[i] = new Particule(originLoc);
}
}
void draw() {
background(0);
for (int i = 0; i < Environment.length; i++) {
Environment[i].run();
}
}
class Particule {
// Global Variables
Vec3D loc = new Vec3D();
Vec3D speed = new Vec3D(random(-1, 1), random(-1, 1), 0);
Vec3D locRef = new Vec3D(100, 100, 0);
Vec3D speedRef = new Vec3D(0.5, 0.7, 0);
int RAD = round(random(1, 5));
//Constructor
Particule(Vec3D _loc) {
loc = _loc;
}
//Method
void run() {
display();
aggregate();
separate();
move();
bounce();
}
void bounce() {
if (loc.x > width | loc.x < 0) {
speed.x = - speed.x;
}
if (loc.y >= height | loc.y < 0) {
speed.y = - speed.y;
}
if (locRef.x > width | locRef.x < 0) {
speedRef.x = - speedRef.x;
}
if (locRef.y >= height | locRef.y < 0) {
speedRef.y = - speedRef.y;
}
}
void move() {
loc.addSelf(speed);
locRef.addSelf(speedRef);
}
void display() {
noFill();
stroke(255);
ellipse(loc.x, loc.y, RAD, RAD);
pushStyle();
noStroke();
// fill(255, 0, 0);
ellipse(locRef.x, locRef.y, 10, 10);
popStyle();
}
void aggregate() {
Vec3D distance = new Vec3D();
distance = loc.copy();
distance = locRef.sub(distance);
float d = distance.magnitude();
println(d);
if (d > -100 && d < 100) {
distance.normalize();
speed = distance;
//stroke(0,255,0);
//line(loc.x, loc.y, distance.x, distance.y);
stroke(0, 0, 255);
line (locRef.x, locRef.y, loc.x, loc.y);
}
}
void separate() {
Vec3D distance = new Vec3D();
distance = loc.copy();
distance = locRef.sub(distance);
float d = distance.magnitude();
if (d <= RAD*5 && d >= -RAD*5) {
speed.invert();
}
}
}
I am more asking for a general syntax to use a class object into an other class.
Thanks!
Sylvain.
«Prev
Next »
Moderate user : sylvain.us
Forum