How do I use a color as a boarder to restrict an object's motion along a vector?
in
Programming Questions
•
2 years ago
Is it possible to restrict an object's motion by using a pixel value as a boarder? In other words, if I have a white shape on a black background, can I tell processing something like
if ("pixel value is black") {
velocity.x = -1*velocity.x;
velocity.y = -1*velocity.y;
}
The image below is what I would like to use. I have elements in motion using the following code, but I want to restrict the motion to occur within the swiggly shape (see image).
class Mover2 {
PVector att5;
PVector velocity;
PVector location;
float topSpeed;
float xWidth;
////////////////this is the initialization of variables
Mover2() {
location = new PVector(random(width), random(height));
velocity = new PVector(0,0);
topSpeed = 30;
}
void update() {
PVector att5att = new PVector (689, 64);
PVector att5Dir = PVector.sub(att5att, location);
att5Dir.mult(.0001);
att5 = att5Dir;
velocity.add(att5);
velocity.limit(topSpeed);
location.add(velocity);
}
void display() {
noStroke();
fill(0);
ellipse(location.x, location.y, 2,2);
}
void checkEdges() {
//-----this is not what I want to happen
if(location.x > width) {
velocity.x = -1*velocity.x;
}
if(location.x < 0) {
velocity.x = -1*velocity.x;
}
if(location.y > height) {
velocity.y = -1*velocity.y;
}
if(location.y < 0) {
velocity.y = -1*velocity.y;
}
}
}
PImage img;
Mover2[] movers = new Mover2 [1000];
void setup() {
size (1600, 100);
img = loadImage("large elev outlines.png");
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover2();
}
}
void draw () {
noStroke();
fill(0,0,0);
image (img, 0,0);
for (int i = 0; i < movers.length; i++) {
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
}
if ("pixel value is black") {
velocity.x = -1*velocity.x;
velocity.y = -1*velocity.y;
}
The image below is what I would like to use. I have elements in motion using the following code, but I want to restrict the motion to occur within the swiggly shape (see image).
class Mover2 {
PVector att5;
PVector velocity;
PVector location;
float topSpeed;
float xWidth;
////////////////this is the initialization of variables
Mover2() {
location = new PVector(random(width), random(height));
velocity = new PVector(0,0);
topSpeed = 30;
}
void update() {
PVector att5att = new PVector (689, 64);
PVector att5Dir = PVector.sub(att5att, location);
att5Dir.mult(.0001);
att5 = att5Dir;
velocity.add(att5);
velocity.limit(topSpeed);
location.add(velocity);
}
void display() {
noStroke();
fill(0);
ellipse(location.x, location.y, 2,2);
}
void checkEdges() {
//-----this is not what I want to happen
if(location.x > width) {
velocity.x = -1*velocity.x;
}
if(location.x < 0) {
velocity.x = -1*velocity.x;
}
if(location.y > height) {
velocity.y = -1*velocity.y;
}
if(location.y < 0) {
velocity.y = -1*velocity.y;
}
}
}
PImage img;
Mover2[] movers = new Mover2 [1000];
void setup() {
size (1600, 100);
img = loadImage("large elev outlines.png");
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover2();
}
}
void draw () {
noStroke();
fill(0,0,0);
image (img, 0,0);
for (int i = 0; i < movers.length; i++) {
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
}
1
