Skip to content

Commit

Permalink
Merge pull request #7 from Andemanden/ActualRotator
Browse files Browse the repository at this point in the history
The actual rotator. Almost only additions
  • Loading branch information
Andemanden committed Nov 26, 2021
2 parents 2192674 + 7f868df commit 20f1f85
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
29 changes: 29 additions & 0 deletions main/bodies.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,33 @@ class Ball extends Circle {

this.setcategory(DEFAULT_COLLISION | PISTON_MECHANISM_COLLISION);
}
}

class Rectangle extends Physical {
constructor(_x, _y, _w, _h, matteroptions = {}, _isVisible = true) {
super();
this.visible = _isVisible;
this.w = _w; this.h = _h;
this.body = Bodies.rectangle(_x, _y, this.w, this.h, matteroptions);
World.add(world, this.body);
}
getcamsize(cam) {
return {w: this.w*cam.proportion,
h: this.h*cam.proportion};
}
show() {
push();
var campos = this.getcampos(camera);
var camsize = this.getcamsize(camera);
var camangle = this.getcamangle(camera);
translate(campos.x, campos.y);
rotate(camangle);
fill(255);
rectMode(CENTER);
rect(0, 0, camsize.w, camsize.h);
fill(0);
circle(0,0,2);
pop();
}

}
37 changes: 36 additions & 1 deletion main/globals.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
const NO_COLLISION = 0b0000;
const DEFAULT_COLLISION = 0b0001; // for every normal object
const PISTON_MECHANISM_COLLISION = 0b0010; // for collision with the piston.
const PISTON_STOPPLATE_COLLISION = 0b0100;
const PISTON_STOPPLATE_COLLISION = 0b0100;

var keysdown = [];

// key character to char code
function ktocc(key) {
return key.charCodeAt(0);
}

// User input
function mousePressed()
{

}

function keyPressed() {
keysdown.push(keyCode);
}
function keyReleased() {
keysdown.splice(keysdown.indexOf(keyCode));
}

// Character movement
function moves() {
for (key of keysdown) {
switch (key) {
case ktocc("w"):
console.log("w");
//rotator.activate();
break;
default:
console.log("no");

}
}
}
12 changes: 8 additions & 4 deletions main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ var Engine = Matter.Engine,
Events = Matter.Events;

var engine, world, bodies;

var camera;

var testpiston, testball, rotator;
function setup() {
createCanvas(800, 400);

camera = new Camera(width / 2, height/2, width, height);

//testball = new Ball(width/2, height - 200, 30);
//testpiston = new Piston(width/2, height - 50, 100, 100);
//rotator = new Rotator(width/2+40, height/2, 10, 200,0.2);
engine = Engine.create();
world = engine.world;

Events.on(engine, "beforeUpdate", moves);
Runner.run(engine);
}

function draw() {
background(0);
//testball.show();
//testpiston.show();
//rotator.show();
}
21 changes: 21 additions & 0 deletions main/mechanisms/rotator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//This code goes so hard! Feel free to screenshot!
class Rotator {
/*"YOU CAN'T BEAT MEEEHH"*/
constructor(_x,_y,_w,_h,_rv) {
this.x = _x;this.y = _y; this.w = _w; this.h = _h;this.rot=_rv;
this.arm = new Rectangle(_x, _y, _w * 0.9, _h * 0.9,{isStatic: true});
Matter.Body.setAngle(this.arm.body, -1);
this.arm.setcategory(DEFAULT_COLLISION);
//The intend was mouseconstraint. But it could not find the html element
}

activate() {
/*"And rotator runs free-wheeling"*/
Matter.Body.rotate(this.arm.body, this.rot);
}

show() {
this.arm.show();
}
}

0 comments on commit 20f1f85

Please sign in to comment.