Как создать программу, симулирующую шар, на который действует ветер в выбранную случайно сторону, гравитация, а стенки и пол отталкивают объект?
Пишу программу в p5.js, где хочу создать шар, на который действует ветер в выбранную случайно сторону, гравитация, а стенки и пол отталкивают объект. Писал, используя векторы и второй закон Ньютона. Ошибок нет, но ничего не работает. Ссылка на видео с идеей
class Mover {
constructor() {
this.x = 50;
this.y = 50;
this.position = createVector(this.x, this.y);
this.velocity = createVector(width, height);
this.acceleration = createVector(width, height);
this.mass = 2;
this.f = createVector(1, 1)
this.wind = createVector(1, 0);
this.gravity = createVector(0, 1);
}
applyForce() {
this.f.mult(this.mass, this.acceleration);
this.acceleration.add(this.f / this.mass);
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
// this.acceleration.mult;
}
display() {
stroke(0);
strokeWeight(2);
fill(127);
ellipse(width - this.x, height - this.y, 48, 48);
}
checkEdges() {
if (this.position.x > width) {
this.position.x = width;
this.velocity.x *= -1;
} else if (this.position.x < 0) {
this.velocity.x *= -1;
this.position.x = 0;
}
if (this.position.y > height) {
this.velocity.y *= -1;
this.position.y = height;
}
}
}
function setup() {
createCanvas(640, 360);
m = new Mover();
}
function draw() {
background(255);
m.applyForce(m.wind);
m.applyForce(m.gravity);
m.update();
m.display();
m.checkEdges();
}