How to stop bouncing the ball

Started by
4 comments, last by DeathEvader 10 years, 8 months ago

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();
	al_init_primitives_addon();

	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..

Advertisement

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 :)

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.

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.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

@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.

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 smile.png

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

This topic is closed to new replies.

Advertisement