Help about Pvector & class objet please
in
Programming Questions
•
1 year ago
Hello everybody !
I am novice and and I have to modify this code on pvector: I would like creating several circles which change differents colors when they touch borders, I tried with lists but any result, i'm doing wrong i guess -_-
thank you for your help and advice:)
this is my code :
in the main sketch :
Objet objet;
void setup() {
size(300, 300);
PVector pos = new PVector(width/2, height/2);
PVector vel = new PVector(0, 0);
PVector acc =new PVector(0, 0);
objet = new Objet(pos, vel, acc, 1.5);
}
void draw() {
background(0);
objet.draw();
PVector gravite = new PVector(0, 0.08);
objet.appliqueForce(gravite);
PVector vent = new PVector ( 0.01, 0);
objet.appliqueForce(vent);
}
in the Class sketch :
class Objet {
PVector pos;
PVector vel;
PVector acc;
float masse;
Objet (PVector p, PVector v, PVector a, float m) {
pos = p.get();
vel = v.get();
acc = a.get();
masse = m;
}
void update() {
border();
vel.add(acc);
pos.add(vel);
acc.mult(0);
}
void draw() {
update();
fill(255);
ellipse (pos.x, pos.y, masse*10, masse*10);// *5 pour le rendre un peu plus grand
}
void border() {
if (pos.x <0) {
pos.x = 0;
vel.x*=-1;
}
if ( pos.x > width) {
pos.x = width;
vel.x*=-1;
}
if (pos.y <0) {
pos.y = 0;
vel.y*=-1;
}
if ( pos.y > height) {
pos.y = height;
vel.y*=-1;
}
}
void appliqueForce(PVector force) {
force.div(masse);
acc.add(force);
}
PVector getGraviteForce (Objet obj) {
PVector direction = PVector.sub(pos, obj.pos);
float distance = direction.mag();
distance = constrain(distance,5.0,25.0);
direction.normalize();
float force = (G * masse * obj.masse)/ (distance*distance);
direction.mult(force);
return direction;
}
}
I am novice and and I have to modify this code on pvector: I would like creating several circles which change differents colors when they touch borders, I tried with lists but any result, i'm doing wrong i guess -_-
thank you for your help and advice:)
this is my code :
in the main sketch :
Objet objet;
void setup() {
size(300, 300);
PVector pos = new PVector(width/2, height/2);
PVector vel = new PVector(0, 0);
PVector acc =new PVector(0, 0);
objet = new Objet(pos, vel, acc, 1.5);
}
void draw() {
background(0);
objet.draw();
PVector gravite = new PVector(0, 0.08);
objet.appliqueForce(gravite);
PVector vent = new PVector ( 0.01, 0);
objet.appliqueForce(vent);
}
in the Class sketch :
class Objet {
PVector pos;
PVector vel;
PVector acc;
float masse;
Objet (PVector p, PVector v, PVector a, float m) {
pos = p.get();
vel = v.get();
acc = a.get();
masse = m;
}
void update() {
border();
vel.add(acc);
pos.add(vel);
acc.mult(0);
}
void draw() {
update();
fill(255);
ellipse (pos.x, pos.y, masse*10, masse*10);// *5 pour le rendre un peu plus grand
}
void border() {
if (pos.x <0) {
pos.x = 0;
vel.x*=-1;
}
if ( pos.x > width) {
pos.x = width;
vel.x*=-1;
}
if (pos.y <0) {
pos.y = 0;
vel.y*=-1;
}
if ( pos.y > height) {
pos.y = height;
vel.y*=-1;
}
}
void appliqueForce(PVector force) {
force.div(masse);
acc.add(force);
}
PVector getGraviteForce (Objet obj) {
PVector direction = PVector.sub(pos, obj.pos);
float distance = direction.mag();
distance = constrain(distance,5.0,25.0);
direction.normalize();
float force = (G * masse * obj.masse)/ (distance*distance);
direction.mult(force);
return direction;
}
}
1