# Need help with code! Collision is not working. I use java on processing 3.3.1

Discussion in 'Java' started by newbie73, Dec 9, 2019.

1. ### newbie73

Joined:
Dec 9, 2019
Messages:
6
0
The code doesn't work, when I launch the code the balls are shown but they do not bounce of the side of the screen. I would appreciate it if someone could also help me create a code for each balls colliding with each other e.g when the balls hit each other they bounce off each other.

float [] x={0,100,400,0,300,50,800};
float [] y={100,200,500,50,0,30,600};
float [] dx={(3),(4),(6),(8),(10), (2), (5)};
float [] dy={ (5), (5), (3), (5), (1), (6), (8)};
float [] xx={3,4,6,8,10,2,5};
float [] yy={5,5,3,5,1,6,8};
float ballSize;

void setup() {
size(1920, 1080);
for (int i=0; i<=6; i++) {
xx=xx*2;
yy=yy*2;
}
}

void draw() {
noStroke();
ballSize= 60;
fill(0, 10);
rect(0,0, width, height);
fill(255);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);

for(int i=0; i<=6; i++) {
x+=(dx);
y+=(dy);
xx+=0.0001;
yy+=0.0001;
}
for (int i=0; i<=6; i++) {
if(x>=width-50) {
dx=(-xx);
}
}
for (int i=0; i<6; i++) {
if (y>=height-50) {
dy=(yy);
}
}
}

newbie73, Dec 9, 2019

2. ### rogerlacante

Joined:
Dec 9, 2019
Messages:
1
0
float [] x={0,100,460,0,300,70,800};
float [] y={100,200,300,50,0,80,600};
float [] dx={(3),(4),(6),(8),(10), (2), (5)};
float [] dy={ (5), (5), (3), (5), (1), (6), (8)};
float [] xx={3,4,6,8,10,2,5};
float [] yy={5,5,3,5,1,6,8};
float ballSize;

void setup() {
size(1920, 1080);
for (int i=0; i<=6; i++) {
xx=xy*6;
yy=xy*6;
}
}

void draw() {
noStroke();
ballSize= 60;
fill(0, 10);
rect(0,0, width, height);
fill(255);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);
ellipse(x,y, ballSize, ballSize);

for(int i=0; i<=6; i++) {
x+=(dx);
y+=(dy);
xx+=0.0001;
yy+=0.0001;
}
for (int i=0; i<=6; i++) {
if(x>=width-50) {
dx=(-xx);
}
}
for (int i=0; i<6; i++) {
if (y>=height-50) {
dy=(yy);
}
}

The float of the variable x was in collision with the xy intercept.

It should be fixed now. }

rogerlacante, Dec 9, 2019

3. Joined:
Dec 9, 2019
Messages:
6