Update added propeller, birds and boxes

This commit is contained in:
Lev
2021-06-10 16:47:45 -05:00
parent 5336f7dfe8
commit 361e49e3a5
3 changed files with 50 additions and 10 deletions

View File

@ -7,8 +7,9 @@
<script src="libraries/p5.sound.min.js" type="text/javascript"></script> <script src="libraries/p5.sound.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript" src="libraries/matter.js"></script> <script language="javascript" type="text/javascript" src="libraries/matter.js"></script>
<script language="javascript" type="text/javascript" src="physics.js"></script>
<script src="sketch.js" type="text/javascript"></script> <script src="sketch.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript" src="physics.js"></script>
<style> body {padding: 0; margin: 0;} canvas {vertical-align: top;} </style> <style> body {padding: 0; margin: 0;} canvas {vertical-align: top;} </style>
</head> </head>

View File

@ -16,13 +16,21 @@ function drawGround(){
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
function setupPropeller(){ function setupPropeller(){
// your code here // your code here
propeller = Bodies.rectangle(150, 480, 200, 15, {isStatic: true, angle: 0});
World.add(engine.world, [propeller]);
} }
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
//updates and draws the propeller //updates and draws the propeller
function drawPropeller(){ function drawPropeller(){
Body.setAngle(propeller, angle);
Body.setAngularVelocity(propeller, angleSpeed);
push(); push();
// your code here fill(255);
//translate(propeller.position.x, propeller.position.y);
//rotate(propeller.angle);
drawVertices(propeller.vertices);
pop(); pop();
angle += angleSpeed;
} }
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
function setupBird(){ function setupBird(){
@ -35,19 +43,47 @@ function setupBird(){
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
function drawBirds(){ function drawBirds(){
push(); push();
//your code here for (let i = 0; i < birds.length; i++)
{
drawVertices(birds[i].vertices);
if(isOffScreen(birds[i]))
{
World.remove(engine.world, birds[i]);
birds.splice(i, 1);
i--;
}
}
pop(); pop();
console.log("# Birds: ", birds.length);
console.log("# World Bodies: ", engine.world.bodies.length);
} }
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
//creates a tower of boxes //creates a tower of boxes
function setupTower(){ function setupTower(){
//you code here boxes = Composites.stack(500, 100, 3, 6, 0, 0,
function(x,y){
let partA = Bodies.rectangle(x, y, 80, 80, {density: 0.001});
colors.push(Math.trunc(random(100, 255)));
return Body.create({parts: [partA]});
});
Composite.add(engine.world, [boxes]);
} }
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
//draws tower of boxes //draws tower of boxes
function drawTower(){ function drawTower(){
push(); push();
//your code here for (let i = 0; i < boxes.bodies.length; i++) {
fill(0, colors[i], 0);
noStroke();
drawVertices(boxes.bodies[i].vertices);
if(isOffScreen(boxes.bodies[i]))
{
World.remove(engine.world, boxes.bodies[i]);
boxes.bodies.splice(i, 1);
colors.splice(i, 1);
i--;
}
}
pop(); pop();
} }
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////

View File

@ -9,16 +9,18 @@ var Body = Matter.Body;
var Constraint = Matter.Constraint; var Constraint = Matter.Constraint;
var Mouse = Matter.Mouse; var Mouse = Matter.Mouse;
var MouseConstraint = Matter.MouseConstraint; var MouseConstraint = Matter.MouseConstraint;
let Composites = Matter.Composites;
let Composite = Matter.Composite;
var engine; var engine;
var propeller; var propeller;
var boxes = []; var boxes = null;
var birds = []; var birds = [];
var colors = []; var colors = [];
var ground; var ground;
var slingshotBird, slingshotConstraint; var slingshotBird, slingshotConstraint;
var angle=0; var angle=0;
var angleSpeed=0; var angleSpeed=0.01;
var canvas; var canvas;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
function setup() { function setup() {
@ -56,10 +58,10 @@ function draw() {
//use arrow keys to control propeller //use arrow keys to control propeller
function keyPressed(){ function keyPressed(){
if (keyCode == LEFT_ARROW){ if (keyCode == LEFT_ARROW){
//your code here angleSpeed += 0.01;
} }
else if (keyCode == RIGHT_ARROW){ else if (keyCode == RIGHT_ARROW){
//your code here angleSpeed -= 0.01;
} }
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -93,7 +95,8 @@ function mouseReleased(){
//tells you if a body is off-screen //tells you if a body is off-screen
function isOffScreen(body){ function isOffScreen(body){
var pos = body.position; var pos = body.position;
return (pos.y > height || pos.x<0 || pos.x>width); return (pos.y > height)
// Modified so bodies don't disappear when they're not completely off the screen yet
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//removes a body from the physics world //removes a body from the physics world