Waka
November 28, 2020, 9:08am
1
Hi, Can you please help to make the ball hit the rectangle rather than go through the rectangle
float ballx,bally,xspeed=5,yspeed=8;
void setup() {
size(displayWidth,displayHeight);
stroke(255);
strokeWeight(100);
}
void draw() {
background(0);
point(ballx,bally); //draw the ball
if(sideCollision(ballx)) {
xspeed = xspeed * -1; //reverse horizontal speed
}
if(topOrBottomCollision(bally)) {
yspeed = yspeed*-1; //reverse vertical speed
}
ballx = ballx + xspeed;
bally = bally + yspeed;
rect(810,100,300,75);
rect(mouseX,880,300,75);
}
boolean sideCollision(float x) {
if(x<0 || x>width) {
return true;
} else {
return false;
}
}
boolean topOrBottomCollision(float y) {
if(y<0 || y>height) {
return true;
} else {
return false;
}
}
yanze03
November 28, 2020, 9:33am
2
Hi, I used to struggle with collision too but it is not that hard if you make a lot of them.
I don’t know how your program looks or what it is supposed to do and I dont see any other values for the balls appart from ballx and bally. Usually when I make collisions I write an If statement:
if( ballx > 810 || balx < 1110){
xspeed = xspeed*-1
}
I usually try to think about the square it has to hit. The left side value is 810, the right side value is 810+300=1110. The ball has to change direction if it comes between those two values so change direction if: the right side of the ball (ballx) is bigger than the left side value of the box (810) or the other way around, if the left side of the ball (ballx) is smaller than the right side value of the box (1110)
Waka
November 28, 2020, 9:34am
3
thank you, but can you write the whole thing in codes, so I can understand better. As it gives me a error that ballx cannot be resolved to a variable.
yanze03
November 28, 2020, 9:36am
4
That is the code for the X-side of the box repeat this proces for the Y-side of the box and it should work
Waka
November 28, 2020, 9:37am
5
but it isnt working, and where exactly should i put it
yanze03
November 28, 2020, 9:39am
6
Put this in the void draw() , it will only work when the ball hits the square.
Waka
November 28, 2020, 9:39am
7
I aready put it in void draw but it did not work
Waka
November 28, 2020, 9:41am
8
It says ballx cannot be resolved to a variable
yanze03
November 28, 2020, 9:41am
9
I found the problem,
Waka:
rect(mouseX,880,300,75);
if the ball has to hit this rectangle you have to put the mouseX variable in the if statement:
if( ballx > mouseX || balx < mouseX+300){
xspeed = xspeed*-1
}
Waka
November 28, 2020, 9:42am
10
can you do it yourself on your proccessing app and then paste in the whole thing
Waka
November 28, 2020, 9:42am
11
Please, because it is confusing for me
Waka
November 28, 2020, 9:44am
12
Becuase on my codes it still says ballx cannot be resolved to a variable
Waka
November 28, 2020, 9:44am
13
are you there?? Please Help Me
yanze03
November 28, 2020, 9:48am
14
put the code inside the void draw() you made the variable ballx
Waka
November 28, 2020, 9:53am
15
I know look this is what I did, and the reason why I was getting that error was because you wrote the wrong spelling for ball you wrote balx instead of ballx but also after doing that it is the same as before:
…
float ballx,bally,xspeed=5,yspeed=8;
void setup() {
size(displayWidth,displayHeight);
stroke(255);
strokeWeight(100);
}
void draw() {
background(0);
point(ballx,bally); //draw the ball
if(sideCollision(ballx)) {
xspeed = xspeed * -1; //reverse horizontal speed
}
if(topOrBottomCollision(bally)) {
yspeed = yspeed*-1; //reverse vertical speed
}
ballx = ballx + xspeed;
bally = bally + yspeed;
rect(810,100,300,75);
rect(mouseX,880,300,75);
if( ballx > 810 || ballx < 1110){
xspeed = xspeed*-1;
}
}
boolean sideCollision(float x) {
if(x<0 || x>width) {
return true;
} else {
return false;
}
}
boolean topOrBottomCollision(float y) {
if(y<0 || y>height) {
return true;
} else {
return false;
}
}
…
yanze03
November 28, 2020, 10:16am
16
The ball will now bounce between the X sides you have to make the if statement for the Y side as well
quark
November 28, 2020, 11:03am
17
You started this conversation in another thread (your first post) and members of the forum responded to you. Since then you have deleted the discussion title and all your comments making the whole conversation useless to anyone wanting to read it.
This is extremely disrespectful to the members who responded and goes against forum etiquette. Please do not do that again here
Waka
November 28, 2020, 12:19pm
18
@yanze03 , I also did the other side you ballx and bally but it is still not bouncing on it Here are my codes:
float ballx,bally,xspeed=5,yspeed=8;
void setup() {
size(displayWidth,displayHeight);
stroke(255);
strokeWeight(100);
}
void draw() {
background(0);
point(ballx,bally); //draw the ball
if(sideCollision(ballx)) {
xspeed = xspeed * -1; //reverse horizontal speed
}
if(topOrBottomCollision(bally)) {
yspeed = yspeed*-1; //reverse vertical speed
}
ballx = ballx + xspeed;
bally = bally + yspeed;
rect(810,100,300,75);
rect(mouseX,880,300,75);
if( ballx > 810 || ballx < 1110){
xspeed = xspeed*-1;
}
if( bally > 810 || bally < 1110){
xspeed = xspeed*-1;
}
}
boolean sideCollision(float x) {
if(x<0 || x>width) {
return true;
} else {
return false;
}
}
boolean topOrBottomCollision(float y) {
if(y<0 || y>height) {
return true;
} else {
return false;
}
}
Please Help as Soon as Possible
Waka
November 28, 2020, 12:21pm
19
Continuing the discussion from TfGuy44 (Please Help) - This ball goes through the rectangle rather than hitting it or bouncing it :
@yanze03 , I also did the other side you ballx and bally but it is still not bouncing on it Here are my codes:
float ballx,bally,xspeed=5,yspeed=8;
void setup() {
size(displayWidth,displayHeight);
stroke(255);
strokeWeight(100);
}
void draw() {
background(0);
point(ballx,bally); //draw the ball
if(sideCollision(ballx)) {
xspeed = xspeed * -1; //reverse horizontal speed
}
if(topOrBottomCollision(bally)) {
yspeed = yspeed*-1; //reverse vertical speed
}
ballx = ballx + xspeed;
bally = bally + yspeed;
rect(810,100,300,75);
rect(mouseX,880,300,75);
if( ballx > 810 || ballx < 1110){
xspeed = xspeed*-1;
}
if( bally > 810 || bally < 1110){
xspeed = xspeed*-1;
}
}
boolean sideCollision(float x) {
if(x<0 || x>width) {
return true;
} else {
return false;
}
}
boolean topOrBottomCollision(float y) {
if(y<0 || y>height) {
return true;
} else {
return false;
}
}
Please Help as Soon as Possible
yanze03
November 28, 2020, 12:43pm
20
you do realise that you just coppied the code for the X-sides and pasted them for the Y-sides?
I suggest you check out: