xxxxxxxxxx
108
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Reference to physics world
var physics;
// Our "Chain" object
var chain;
// Our helicopter
let heli;
let boxy;
let isSteering = false;
let roll = 0;
let torque = 0;
let hook = false;
function setup() {
createCanvas(600,400);
angleMode(DEGREES);
// Initialize the physics
physics=new VerletPhysics2D();
physics.addBehavior(new GravityBehavior(new Vec2D(0,0.5)));
// Set the world's bounding box
physics.setWorldBounds(new Rect(0,0,width,height));
heli = new Helicopter(1,random(width), height-25);
boxy = new Cargo(random(width), random(height));
// Initialize the chain
// chain = new Chain(180, 20, 16, 0.2);
}
function draw() {
// Update the physics world
physics.update();
background(220);
// Update physics
physics.update();
// Update chain's tail according to mouse position
// chain.updateHead(mouseX, mouseY);
// Display chain
// let d = dist(heli.chain.head.x,heli.chain.head.y,heli.chain.tail.x,heli.chain.tail.y);
// if(d < 100 && (heli.chain.tail.y - heli.chain.head.y) < 100){
// hook = true;
// }
// Helicopter and Chain
heli.chain.display();
heli.chain.tail.displayTail();
heli.update();
heli.display(isSteering);
gravity = createVector(0, 0.01);
heli.applyForce(gravity);
let collective = createVector(roll, -torque);
heli.applyForce(collective);
if (mouseIsPressed) {
let wind = new Vec2D(10, 0);
heli.chain.tail.addForce(wind);
}
// if (keyIsDown(LEFT_ARROW)) {
// }
//Cargo
// boxy.display();
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;
}
}
}
// function mousePressed() {
// // Check to see if we're grabbing the chain
// chain.contains(mouseX, mouseY);
// }
// function mouseReleased() {
// // Release the chain
// chain.release();
// }
// function keyReleased(){
// isSteering = false;
// }