Code a Week #4 Wittgenstein, my idol.

So this week completes somewhat my dive into processing, at least for a a little while. We salvaged working copies of Canon X-07, which understands the programming language of my childhood: BASIC! WUHUU!!!

Canon X-07

So I am going to be experimenting with that and sharing some results. I am also teaching two eTextile courses in the coming months and using Adafruits Gemma’s and Flora’s in then so some ATTiny code is following also. But here is Wittgenstein and vectors with force applied into them. The grey dot should be draggable, not sure if it works in the p5.js environment though.

show code

class Attractor {
float mass;
float G;
PVector location;
boolean dragging = false;
boolean rollover = false;
PVector dragOffset;
Attractor() {
location = new PVector(width/2,height/2);
mass = 20;
G = (second()/30);
dragOffset = new PVector(0.0,0.0);
}

PVector attract(Mover m) {
PVector force = PVector.sub(location,m.location);
float d = force.mag();
d = constrain(d,5.0,25.0);
force.normalize();
float strength = (G * mass * m.mass) / (d * d);
force.mult(strength);
return force;
}

void display() {
ellipseMode(CENTER);
noStroke();
if (dragging) fill (second()*4.25,50);
if (dragging) text("Knowledge is in the end based on acknowledgement.",350,mouseY+6);
else if (rollover) fill(240,40,40,200);
else fill(175,200);
ellipse(location.x,location.y,mass*2,mass*2);
}

void clicked(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
dragging = true;

dragOffset.x = location.x-mx;
dragOffset.y = location.y-my;

}
}

void hover(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
rollover = true;
text("One often makes a remark and only later sees how true it is.",mouseX-80,mouseY+50);

}
else {
rollover = false;
fill(255);
text("One often makes a remark and only later sees how true it is.",100,240);
}
}

void stopDragging() {
dragging = false;
fill(255);
text("Knowledge is in the end based on acknowledgement.",150,150);
}

void drag() {
if (dragging) {
location.x = mouseX + dragOffset.x;
location.y = mouseY + dragOffset.y;
}
}

}

class Mover {

PVector location;
PVector velocity;
PVector acceleration;
float mass;

Mover(float m, float x, float y) {
mass = m;
location = new PVector(x, y);
velocity = new PVector(1, 0);
acceleration = new PVector(0, 0);
}

void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}

void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}

void display() {
noStroke();
fill(10,180,140,100);
ellipse(location.x, location.y, mass*25, mass*25);
}
}

Mover[] movers = new Mover[10];

Attractor a;

void setup() {
PFont myFont;
myFont = createFont("Georgia", 12);
textFont(myFont);
size(640, 360);
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(0.1, 2), random(width), random(height));
}
a = new Attractor();
}

void draw() {
background(255);

a.display();
a.drag();
a.hover(mouseX, mouseY);

for (int i = 0; i < movers.length; i++) {
PVector force = a.attract(movers[i]);
movers[i].applyForce(force);

movers[i].update();
movers[i].display();
}
}

void mousePressed() {
a.clicked(mouseX, mouseY);
}

void mouseReleased() {
a.stopDragging();
}

Comments are closed.