bounce off at an angle
in
Programming Questions
•
2 years ago
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)){
colliz[count].start(mouseX, mouseY, circSize, radius);
count++;
}
}
class Circ{
float x, y; //hold the current x and y for each circ object
int mX, mY;
int xOrigin = width/2;
int yOrigin = height/2;
//cRadius = bounding area, cDiameter is the diameter of the points
int cDiameter, cRadius;
//these variable control the move
int directionX = 1;
int directionY = 1;
float speedX = random(-3,3);
float speedY = random(-3,3);
boolean isIn = true;
//boolean isOn = false; //not needed?
void start(int startX, int startY, int dia, int bounds){
x = startX;
y = startY;
cRadius = bounds;
cDiameter = dia;
fill(255);
ellipse(x, y, cDiameter, cDiameter);
move();
}
void inside(){
//x = sx;
//y = sy;
if ((sq(x - xOrigin) + sq(y - yOrigin)) <= sq(cRadius)){
isIn = true;
} else {
isIn = false;
}
//println(isIn);
//return isIn;
}
void directionShift(){
directionX *= -1;
directionY *= -1;
/*
if(abs(x-xOrigin) <= cRadius) {
directionX *= -1;
} else if (abs(y-yOrigin) <= cRadius) {
directionY *= -1;
}
*/
}
void move(){
inside();
if(isIn == true) {
x = x + (speedX * directionX);
y = y + (speedY * directionY);
fill(255);
ellipse(x, y, cDiameter, cDiameter);
} else if(isIn == false){
directionShift();
x = x + (speedX * directionX);
y = y + (speedY * directionY);
fill(255);
ellipse(x, y, cDiameter, cDiameter);
}
}
//write a function to check for collisions,
//call directionShift when a collision happens
}
2