please format code with </> button * homework policy * asking questions
hello! I am a novice processing programmer living in Korea.
I used Google Translator, so I hope you understand even if the words are strange.
The problem I have is that the balls don’t bounce each other.
I made the ball move with the keyboard and mouse.
and I used PVector to create a program where a ball bounces off a wall.
But I can’t make it physically bounce when the balls hit each other.
this is my code.
Please help me!
PVector location; // Location of shape1
PVector location2; // Location of shape2
PVector location3; // Location of shape3
PVector velocity; // Velocity of shape1
PVector velocity2; // Velocity of shape2
PVector velocity3; // Velocity of shape3
PVector mouse;
int tumx=1,tumy=1,tumx2=1,tumy2=1,tumx3=1,tumy3;
int checkx=1, checky=1, yee=1, check2x=1, check2y=1, check3x=1, check3y=1;
void setup() {
size(1200,600);
location = new PVector(300,300);
location2 = new PVector(600,300);
location3 = new PVector(900,300);
velocity = new PVector(0,0);
velocity2 = new PVector(0,0);
velocity3 = new PVector(0,0);
mouse = new PVector(mouseX,mouseY);
}
void draw() {
background(150);
ellipse(mouseX,mouseY,20,20);
//first ball
location.add(velocity);
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}
if ((location.y > height) || (location.y < 0)) {
velocity.y = velocity.y * -1;
}
//second ball
location2.add(velocity2);
if ((location2.x > width) || (location2.x < 0)) {
velocity2.x = velocity2.x * -1;
}
if ((location2.y > height) || (location2.y < 0)) {
velocity2.y = velocity2.y * -1;
}
//third ball
location3.add(velocity3);
if ((location3.x > width) || (location3.x < 0)) {
velocity3.x = velocity3.x * -1;
}
if ((location3.y > height) || (location3.y < 0)) {
velocity3.y = velocity3.y * -1;
}
//start the first ball
if(keyPressed)
{
if(yee==1)
{
velocity.x=round((location.x-mouseX)/50);
velocity.y=round((location.y-mouseY)/50);
checkx=0;
checky=0;
yee=0;
}
}
// if the ball collide
//if((location2.x-location.x)*(location2.x-location.x)+(location2.y-location.y)*(location2.y-location.y)<=9216.5 && 9215.5<=(location2.x-location.x)*(location2.x-location.x)+(location2.y-location.y)*(location2.y-location.y))
if(location.dist(location2)<2304)
{
}
if(location.dist(location2)<2304)
{
}
if(location.dist(location2)<2304)
{
}
//make the ball slower
ball();
//draw a ball
stroke(0);
strokeWeight(2);
fill(255);
ellipse(location.x,location.y,48,48);
fill(225,0,0);
ellipse(location2.x,location2.y,48,48);
fill(225,225,0);
ellipse(location3.x,location3.y,48,48);
}
void ball()
{
if(checkx==0 || checky==0 || check2x==0 || check2y==0 || check3x==0 || check3y==0)
{
//ball1
velocity.x=(velocity.x)*0.995;
text("X : "+nfc(velocity.x,1), 100, 100);
if(-0.1<velocity.x && velocity.x<0.1)
{
velocity.x=0;
checkx=1;
if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
{
yee=1;
}
}
velocity.y=(velocity.y)*0.995;
text("Y : "+nfc(velocity.x,1), 100, 130);
if(-0.1<velocity.y && velocity.y<0.1)
{
velocity.y=0;
checky=1;
if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
{
yee=1;
}
}
//ball2
velocity2.x=(velocity2.x)*0.995;
text("X : "+nfc(velocity2.x,1), 300, 100);
if(-0.1<velocity2.x && velocity2.x<0.1)
{
velocity2.x=0;
check2x=1;
if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
{
yee=1;
}
}
velocity2.y=velocity2.y*0.995;
text("Y : "+nfc(velocity2.x,1), 100, 130);
if(-0.1<velocity2.y && velocity2.y<0.1)
{
velocity2.y=0;
check2y=1;
if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
{
yee=1;
}
}
//ball3
velocity3.x=velocity3.x*0.995;
text("X : "+nfc(velocity3.x,1), 600, 100);
if(-0.1<velocity3.x && velocity3.x<0.1)
{
velocity3.x=0;
check3x=1;
if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
{
yee=1;
}
}
velocity3.y=velocity3.y*0.995;
text("Y : "+nfc(velocity3.x,1), 600, 130);
if(-0.1<velocity3.y && velocity3.y<0.1)
{
velocity3.y=0;
check3y=1;
if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
{
yee=1;
}
}
}
}