of what exactly I should do, I want it to hit both rectangles from any side
in my original reply I wrote:
if( ballx > 810 || balx < 1110){
xspeed = xspeed*-1
}
the program will check if the ballX is either bigger than 810 or ballx is smaller than 1110
what you want is that it checks that the ball is between those coordinates so it should be checking if ballX is bigger than 810 AND ballx is smaller than 1110
in code:
if( ballx > 810 && balx < 1110){
xspeed = xspeed*-1
}
I tried this out
if( ballx > 810 && ballx < 1110){
xspeed = xspeed*-1;
}
if( bally > mouseX+300 || bally < 880){
xspeed = xspeed*-1;
}
}
but it is still, not working you can paste this in processing and see what the problem is
add the Y axis so it is more precise:
if( ballx > 810 && ballx < 1110 && bally < 400){
xspeed = xspeed*-1;
}
edit sorry it should be 175 instead of 100
float ballx,bally,xspeed=5,yspeed=8;
void setup() {
fullScreen();
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 the ball hits the underside of the top rectangle
if(ballx > 810 && ballx < 1110 && bally < 175){
yspeed = yspeed*-1;
}
//if the ball hits the left side of the rectangle
if(ballx >= 810 && bally >100 && bally < 175){
xspeed = xspeed*-1;
}
//if the ball hits the right side of the rectangle
if(ballx <= 1110 && bally >100 && bally < 175){
xspeed = xspeed*-1;
}
//if the ball hits the top side of the bottem rectangle
if(ballx > mouseX && ballx < mouseX+300 && bally > 880){
yspeed = yspeed*-1;
}
//if the ballhits the side of the bottem rectangle
if(ballx >= mouseX && bally > 880 && bally < 955){
xspeed = xspeed*-1;
}
//if the ball hits the right side of the bottem rectangle
if(ballx <= mouseX+300 && bally > 880 && bally < 955){
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;
}
}
this should be your code.
The reason thatit is not working is because of:
strokeWeight(100);
the positions will be off⌠use ellipse() instead of point()
Hello, Thank you for supporting me so far, but can you do the exact same thing that you did in the last message but change it from a point to circle(Eclipse), because I tried doing it but then the variables (ballx and bally) got messed up.
Thank You and Please Help as Soon as Possible
and Sorry again for troubling you so much
and please also try it on processing before pasting it in here
Please reply as Soon as Possible
Hi TfGuy44!!!
How about I reply instead?
Youâve called for my help by name.
That alone is incredibly rude.
Youâre now demanding that someone who was nice enough to try to help you reply as soon as possible.
That is also very rude.
I will re-iterate this as well:
Youâve also posted this question as an off-topic post in another thread I replied to. Wrong follows wrong.
We help people as a kindness. Weâre under no obligation at all to help you, and certainly donât have to provide code to you. Iâm outright upset with you, and arenât even going to bother LOOKING at your posts, let alone try to help you.
I am really sorry, But I put I your name in the tittle because I saw many of your posts related to collision and I thought you would know how to do this very well. And I was not demanding on @yanze03, I just needed his help urgently, and I also said sorry for troubling him and thanked him for helping me out so much. The other post that I deleted was an accident, and I also tried flagging it and getting it back but it didnât work. But I am really sorry for any inconvenience cause. And I will not disturb you or anyone again.
Sorry Again
That said, collision is a solved problem.
I have nothing to add that this guide doesnât already cover.
Learn how to, and then write, your own code, please.
I wish I knew about this guide, this seems Interesting it covers different shapes.
I did not want to reply again, and sorry again for bothering you but my problem is not Collison anymore it is âHow can I change the points to an ellipse with the same variables so my codes that are based on my variables donât get spoiled?â
Sorry for disturbing you again
and Sorry for all the inconvenience caused @yanze03, @quark and @TfGuy44.
At this point it is difficult to see far how you have got so please post the entire sketch code so we can try it out.
But before that please edit the title of this discussion to remove the reference to TfGuy and the âPlease helpâ which is pointless because most discussions are seeking help.
Hi @quark Sorry again for all the inconvenience caused. And thank you for helping me. Here are the codes
float ballx,bally,xspeed=5,yspeed=8;
void setup() {
fullScreen();
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 the ball hits the underside of the top rectangle
if(ballx > 810 && ballx < 1110 && bally < 175){
yspeed = yspeed*-1;
}
//if the ball hits the left side of the rectangle
if(ballx >= 810 && bally >100 && bally < 175){
xspeed = xspeed*-1;
}
//if the ball hits the right side of the rectangle
if(ballx <= 1110 && bally >100 && bally < 175){
xspeed = xspeed*-1;
}
//if the ball hits the top side of the bottem rectangle
if(ballx > mouseX && ballx < mouseX+300 && bally > 880){
yspeed = yspeed*-1;
}
//if the ballhits the side of the bottem rectangle
if(ballx >= mouseX && bally > 880 && bally < 955){
xspeed = xspeed*-1;
}
//if the ball hits the right side of the bottem rectangle
if(ballx <= mouseX+300 && bally > 880 && bally < 955){
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;
}
}
Before we begin I suggest you use Ctrl-T (Windows linux) or Cmd-T (osx) in Processing IDE to correctly indent your code. This makes it easier to see the code structure for you and me
OK I have tried the program and modified it to make use of ellipse It only involved some minor changes which I show below.
void setup() {
fullScreen();
stroke(255);
fill(255);
strokeWeight(1);
}
void draw() {
background(0);
// point(ballx, bally); //draw the ball (replaced by next line)
ellipse(ballx, bally, 100,100); // Ball diameter = 100
if (sideCollision(ballx)) {
xspeed = xspeed * -1; //reverse horizontal speed
}
The other thing that I noticed is that the ball movement is wrong because there are several times it reverses direction even if it hasnât hit anything.
This discussion is flagged as homework, is that why you are in such a hurry?
Sorry for the late reply this was accidently marked as Homework while I was changing the title, But yes I am kind of in a hurry. According to @yanze03 the ball movement that is wrong because there are several times it reverses direction even if it hasnât hit anything is because it is a point not an ellipse. And I am a little bit confused because you have still kept the stroke, fill and stroke weight even when you have changed it to a ellipse.
Ok Thank You, I have done what you had written, Put there are still some problems:
- The size of the rectangle is smaller (I think I can fix that)
- The size of the ellipse is smaller (I think I can also fix that)
- The ball movement is still wrong because there are several times it reverses direction even if it hasnât hit anything specially the 4 edges of the screen and about 100 pixels far from the still rectangle from both sides.
You can check all of these problems on your processing.
When drawing a 2D shape using ellipse
or rect
we can control what it looks like with
stroke(...)
the colour of the border
strokeWeight(...)
the thickness of the border
fill(...)
the colour of the interior.
No experienced programmer would use very thick borders to emulate 2D shapes in this situation i.e. a 2D game because it makes the border positions uncertain.
In my code you can remove the strokeWeight(1);
statement because the default stroke weight i s 1 already I left it there for the sake of completeness.
The unwanted reversal of the ball is due to errors in your collision detection code.
Thank You but what are the errors in my collision detection code. Can you help me fix them.