xxxxxxxxxx
162
class Time {
constructor() {
this.millis_last = millis();
this.sec = this.millis_last;
this.active = true;
}
update() {
if (this.active) {
this.sec += (millis() - this.millis_last) * 1e-3;
}
this.millis_last = millis();
}
}
let time
let index
let cc
let ra
let rb
let rbig
let hit
let offset
function setup() {
createCanvas(600, 600);
noStroke();
time = new Time();
index = -1;
cc = new Circle(50, 50, 25);
ra = new Rectangle(150, 50, 50, 50, 0);
let vertexs = [];
for (let i = 0; i < 5; i++) {
vertexs.push(createVector(0, 25).rotate(2 * PI * i / 5));
}
rb = new Convex(250, 50, vertexs, 0);
rbig = new Rectangle(300, 300, 200, 50, 0);
hit = new Hit();
offset = createVector();
}
function mouseReleased() {
if (mouseButton === LEFT) {
index = (index + 1) % 3;
}
if (mouseButton === CENTER) {
time.active = !time.active;
}
}
function mouseWheel(event) {
let delta = event.delta > 0 ? -1 : 1;
let min_max = [20, 150];
switch (index) {
case 0:
cc.r = constrain(cc.r + delta * 2, min_max[0] * 0.5, min_max[1] * 0.5);
break;
case 1:
ra.set_size(constrain(ra.w + delta * 4, min_max[0], min_max[1]),
constrain(ra.h + delta * 3, min_max[0], min_max[1]));
break;
case 2:
let w = rb.bound_size_local.x * rb.scale_factor;
let w_new = constrain(w + delta * 4, min_max[0], min_max[1]);
rb.set_scale(w_new / rb.bound_size_local.x);
break;
}
}
function move() {
switch (index) {
case 0:
cc.p.x = mouseX;
cc.p.y = mouseY;
break;
case 1:
ra.p.x = mouseX;
ra.p.y = mouseY;
break;
case 2:
rb.p.x = mouseX;
rb.p.y = mouseY;
break;
}
}
function check() {
var p = null;
switch (index) {
case 0:
circle_rectangle(cc, rbig, hit);
p = cc.p.copy();
break;
case 1:
rectangle_rectangle_overlap(ra, rbig, hit);
break;
case 2:
convex_convex(rb, rbig, hit);
p = rb.p.copy();
break;
}
if (hit.hit) {
if (p != null) {
offset = hit.n.copy().mult(hit.distance);
draw_ray(hit.p, offset);
fill(0);
textSize(12);
textAlign(RIGHT);
text(`Hit distance: ${hit.distance.toFixed(2)}`, 590, 590);
}
} else {
offset.x = 0;
offset.y = 0;
}
}
function draw_manual() {
textAlign(LEFT);
fill(0, 0, 0);
text('Left click to switch object', 10, 560);
text('Middle click to toggle rotation', 10, 575);
text('Scroll to change object size', 10, 590);
}
function draw() {
background(240);
frameRate(60);
textSize(12);
draw_manual();
time.update();
move();
cc.show([0, 255, 100, 128], offset.copy().mult(index == 0 ? 1 : 0));
ra.show([255, 0, 100, 128], createVector());
rb.show([100, 0, 255, 128], offset.copy().mult(index == 2 ? 1 : 0));
if (index != 2) {
rb.show_bound();
}
rbig.show(hit.hit ? [255, 150, 0, 100] : [0, 150, 255, 128], createVector());
check();
ra.rad = -time.sec * 0.3;
rb.rad = -time.sec * 0.3;
rbig.rad = time.sec * 0.2;
}
function draw_ray(p, d, cl = 'black', rad = 0) {
stroke(cl);
strokeWeight(1);
d = d.copy().rotate(rad);
line(p.x, p.y, p.x + d.x, p.y + d.y);
noStroke();
}
function draw_normal(a, b, n, cl = 'black') {
draw_ray(createVector((a.x + b.x) * 0.5, (a.y + b.y) * 0.5), n.copy().mult(20), cl);
}
function draw_line(a, b, cl = 'black') {
stroke(cl);
strokeWeight(1);
line(a.x, a.y, b.x, b.y);
noStroke();
}