Public Group

# How to stop bouncing the ball

This topic is 2161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, I am pretty new to allegro5 and made a simple program of a bouncing ball using the "Physics for Lazy Game Developers" (labs.skookum.com/demos/barcampclt_physics/newton/4) as a guide and well the ball is bouncing well enough but it doesn't stop bouncing.Here is my program:

#include<allegro5\allegro.h>
#include<allegro5\allegro_primitives.h>
#include <stdlib.h>

int main(void){
const int FPS = 60;
int w = 640;
int h = 480;
float x = w/2;
float y = 2;
float a = 40;
float velx = 0;
float vely = 0;
int elastic = 1;
int temp
al_init();

ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;

al_create_display(w,h);

event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / FPS);

al_register_event_source(event_queue,al_get_timer_event_source(timer));

al_start_timer(timer);
bool done = false;
while(!done){

ALLEGRO_EVENT ev;
al_wait_for_event(event_queue,&ev);

al_draw_circle(x,y,15,al_map_rgb(0,255,0),2);
//al_draw_line(0,480,640,480,al_map_rgb(0,255,0),5);
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));

if(y+15>h){
y = h - 15;
temp = vely;
vely = -abs(temp) * elastic;
//vely = 0;
}

x += velx;
y += vely;

velx *= .99;
vely *= .99;
vely += .25;
}
al_destroy_display(display);
}


I know that i didn't put any code that will stop it bouncing and the site used this:  ball.vy *= ball.elasticy; .. but i can't understand what this "elasticy" of the ball is and how do i use it. Is there any other way of stopping the ball? i tried doing different stuff but couldn't find the solution.
Any help would be appreciated :)

P.S sorry about the messy source code, i made this program in a hurry :p oh and also, i am new to this site and so sorry if i am in the wrong section..

##### Share on other sites

So, what does the ball do? Trying to follow the code, I'm kinda surprised if it does anything, because vely is set to 0 and then multiplied with, which keeps it at 0. But I'm looking at it on my phone ;)

It looks like you're trying to dampen the bounce. So, if it is bouncing, and the bounce is getting smaller, but never stopping, then essentially what you need to do is to figure out the height of the next bounce, and if it's below some threshold just set vely to 0.

And if that's not your problem then you'll probably need the help of someone who isn't browsing the forum with a phone :)

##### Share on other sites

x and y = the ball's current positions

vx and vy = the ball's velocity which is used to move the ball every game logic tick/update.

So if vx and vy continually move the x and y, unless vx and vy reach zero, they will continue to move the ball.

In the code example the site uses, 'elasticy' is used to cut in half 'vx' and 'vy' every tick. Since it is continually being cut in half, it'll eventually reach zero or really close to zero.

Because elasticy is 0.5, and it is multiplied against vx and vy, if vy is 50, it'll be cut to 25, then 12, then 6, then 3, then 1, then 0. This gradually slows down and then stops the ball.

Edited by Servant of the Lord

##### Share on other sites

There are some problems with your code.. first:

int elastic = 1;

Should not be an int, but a float. This will express the amount of momentum absorbed by the ball elasticity every time it hits the ground. infact, it is only used when the ball is below a certain level.

By setting it at 1 you are saying the bounce is not "eating" any momentum off the ball, to have the ball stop bouncing set elastic at some number < 1 and see what happens.

Also be careful with abs.. I am not sure, but you might want to use fabs because you are dealing with a float.

##### Share on other sites

@Kunos Oh right i can't believe i missed that xD, yeah its working now, the ball bounces and stops very realiticly, and @Servant of the lords thanks about the explanation, I couldn't understand that part of the program before.

##### Share on other sites

So, what does the ball do? Trying to follow the code, I'm kinda surprised if it does anything, because vely is set to 0 and then multiplied with, which keeps it at 0. But I'm looking at it on my phone ;)

It looks like you're trying to dampen the bounce. So, if it is bouncing, and the bounce is getting smaller, but never stopping, then essentially what you need to do is to figure out the height of the next bounce, and if it's below some threshold just set vely to 0.

And if that's not your problem then you'll probably need the help of someone who isn't browsing the forum with a phone

Well the ball does bounce, but that was not my problem :p but its fixed now so i doesn't matter.

• 21
• 12
• 9
• 17
• 13