We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I have been following the Processing tutorials and trying to make a simple bouncing ball sketch combined with an ease-in sketch (without using the mouseX and MouseY) to make a bouncing ball with ease-in motion when the ball approaches to both edges of heights but failed. The closest I got was a ball moves to one direction with ease but couldn't make it move back. I think I must miss some fundamental concept of this. Can anyone point me a direction? Thanks.
Bouncing ball sketch (from processing tutorial)
float x;
float y;
float speed = 5;
void setup() {
size(640, 360);
noStroke();
}
void draw() {
background(51);
y = y + speed;
if ((y>=height)||(y<=0)) {
speed =speed*-1;
}
ellipse(width/2, y, 66, 66);
}
Ease in sketch (from processing tutorial)
float x;
float y;
float easing = 0.05;
void setup() {
size(640, 360);
noStroke();
}
void draw() {
background(51);
float targetX = mouseX;
float dx = targetX - x;
if(abs(dx) > 1) {
x += dx * easing;
}
float targetY = mouseY;
float dy = targetY - y;
if(abs(dy) > 1) {
y += dy * easing;
}
ellipse(x, y, 66, 66);
}
Answers
My code
When do you detect when the ball reaches the top or bottom of the frame?
Thank you for the reply. I use checkEdge function like Bouncing Ball sketch above by -1. However, the behavior is still not right. 1. I make the easing-1, the ball does bounce back, but it doesn't ease in when it moves back to top. (I feel something is wrong with using easing*-1...) 2. When the ball is at the bottom and accelerating back to the top, it goes from slow to fast, it should be "fast and then slowly ease in.
I cleaned up the code a little and here is the updated one, thanks
Well, take a look at this line:
You're always basing your velocity on the distance from the bottom of the frame: so the higher up the ball is, the faster it will go.
If you want to slow the ball down when it's closer to the bottom or the top, then you'll have to change your logic. How you do this is up to you, but you might use the distance from the middle instead, or keep track of which way the ball is going and change your velocity accordingly.
here is another approach
we have a target (bouncing line) and the ball that follows it. This following is with easing.
This is bit more like in the example where we have also a target (mouse) and a ellipse / ball position.
Best, Chrisir ;-)
with 2 dimensions it flies curves
or in 3 dimensions
Thank you KevinWorkman. Ok, keep track of which way the ball is going and change the velocity that I understand but I guess I didn't do it right . And thank you Chrisir, a moving target! YES! I think this is where my logic got stuck! Let me revise it and clear my logic again see if I fully understand this! And the plusOrMinus function which returns a positive or a negative value, learn something new today, great Friday for me! Thank you guys!
Hi, I studied for several days and figured what I wanted was more like the type of motion with a sudden acceleration (like someone pushes it while it's not moving). So, I revised my code into the following. I searched the tutorial and got this millis() code which I can give the ball a push every 6 seconds. And I set the target y to 0 and height every 6 seconds so the ball will turn the direction.