xxxxxxxxxx
168
/*
The Hoverer:
Press the mouse on the canvas to start (apply wind and gravity)
Then up-arrow to take-off.
up/down arrow - collective (power)
right/left arrows - roll axis (when you roll it affects your lift power)
*/
let heli;
let airDensity = 0.75
let mass = 1;
let torque = 0
let isSteering = false;
let roll = 0;
let xoff = 0.0;
let started = false;
function setup() {
createCanvas(600, 400);
angleMode(DEGREES);
heli = new Helicopter(1);
}
function draw() {
background(255);
//drawing altitudes
fill(230);
noStroke();
rect(0,height-150,width,150);
fill(210);
rect(0,height-300,width,150);
fill(200);
rect(0,0,width,100);
//Gravity
if (started){
gravity = createVector(0,0.01*mass);
heli.applyForce(gravity);
}
// random wind
if (started){
xoff = xoff + 0.005;
let n = map(noise(xoff),0,1,-0.05,0.05);
let wind = createVector(n,0);
heli.applyForce(wind);
xoff = xoff + 0.0001;
fill(0);
textAlign(CENTER);
text ('wind',width/2,30);
rect(width/2,10,n*5000,10);
}
heli.update();
heli.display(isSteering);
//changing the air density according to altitude
if (heli.pos.y > 250){
airDensity = 0.75;
}
if (heli.pos.y < 250 && heli.pos.y > 100){
airDensity = 0.5;
}
if (heli.pos.y < 100){
airDensity = 0.2;
}
//Steering with arrow keys
let collective = createVector(roll,-torque*airDensity*mass);
heli.applyForce(collective);
// print(collective);
if (keyIsPressed === true){
if (keyCode === LEFT_ARROW){
roll -= 0.001;
torque -= 0.0005;
isSteering = true;
heli.rotorAngle -= 1;
} else if (keyCode === RIGHT_ARROW){
roll += 0.001;
torque -= 0.0005;
isSteering = true;
heli.rotorAngle += 1;
} if (keyCode === UP_ARROW){
torque = 0.02;
} else if (keyCode === DOWN_ARROW){
torque -= 0.001;
}
}
// print(isSteering);
//equilibrium force
// if (!isSteering){
// collective.mult(0.99);
// }
}
function mousePressed(){
started = true;
}
class Helicopter {
constructor(mass) {
this.pos = createVector(width/2,height-25);
this.vel = createVector(0,0);
this.acc = createVector(0,0);
this.mass = mass
this.diameter = 50;
this.rotor = 0;
this.rotorAngle = 0;
}
display(steer){
if(steer){
translate(this.pos.x,this.pos.y);
rotate(this.rotorAngle);
translate(-this.pos.x,-this.pos.y);
}
strokeWeight(1);
stroke(0);
fill(255);
ellipse(this.pos.x,this.pos.y, this.diameter, this.diameter);
strokeWeight(this.rotor);
line(this.pos.x-45, this.pos.y-(this.diameter/2), this.pos.x+45, this.pos.y-(this.diameter/2))
this.rotor += 0.5;
if(this.rotor > 3){
this.rotor = 0;
}
if (this.pos.y >= height - this.diameter/2){
this.pos.y = height - this.diameter/2;
this.vel.y = -this.vel.y*0.5;
}
}
applyForce(force) {
this.acc.add(force);
}
update() {
this.vel.mult(0.999);
this.pos.add(this.vel);
this.vel.add(this.acc);
this.acc.mult(0);
}
}