xxxxxxxxxx
48
let pos;
let v;
let acc;
const q = -1.6;
const B0 = 10;
const a = 2;
const frames = 60;
const dt = 1 / frames;
let m;
function setup() {
createCanvas(400, 400);
frameRate(frames);
pos = createVector(-2*a, 0, 0);
v = createVector(3.2, 0, 0);
acc = createVector(0, 0, 0);
m = 10;
}
function draw() {
translate(width/2, height / 2);
background(220);
acc = getAcc();
v.add(p5.Vector.mult(acc,dt));
pos.add(p5.Vector.mult(v,dt));
line(0, -height/2, 0, height/2);
line(-width/2, 0, width/2, 0);
line(-a * 30, -height/2, -a*30, height/2);
line(a * 30, -height/2, a*30, height/2);
circle(pos.x*30, pos.y*30, 10);
}
function getB() {
if (abs(pos.x) > a) {
return 0
} else if (pos.x < 0) {
return B0
} else {
return -B0
}
}
function getAcc() {
c = q/m
B = getB()
return createVector(v.y * B,-v.x * B,0).mult(c)
}