We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi!! I want to achieve behaviour of my ball like in flappy bird game and here what I have
float xpos = 200;
float ypos = 50;
float vy = 0;
float gravity = 0.98;
float bounce = -1;
void setup()
{
size(400,600);
smooth();
noStroke();
}
void draw()
{
background(0);
ellipse(xpos, ypos, 30, 30);
vy += gravity;
ypos += vy;
if(ypos > height - 15)
{
ypos = height - 15;
}
}
void keyPressed() {
vy *= bounce;
}
But behaviour is strange and its going down anyway. What is my mistake?
Answers
Your code's fine. It behaves just like the bird would. What's the problem?
@TfCuy Try to do it longer and you will see that it's impossible to move ellipse higher than it was
Ah! Ok. When you "bounce" your velocity suddenly changes direction. When you "flap", you actually gain some upward force - some to stop your decent, and some to make you go up. Instead of simply reversing the direction of the velocity when a key is pressed, try setting it to a fixed constant (-12 seems good).
@TfGuy44 that helped me! thank you very much!