xxxxxxxxxx
var Engine = Matter.Engine,
Render = Matter.Render,
World = Matter.World,
Bodies = Matter.Bodies;
var engine
var box1
var world
var ground
var xspeed=2
var yspeed=1
var x=0
function setup() {
frameRate(60)
createCanvas(windowWidth, windowHeight);
engine = Engine.create();
world=engine.world
Engine.run(engine);
box1 = Bodies.rectangle(200, 100, 80, 80);
ground = Bodies.rectangle(width/2, height-40, width, 80,{ isStatic: true });
World.add(world, box1);
World.add(world, ground);
rectMode(CENTER)
// hits.push(new Hit(0,height/2,40,height))
// hits.push(new Hit(width,height/2,40,height))
colorMode(HSB,255)
}
function draw() {
background(133, 176, 255)
Engine.update(engine, 1000/(frameRate()*2));
push()
fill(82, 250, 223)
rect(width/2, height-40, width, 80)
pop()
for (let i=0; i< boxs.length;i++){
boxs[i].move();}
for (let i=0; i< hits.length;i++){
hits[i].move();}
//boxs.push(new Box())
rect(box1.position.x,box1.position.y,80,80)
if(frameCount%20==0){
mousePressed()}
}
function mousePressed(){
boxs.push(new Box(x,0,random(10,60),random(10,60)))
x+=60
if(x>width){x=0}
}
var boxs=[]
var hits=[]
// boxs.push(new Box(x,y))
function Box (xx,yy,ww,hh) {
this.x=xx
this.y=yy
this.w=ww
this.h=hh
this.f=random(255)
this.body=Bodies.rectangle(xx, yy, ww, hh);
World.add(world, this.body);
this.move=function(){
// Matter.Body.setVelocity( this.body, {x: xspeed, y: yspeed});
var pos=this.body.position
var angle=this.body.angle
push()
translate(pos.x,pos.y)
fill(this.f,999,999)
rotate(angle)
rect(0,0,this.w,this.h)
pop()
}}
function Hit (xx,yy,ww,hh) {
this.x=xx
this.y=yy
this.w=ww
this.h=hh
this.body=Bodies.rectangle(xx, yy, ww, hh,{ isStatic: true });
World.add(world, this.body);
this.move=function(){
var pos=this.body.position
var angle=this.body.angle
push()
translate(pos.x,pos.y)
rotate(angle)
//rect(0,0,this.w,this.h)
pop()
}}