-1

This is from a Khanacademy project - I was wondering why when I try to apply my repulsion force from my buglight to each of the flies why does it return __env__.flies[j].applyForce is not a function.

I had been messing around with this and can't get it to accept that force. Also when I try to make the glow of the buglight within that function change by adding an amount and then removing an amount it looks like it is happening too fast because it is only showing the maximum glow amount.

CODE:

// I created a light over where the mouse is and the flies (random start poisitions) are drawn toward the light, but I included some randomness to their movements to add some variation to their paths, the flies are repelled from the buglight using a repulsion force:

var Mover = function() {
    this.position = new PVector (random(width),random(height));
    this.velocity = new PVector (0,0);
    this.acceleration = new PVector (0,0);
    this.mass = 1;
};

Mover.prototype.update = function () {
    var mouse = new PVector(mouseX,mouseY); 
    var dist = PVector.sub(mouse, this.position);
    var randomPush = new PVector(random(-1,1),random(-1,1));
    dist.normalize();
    dist.mult(0.4);
    this.acceleration = dist;
    this.acceleration.add(randomPush);
    this.velocity.add(this.acceleration);
    this.velocity.limit(9);
    this.position.add(this.velocity);
};

Mover.prototype.display = function () {
    strokeWeight(7);
    stroke(0, 0, 0);
    point(this.position.x,this.position.y);
    strokeWeight(5);
    point(this.position.x - 3,this.position.y -3);
    point(this.position.x + 3,this.position.y -3);
};

var Buglight = function (){
    this.position = new PVector (random(width-50),random(height-80));
    this.velocity = new PVector (0,0);
    this.acceleration = new PVector (0,0);
    this.glow = 70;
    this.mass = 20;
    this.G = 1;
};

Buglight.prototype.update = function() {
    noStroke();
    fill(39, 131, 207,90);
    ellipse(this.position.x+20,this.position.y+35,this.glow,this.glow);
};

Buglight.prototype.repulsion = function(fly) {
    var force = PVector.sub(this.position,fly.position);
    var distance = force.mag();
    distance = constrain(distance, 4.0, 20.0);
    force.normalize();
    var strength = -1 *(this.G * this.mass * fly.mass) / (distance * distance);
    force.mult(strength);
    return force;

};

Buglight.prototype.display = function() {
    noStroke();
    fill(5, 170, 252);
    rect(this.position.x+15,this.position.y,10,60);
    fill(0, 0, 0);
    noStroke();
    rect(this.position.x-10,this.position.y+60,60,10,10);
    rect(this.position.x,this.position.y,10,60);
    rect(this.position.x+30,this.position.y,10,60    );
    quad(this.position.x,this.position.y,
    this.position.x-10,this.position.y+20,
    this.position.x+50,this.position.y+20,
    this.position.x+40,this.position.y);

};

Buglight.prototype.checkEdges = function () {

};

var flies = [];
for (var i = 0; i < 4; i++){
    flies[i] = new Mover();
}
var buglight = new Buglight();
var fly = new Mover();

draw = function() {
    background(71, 71, 71);
    strokeWeight(56);
    stroke(219, 247, 8,100);
    fill(255, 238, 0);
    ellipse(mouseX,mouseY,20,20);
    buglight.update();
    buglight.display();

    for (var j = 0; j < flies.length; j++) {
        var blueForce = buglight.repulsion(flies[j]);

        flies[j].applyForce(blueForce); 
        flies[j].update();

        flies[j].display(); 
    }
};
grizzthedj
  • 7,131
  • 16
  • 42
  • 62
jj1993
  • 1
  • 2

1 Answers1

0

There is no applyForce function on your Mover class. Something is missing I feel.

AaronHolland
  • 1,595
  • 1
  • 16
  • 32