#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Tunnel Effect

edited May 2016

Hello, how is it going?

Im working on a tunnel effect to follow my mouse, im using vector math and attraction based on the one Daniel Shiffman uses on Chapter 2 - Forces of Nature of Code book to make them come after the mouse but still isnt working how i want. What i want is for the rectangles to always finish where the mouse is at that moment. Any toughts? Even when my mouse is not moving some of the rectangles behave strange.. no idea what that could be

Here`s the code

** main tab**

``````int x, y;
int k = 0;
int limite = 999;
Retangulo[] retangulos = new Retangulo[limite];
Atrator a;

void setup() {
size(800, 800);
strokeWeight(2);
rectMode(CENTER);
x = width;
y = height;
for (int i = 0; i < limite; i++) {
retangulos[i] = new Retangulo(i);
}
a = new Atrator();
}

void draw() {
background(0);

//get mouseX and mouseY position and make it the point of attraction
a.atualizaPosicao();

//turn rectangles boolean on according to framecount
if (frameCount % 20 == 0) {
k++;
}

//restart (if needed) procces of counting of how many rectangles were drawn
if (k >= limite) k = 0;

//check rectangles boolean and draw if theyre on
for (int i = 0; i < limite; i++) {
PVector forca = a.atrair(retangulos[i]);
retangulos[i].aplicaForca(forca);
retangulos[i].update();
retangulos[i].desenha();
}
}
}
``````

Rectangle Class

``````class Retangulo {

Retangulo(int tag_) {
tag = tag_;
location = new PVector(width/2, height/2);
tamanho = new PVector(width, height);
mass = 1;
aceleracao = new PVector(0, 0);
}

void update() {
aceleracao.mult(0);
}

void desenha() {
//control size when its too small
if ((tamanho.x < 10) || (tamanho.y < 10)) {
println(tag);
location = new PVector(width/2, height/2);
tamanho = new PVector(width, height);
}
//draw if on and make it smaller over time
stroke(255);
strokeWeight(2);
noFill();
rect(location.x, location.y, tamanho.x, tamanho.y);
}
}

void aplicaForca(PVector force) {
//make it not be affected by force when its close to disappear, made it look better
if ((tamanho.x > 50) && (tamanho.y > 50)) {
PVector f = PVector.div(force, mass);
}
}
}
``````

Attractor class

``````// Attraction
// Daniel Shiffman <http://www.shiffman.net>;

// A class for a draggable attractive body in our world

class Atrator {
float mass;    // Mass, tied to size
float G;       // Gravitational Constant
PVector location;   // Location
int controleDistancia;

Atrator() {
location = new PVector(0, 0);
mass = 20;
G = 1;
controleDistancia = 50;
}

PVector atrair(Retangulo r) {
PVector force = PVector.sub(location, r.location);   // Calculate direction of force
float d = force.mag();                              // Distance between objects
d = constrain(d, 5.0, 25.0);                          // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize();                                  // Normalize vector (distance doesn't matter here, we just want this vector for direction)
float strength = (G * mass * r.mass) / (d * d);     // Calculate gravitional force magnitude
force.mult(strength);                               // Get force vector --> magnitude * direction
PVector locationMouse = new PVector (mouseX, mouseY);
PVector locationRetangulo = new PVector (r.location.x, r.location.y);
locationMouse.sub(locationRetangulo);
return force;

//trying to apply more attraction when they are close to make it disapear where the mouse is, didnt work
//if (locationMouse.x < controleDistancia*2 || locationMouse.y < controleDistancia*2) return force.mult(1.02);
//else if (locationMouse.x < controleDistancia || locationMouse.y < controleDistancia) return force.mult(1.06);
//else return force;
}

void atualizaPosicao() {
location.x = mouseX;
location.y = mouseY;
}
}
``````
Tagged: