Hi,
I have always been bad with Arrays, maybe someone can help me out.
I am trying to create an interactive Processing sketch, where I send OSC messages into Processing, which I have worked out.
I took one of the examples from the vectors chapter of the Nature of Code book and I am altering it /building upon it.
When I run my sketch you can see a bunch of boxes, which are part of an Array, randomly appear on the screen and start floating off screen. What I want to happen is that when a variable, say “ramp1.” = 1, 1 box will be randomly generated and float off screen, when ramp1 = 2, 2 boxes will float off screen.
Right now the boxes are generated from this:
for (int i = 0; i < movers.length; i++) {
movers[i].update();
movers[i].display();
Which creates all the boxes in the array. If I take my incoming OSC value, ramp1, and convert it to an int, when I first run the sketch, it will appear, float offscreen, and I’m wondering the best way to get it back to the screen to float off again.
i’m doing this to get the box to initially float off screen?
int r1 = int(ramp1);
movers[r1].update();
movers[r1].display();
What would be a good way to reset a box back onto the screen, and control the number of boxes showing?
I realize this is kind of a basic question, any help is appreciated.
tHank you and I put my code + one tab below.
Mover[] movers = new Mover[100];
float ramp1;
import oscP5.*;
OscP5 oscP5;
void setup() {
size(800,800);
smooth();
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover();
}
oscP5 = new OscP5(this, 12000);
}
void oscEvent(OscMessage theOscMessage) {
float value = theOscMessage.get(0).floatValue();
if (theOscMessage.checkAddrPattern("/ramp1")) {
if (value > -1000) {
ramp1 = value;
} else {
ramp1 = 0.0;
}
}
}
void draw() {
background(255);
strokeWeight(2);
stroke(0);
fill(50,50,180);
ellipse(width/2, height/2, 500,500);
println(ramp1);
for (int i = 0; i < movers.length; i++) {
movers[i].update();
movers[i].display();
// need to figure out how to show/hide these
}
}
next tab for Object
class Mover {
// The Mover tracks location, velocity, and acceleration
PVector location;
PVector velocity;
PVector acceleration;
// The Mover's maximum speed
float topspeed;
float xrange;
float yrange;
float destx, desty;
Mover() {
xrange = random(width);
if ( xrange > 300 && xrange < 500 ){
yrange = random(height);
}
else { yrange = random(100)+height/2-50;
}
// Start in the center
location = new PVector(xrange,yrange);
velocity = new PVector(0,0);
topspeed = 5;
}
void update() {
if (xrange < width/2-250){
destx = 0-200;
desty = height/2;
}
if (xrange > width/2-250 && xrange < width/2+250 && yrange > height/2-250 && yrange < height/2 ){
destx = width/2;
desty =0-200;
}
if (xrange > width/2-250 && xrange < width/2+250 && yrange > height/2 && yrange < height/2+250 ){
destx = width/2;
desty = height+200;
}
if (xrange > width/2+250){
destx = width+200;
desty = height/2;
}
if (yrange < height/2-250){
desty = 0-200;
destx = width/2;
}
// if (yrange > height/2-250 && yrange < height/2+250){
// desty = height/2;
// }
if (yrange > height/2+250){
desty = height + 200;
destx = width/2;
}
// Compute a vector that points from location to mouse
PVector dest = new PVector(destx,desty);
PVector acceleration = PVector.sub(dest,location);
// Set magnitude of acceleration
acceleration.setMag(0.2);
acceleration.normalize();
acceleration.mult(0.2);
// Velocity changes according to acceleration
velocity.add(acceleration);
// Limit the velocity by topspeed
velocity.limit(topspeed);
// Location changes by velocity
location.add(velocity);
}
void display() {
rectMode(CENTER);
stroke(0);
strokeWeight(2);
//fill(127,200);
rect(location.x,location.y,48,48);
if (location.x > width/2-250 && location.x < width/2+250 && location.y > height/2-250 && location.y < height/2+250){
fill(255,200);
}
else { fill(100, 200,180,100);
}
}
}