|
How to stop bouncing the ball |
Shehryar Malik
Member #15,265
August 2013
|
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) and well the ball is bouncing well enough but it doesn't stop bouncing.Here is my program: 1#include<allegro5\allegro.h>
2#include<allegro5\allegro_primitives.h>
3#include <stdlib.h>
4
5int main(void){
6 const int FPS = 60;
7 int w = 640;
8 int h = 480;
9 float x = w/2;
10 float y = 2;
11 float a = 40;
12 float velx = 0;
13 float vely = 0;
14 int elastic = 1;
15 int temp
16 al_init();
17 al_init_primitives_addon();
18
19 ALLEGRO_DISPLAY *display = NULL;
20 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
21 ALLEGRO_TIMER *timer = NULL;
22
23 al_create_display(w,h);
24
25 event_queue = al_create_event_queue();
26 timer = al_create_timer(1.0 / FPS);
27
28 al_register_event_source(event_queue,al_get_timer_event_source(timer));
29
30 al_start_timer(timer);
31 bool done = false;
32 while(!done){
33
34 ALLEGRO_EVENT ev;
35 al_wait_for_event(event_queue,&ev);
36
37 al_draw_circle(x,y,15,al_map_rgb(0,255,0),2);
38 //al_draw_line(0,480,640,480,al_map_rgb(0,255,0),5);
39 al_flip_display();
40 al_clear_to_color(al_map_rgb(0,0,0));
41
42 if(y+15>h){
43 y = h - 15;
44 temp = vely;
45 vely = -abs(temp) * elastic;
46 //vely = 0;
47 }
48
49 x += velx;
50 y += vely;
51
52velx *= .99;
53vely *= .99;
54vely += .25;
55if(abs(temp) <= 1){
56elastic = 0;
57}
58 }
59 al_destroy_display(display);
60}
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. P.S sorry about the messy source code, i made this program in a hurry :p |
AmnesiA
Member #15,195
June 2013
|
It would make it a lot easier to read if you'd indent properly and comment what some of your variables do. Like was is temp for? Also, and this may be only my personal feelings, but if you're asking people to take time out of their day to help you then you should make it as easy for them as possible which means taking the time to a.) try to solve the problem yourself and b.) make the code as easy to read as possible. That being said, elasticity is a variable they have given to the ball to represent how bouncy the object is. For example, they use an elasticity of .5 so every time the ball hits the ground it loses 50% of its energy (in real-life physics this would mean that 50% of its energy is transferred to the ground). This means that elasticity has to be a floating point number and cannot be an integer; the way you are using it it is useless. Going over your code again I can't figure out what the temp variable is used for still, but here's what you want: 1 #include<allegro5\allegro.h>
2 #include<allegro5\allegro_primitives.h>
3 #include <stdlib.h>
4 #include<stdio.h>
5
6 int main(void){
7
8 const int FPS = 60;
9 int w = 640;
10 int h = 480;
11 struct ball{ //KEEPS TRACK OF BALL VARIABLES
12 float x;
13 float y;
14 float velx;
15 float vely;
16 float elastic;
17 int radius;
18 };
19 ball myBall; //Make a ball
20 myBall.x = w/2;
21 myBall.y = 2;
22 myBall.velx = 0;
23 myBall.vely = 0;
24 myBall.elastic = .5;
25 myBall.radius = 15;
26
27 al_init();
28 al_init_primitives_addon();
29
30 ALLEGRO_DISPLAY *display = NULL;
31 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
32 ALLEGRO_TIMER *timer = NULL;
33
34 al_create_display(w,h);
35
36 event_queue = al_create_event_queue();
37 timer = al_create_timer(1.0 / FPS);
38
39 al_register_event_source(event_queue,al_get_timer_event_source(timer));
40
41 al_start_timer(timer);
42 bool done = false;
43 while(!done){
44
45 ALLEGRO_EVENT ev;
46 al_wait_for_event(event_queue,&ev);
47
48 myBall.x += myBall.velx; //The ball moves as far as its velocity
49 myBall.y += myBall.vely;
50 myBall.velx *= .99; //The ball's velocity decreases (friction)
51 myBall.vely *= .99;
52 myBall.vely += .25; //Ball's velocity downward increases (gravity)
53
54 if(myBall.y + myBall.radius > h && myBall.vely > 0){ //Ball has hit the floor and is moving downward
55 myBall.y = h - myBall.radius; //Set the ball to just on top of the floor
56 if( myBall.vely >= 1.4)
57 myBall.vely = -myBall.vely; //Bounce
58 else{
59 myBall.vely = 0; //Stop the ball if it's going too slow
60 }
61 myBall.vely *= myBall.elastic; //Transfer some energy into the ground
62 }
63
64 al_clear_to_color(al_map_rgb(0,0,0));
65 al_draw_circle( myBall.x,myBall.y,myBall.radius,al_map_rgb(0,255,0),2);
66 al_flip_display();
67 }
68 al_destroy_display(display);
69 }
======================= |
Shehryar Malik
Member #15,265
August 2013
|
oh so that's what easticity is for! yeah i am sorry about the messed up code, i wrote this in a hurry, i will make sure to code more clearer next time , thanks its working great now, i couldn't understand what easticity was. P.S thanks about the new code, its working great and will help me a lot :p |
Arthur Kalliokoski
Second in Command
February 2005
|
Shehryar Malik said: since vely is a float and -abs()function only work with ints and longs They all watch too much MSNBC... they get ideas. |
Shehryar Malik
Member #15,265
August 2013
|
oh, didn't know about that, thanks alot! |
AmnesiA
Member #15,195
June 2013
|
If you wanted to use the int value you can also force it to cast as an int by writing abs( (int) vely);. In this type of situation though you definitely want to use the fabs that arthur mentioned (I also didn't know this existed so that's good news). ======================= |
Shehryar Malik
Member #15,265
August 2013
|
Oh great i didn't even know about the force cast, Thanks alot guys, these info helped me alot |
|