xxxxxxxxxx
364
let cnv;
let c;
let img
let bodies = []
let bodiesNumber = 1;
const K = 2;
const friction = 0.5;
let zoom = 1;
let dragPos;
let isDraggingCanvas = false;
let initialMousePos;
// caracteristicas são: minDist, maxDist, strength
// force
// part depu
// part 0 1
//
// depu 0 -5
//
////////
//
// minDist
// part depu
// part 0 diameter
//
// depu diam diam
//
////////
// maxDist
// 0-part 1-depu
// 0 part 0 width
//
// 1 depu 0 100
let forces = [
[0, 0],
[5, -9]
]
let minDist = [
[-1, -1],
[270, 10]
]
let maxDist = [
[0, 0],
[2000, 110]
]
function preload() {
// img = loadImage('/206018.png')
}
function setup() {
cnv = createCanvas(800, 800);
c = color(100, 40, 60);
for (var i = 0; i < bodiesNumber; i++) {
// bodies.push(new Body(random(180,220), random(180,220), 30, c));
bodies.push(new Body(random(-200, 200), random(-200, 200), 30, 'PARTIDO1'));
if (random(1) > 0.7) {
bodies[i].partido = "PARTIDO2"
}
if (random(1) > 0.9) {
bodies[i].partido = "PARTIDO3"
}
if (random(1) > 0.8) {
bodies[i].partido = "PARTIDO4"
}
}
dragPos = createVector(0, 0);
cnv.mouseWheel(handleWheel);
bodies[0].mass = 100;
bodies[0].pos.x = 0;
bodies[0].pos.y = 0;
bodies[0].type = 0;
bodies[0].partido = 'PARTIDO2';
// bodies[1].mass = 100;
// bodies[1].type = 0;
// bodies[1].partido = 'PARTIDO1';
// bodies[2].mass = 100;
// bodies[2].type = 0;
// bodies[2].partido = 'PARTIDO3';
// bodies[3].mass = 100;
// bodies[3].type = 0;
// bodies[3].partido = 'PARTIDO4';
imageMode(CENTER)
// img.resize(50, 0);
}
function draw() {
background(185, 175, 185);
translate(width/2, height/2);
scale(zoom);
translate(dragPos.x , dragPos.y);
for (const b of bodies) {
b.display();
b.update();
}
strokeWeight(4);
stroke(0);
line(-500, 0, 500, 0 )
line(0, -500, 0, 500 )
// noLoop();
console.log(`zoom = ${zoom}\nmouseX = ${mouseX} -- wMouseX = ${screenToWorld(mouseX, mouseY).x}\nmouseY = ${mouseY} -- wMouseY = ${screenToWorld(mouseX, mouseY).y}`)
console.log(`b.Wpos.x = ${bodies[0].pos.x}\nb.Wpos.y = ${bodies[0].pos.y}\n`)
console.log(`dragPos.x = ${dragPos.x}\ndragPos.y = ${dragPos.y}\n\n`)
}
function keyPressed() {
}
function mousePressed() {
initialMousePos = createVector(mouseX, mouseY);
bodySelected = false;
for (let i = 0; i < bodies.length; i++) {
if (bodies[i].isOver()) {
bodies[i].select();
bodySelected = true;
isDraggingCanvas = false;
return;
}
}
if (!bodySelected) {
isDraggingCanvas = true;
}
}
function mouseReleased() {
for (let i = 0; i < bodies.length; i++) {
bodies[i].isSelected = false;
}
isDraggingCanvas = false;
bodySelected = false;
}
function mouseDragged() {
if (isDraggingCanvas) {
let dx = mouseX - initialMousePos.x;
let dy = mouseY - initialMousePos.y;
dragPos.x += dx;
dragPos.y += dy;
initialMousePos.set(mouseX, mouseY);
}
}
function handleWheel() {
const d = event.deltaY/50;
zoom+=d
zoom = constrain(zoom, 0.1, 20);
return false;
}
function screenToWorld(x, y) {
let worldX = (x - dragPos.x - width/2) / zoom;
let worldY = (y - dragPos.y - height/2) / zoom;
return createVector(worldX, worldY);
}
function worldToScreen(x, y) {
let screenX = (x * zoom) + dragPos.x + width/2;
let screenY = (y * zoom) + dragPos.y + height/2;
return createVector(screenX, screenY);
}
class Body {
static isDragging = false;
constructor(x, y, m, partido) {
this.pos = createVector(x, y, 1);
this.mass = m;
this.vel = createVector(0, 0);
// assigned from table used only for loggin
this.minDist = 0;
this.maxDist = 0;
this.type = 1;
this.partido = partido || 'PARTIDO1';
// just for debugging
this.color1 = color(110, 50, 60, 50)
this.color2 = color(210, 150, 160, 50)
this.color3 = color(11, 5, 6, 50)
this.dispColor = this.color1;
this.locked = false;
this.dontMove = false;
this.isSelected = false;
}
select() {
for (const b of bodies) {
b.isSelected = false
}
this.isSelected = true
}
update() {
let dir = createVector(0, 0);
let totalForce = createVector(0, 0);
let acc = createVector(0, 0);
let dist = 0;
for (const body of bodies) {
if (body !== this && this.partido === body.partido) {
this.maxDist = maxDist[this.type][body.type];
this.minDist = minDist[this.type][body.type]
// console.log(body)
//clear for this particle
dir.mult(0);
//copy to keep from messing origina value
dir = body.pos.copy();
// get dir to other
dir.sub(this.pos);
//store distance before normalizing
dist = dir.mag();
//normalize
dir.normalize();
//repel based on dist
if (dist < minDist[this.type][body.type]) {
this.dispColor = this.color2
// don't mess with dir
const force = dir.copy();
// an arbitrary value - in the example we had a table with a unique
// value for each combination. Let's see what i'll need...
force.mult(forces[this.type][body.type] * -3); // negative => repel
//map dist to positive 0~1 and multiply
const mappedD = abs(map(dist, 0, this.minDist, 1, 0)); // <== note 1 e 0 not 0 e 1
force.mult(mappedD);
// a constant to scale down the forces 0.5 in the example
force.mult(K)
//accumulate all the forces of all other particles interacting with this one
totalForce.add(force);
}
if (dist < maxDist[this.type][body.type]) {
this.dispColor = this.color3;
// don't mess with dir
const force = dir.copy();
// an arbitrary value - in the example we had a table with a unique
// value for each combination. Let's see what i'll need...
force.mult(forces[this.type][body.type]);
//map dist to positive 0~1 and multiply
const mappedD = abs(map(dist, 0, maxDist[this.type][body.type], 1, 0)); // <== note 1 e 0 not 0 e 1
force.mult(mappedD);
// a constant to scale down the forces 0.5 in the example
force.mult(K)
//accumulate all the forces of all other particles interacting with this one
totalForce.add(force);
}
}
}
acc.add(totalForce); // if mass totalForce/this.mass
this.vel.add(acc);
this.pos.add(this.vel);
this.vel.mult(friction);
if (this.isSelected) {
let worldMouse = screenToWorld(mouseX, mouseY);
this.pos.x = worldMouse.x;
this.pos.y = worldMouse.y;
}
}
// isOver() {
// const distance = dist(mouseX - width/2, mouseY - height/2, this.pos.x, this.pos.y);
// // console.log('ffffffff', distance < this.mass / 2);
// return distance < this.mass / 2;
// }
isOver() {
let worldMouse = screenToWorld(mouseX, mouseY);
const distance = dist(worldMouse.x, worldMouse.y, this.pos.x, this.pos.y);
return distance < this.mass / 2;
}
isClicked() {
return this.isOver() && mouseIsPressed && !this.dontMove;
}
display() {
push();
noStroke()
fill(255, 30);
circle(this.pos.x, this.pos.y, this.maxDist)
stroke(255, 0, 0, 50);
fill(255, 250, 250, 20);
circle(this.pos.x, this.pos.y, this.minDist)
fill(this.dispColor);
circle(this.pos.x, this.pos.y, this.mass);
// if (this.type === 1) {
// image(img, this.pos.x, this.pos.y);
// textSize(7)
// text('Nome Filhadaputa', this.pos.x -25, this.pos.y +30);
// } else {
// fill(this.dispColor);
// noStroke()
// circle(this.pos.x, this.pos.y, this.mass);
// }
if (this.isOver()) { rect(this.pos.x, this.pos.y, 20, 20) }
pop();
}
checkMouseInteraction() {
if ((this.isOver() && !this.locked && !this.dontMove) || (this.locked && !this.dontMove)) {
this.c = color(170);
} else {
this.c = color(0, 170, 170);
}
if (this.isClicked()) {
this.locked = true;
}
return (this.locked && !this.dontMove)
}
} //<== eof attractor