We are about to switch to a new forum software. Until then we have removed the registration on this forum.
class Points{
PVector pos;
PVector vel = new PVector();
PVector acc = new PVector();
Points(float x_, float y_){
pos = new PVector(x_,y_);
}
void applyForce(PVector force){
acc.add(force);
}
void update(){
vel.add(acc);
pos.add(vel);
acc.mult(0);
show();
}
void show(){
strokeWeight(3);
stroke(255);
ellipse(pos.x,pos.y,10,10);
}
}
ArrayList<PVector> charges = new ArrayList<PVector>();
ArrayList<Points> particles = new ArrayList<Points>();
void setup(){
size(500,500);
for(int i = 0; i < 2; i++){
charges.add(new PVector(random(height),random(width),round(random(1))));
}
for(int i = 0; i < 100; i++){
particles.add(new Points(random(height),random(width)));
print(particles.get(i).pos);
}
}
PVector efield(PVector pos){
PVector force = new PVector();
for(PVector charge:charges){
PVector poscopy = pos;
PVector chargecopy = charge;
float strength = charge.z/pow(dist(pos.x,pos.y,charge.x,charge.y),2);
poscopy.sub(chargecopy);
poscopy.normalize();
poscopy.mult(strength);
force.add(poscopy);
}
return force;
}
void draw(){
background(0);
for(Points p:particles){
p.applyForce(efield(p.pos));
p.update();
}
}
This code is only showing points in one corner of the screen, instead of simulating electric field lines like it should be doing.
Answers
Line 30 you swap width and height
Why the 3rd parameter, it’s a 2D sketch?
You only have 2 charges
The third parameter for the charges is for positive or negative charge
I changed my code to show arrows instead of points but now only the first point I place has an effect on the code. It should give all charges the same effect based on the distance but it isn't working.
WHy don’t you init charges in setup () anymore?
I wanted to keep charges empty and have control over where I add them.
mainly, in efield you need to use .copy()