We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I really don't understand what i am doing wrong here or where i am going wrong conceptually. I am extremely new to processing and have a hard time thinking in terms of logic; but this is precisely what i'm trying to overcome at the moment.
I am only trying to make this ball bounce back. this is the code i wrote but it's only moving the ball once diagonnally across the screen. it does not bounce back. can someone please help :
int a = 0;
int b = 400;
int z = 5;
void setup () {
size (400,400);
}
void draw () {
background (0);
fill (255,0,255);
ellipse (a +z,b-z, 50, 50);
a = a+5;
b = b-5;
if (a>400 || b<0 ) {
z = -z;
}
}
i also want to explain what i understand in this logic - please correct me where i am wrong:
a is a number that starts from 0
b is a number that starts from 400
z is 5.
there is an ellipse of 100 diameter that moves from coordinates (0,400) to (400,0) - right across the graph. The ellipse moves in z increments. so we may say:
the new coordinate for the ellipse moving forward is (a+z),(b-z) ?
(assume a = a+5 (as it moves, old value of a replaced by new value of a)
and b = b-5)
In that case, if i want the ellipse to move in the opposite direction after values become a = 400, b = 0; does it not mean z = -z ie same values, negative direction?
Answers
Your code is kind of a mess. Follow along!
A blank sketch. Everything here works. No problems.
An ellipse appears! Where is the ellipse? Why, it's at (100,100), of course.
Now it's the right color too.
Now the dot's position depends on some other variables. Since those variable are where the dot is going to be, I've given them names that indicate what the value those variables are storing is used for. If I had called these variables a and b, I might get confused later.
Now I am increasing the value stored in those position variables. This makes the ellipse move.
Now the ellipse moves as it did before, but the amount it moves by is controlled by another variable, step_amount. This is where you went wrong before. You would always add 5 to your a and b variables, no matter what the value of z was. You may have had trouble noticing this because z and 5 kind of look the same. With properly named variables, there is less confusion.
This step is optional, but it makes some sense to move the ellipse's behavior to separate functions. I do this now to further illustrate that this makes it more clear what the sketch is doing.
Here's even a bit more optional shuffling. Give yourself a gold star if you can see where I'm going with this.
Hey look, it bounces!
Now it bounces on all the edges! Yeah! ... What?
It DOESN'T?!?
OH NO! NO NO NO! WE BROKE IT!!!
ACK! PANIC!
WHAT'S WRONG???
thank you so much for such a wonderfully illustrated response.
Can you tell me if i got this:
My mistake was i put z as a solid number -- 5 whereas i should be telling the computer "No matter what the value of z is, when a = 400 and be = 0 --z should be in the negative direction" So the computer does not accept my code because it is not explaining the function sufficiently?
Thankfully we know that the problem is because of something that was changed in the check_for_bounce() function. Nothing else was changed! See if you can work out what went wrong. Hint: What happens if both position_x and position_y are greater than 400?
Anyway, using one step_amount variable isn't going to cut it.
That's better. Now, last chance to earn that gold star...
It's the same sketch! So what good is this? Well, for one, the draw() and setup() functions are WAY simple looking.
oh man you've used commands i haven't studied yet. give me a moment while i figure this out :)
Also this. Wheeeeeeeeeeeeeeeeeee!
hey was my main problem my syntax? i got the order wrong or something and confused my variables? I seemed to have understood what's happening correctly. Sorry if i'm asking dummy questions. Hard to move forward without total clarity.
You were changing your step amount (which you called z) just fine, but you were using the value of 5, not z, when you updated your position variables.
i got it right now :D
int locationX = 0; int locationY = 400; int increment = 5;
void setup () { size (400,400);
}
void draw (){
background (0); ellipse (locationX + increment,locationY - increment,50,50);
locationX=locationX+increment; locationY=locationY-increment;
if (locationX >400 && locationY<0){
increment = -increment; }
if (locationX<0 && locationY>400) {increment = -increment;}
}
MAKES SO MUCH MORE SENSE NOW THANK YOU!!!
just one m ore dumb question - theoretically, if the 5 was replaced by the z - it wouldn't work right? i did try this and it didn't. i put z = 5 to substitute 5 in all fields - did i confuse myself by doing that?
ok no nevermind! it does work! jeeeeez! man numbers confuse me. thanks again!