I've struggling to figure out to problems with a sketch.
1) right now when a small circle hits the outer "container" which is another circle, it will bounce if, which is what I want. But i'm only changing the velocity, which means that they circle bounces right back off and heads in the same directions. I would like to do something more "realistic" and have it bounce of at an angle, but I'm struggling with the match behind this.
2) Sometimes when the small circles collide, they stick together. this especially happens when you click in the same space that a circle is moving through. Each click adds a new circle. How do I get them not to stick together?
I know there are better physics engines for collisions, but I want this to be able to run in javascript using processing.js
If anyone has ideas for improvement would really love to here them!
here is the entire code.
ArrayList colliz = new ArrayList();
boolean showVectors = true;
int xOrigin, yOrigin, diameter, cRadius;
PVector center;
void setup() {
size(500, 500);
smooth();
xOrigin = width/2;
yOrigin = height/2;
diameter = width-100;
cRadius = diameter/2;
center = new PVector(xOrigin, yOrigin);
for (int i = 0; i < 1; i++) {
colliz.add(new Circ(new PVector(random(5), random(-5, 5)), new PVector(width/2, height/2)));
}
}
void draw() {
background(100);
noFill();
stroke(255);
ellipse(250, 250, diameter, diameter);
for (int i = 0; i < colliz.size(); i++) {
Circ test = (Circ) colliz.get(i);
test.go();
for (int j = 0; j < colliz.size(); j++) {
Circ collide = (Circ) colliz.get(j);
test.collideEqualMass(collide);
}
}
}
void mousePressed() {
//showVectors = !showVectors;
colliz.add(new Circ(new PVector(random(5), random(-5, 5)), new PVector(mouseX, mouseY)));
}
class Circ {
PVector loc;
PVector vel;
float bounce = 1.0;
float r = 5;
boolean colliding = false;
boolean isIn = true;
PVector ploc;
Circ(PVector v, PVector l) {
vel = v.get();
loc = l.get();
}
// Main method to operate object
void go() {
update();
borders();
render();
}
// Method to update location
void update() {
ploc = loc;
vel.limit(5);
loc.add(vel);
}
void borders() {
// TEST TO SEE IF A CIRCLE CAN BE USED AS THE BOUNDRY
I'm trying to start a sketch where circles are drawn, always touching.
I'm having trouble with the math. For example, I want a circle in the middle, and each time the program is run another circle is drawn touching the first circle. I don't want to just change the x value or the y value, so i'm thinking i have to specify the angle or something.
I'm trying to write a function for a class that would detect whether the mouse has clicked that object. Below is my code.
i want the fill to decrease by 1 each time that the mouse presses the circle. but I'm getting to many messages when the mouse is clicked because the speed of draw()
I'm trying to have a small circle stay inside a larger circle. For the most part the conde i have below works, but at times the small circle escapes, acts erratically for a few moments, then returns inside the circle.
I've been trying to debug why this is happening and haven't had success yet. I would really appreciate if someone can help out and see my error.
When you run the code, if you click inside the large circle a small circle is drawn.
int radius, diameter;
int count, numCirc, circSize;
int centerX, centerY;
//boolean inside = false;
Circ[] colliz;
void setup(){
size(500, 500);
background(0);
//set up the circular boundary
diameter = width-100;
radius = diameter/2;
centerX = width/2;
centerY = height/2;
circSize = 10;
count = 0;
numCirc = 100;
colliz = new Circ[numCirc];
for (int i = 0; i < numCirc; i++){
colliz[i] = new Circ();
}
//x = width/2;
//y = height/2;
//radius = width/2;
//debug boundary
fill(255);
ellipse(centerX, centerY, diameter, diameter);
}
void draw(){
background(0);
fill(200);
ellipse(centerX, centerY, diameter, diameter);
for(int i = 0; i < count; i++){
colliz[i].move();
colliz[i].inside();
//cycle through the array, call isIn for each index
}
}
void mousePressed(){
//when mouse is pressed start a circ at the mouseX, mouseY
That code below is a sketch where when the mouse is clicked, circles start to move within the bounds of a larger circle. When the circles hit the bounding circle, a change in direction causes them to bounce off the bounding circle.
Right now they are just bouncing back and forth. When they hit the wall I want them to bounce off at an angle.
Method 1, I tried to check whether it was the x or y value that was out of bounds first, to just the x or the y direction. But that caused weird behavior, especially when it was definately the y value that was out of bounds.
if(sq(x - xOrigin)<= sq(cRadius)) {
directionX *= -1;
} else if (sq(y - yOrigin)<= sq(cRadius)) {
directionY *= -1;
}
Maybe the answer is to use Pvector like in the circleCollision example sketch?
If you can help greatly appreciated!
Full Code
int radius, diameter;
int count, numCirc, circSize;
int centerX, centerY;
//boolean inside = false;
Circ[] colliz;
void setup(){
size(500, 500);
background(0);
smooth();
noStroke();
//set up the circular boundary
diameter = width-100;
radius = diameter/2;
centerX = width/2;
centerY = height/2;
circSize = 10;
count = 0;
numCirc = 300;
colliz = new Circ[numCirc];
for (int i = 0; i < numCirc; i++){
colliz[i] = new Circ();
}
//x = width/2;
//y = height/2;
//radius = width/2;
//debug boundary
fill(255);
ellipse(centerX, centerY, diameter, diameter);
}
void draw(){
background(0);
fill(200);
ellipse(centerX, centerY, diameter, diameter);
for(int i = 0; i < count; i++){
colliz[i].move();
colliz[i].inside();
//cycle through the array, call isIn for each index
}
}
void mousePressed(){
//when mouse is pressed start a circ at the mouseX, mouseY
if ((sq(mouseX - centerX) + sq(mouseY - centerY)) <= sq(radius)){
I want to control the fill of a circle using the position of the mouse, as the mouse gets closer to the circle, I want the fill to get closer to white. (all greyscale) In other words as the result of a dist() function gets closer to zero, the fill value will get closer to 255.
Right now I have the opposite happening in my patch, as the mouse gets closer the fill gets closer to 0.
I think that map offers a solution, but i can't get it to work.
Below is the code that i'm working with. Please help if you can.
int gridSize, radius; //gridsize is the diameter, among other things
int numCirc;
int cols, rows;
Circ[] grid;
void setup() {
size(500, 500);
smooth();
noStroke();
//noCursor();
background(0);
gridSize = width/10;
cols = width/gridSize;
rows = height/gridSize;
radius = gridSize/2;
numCirc = rows * cols; //because sq() won't work on an int
//numCirc = 2;
grid = new Circ[numCirc];//crate a new array of type Circ