Mail Archives: djgpp/1997/02/02/01:47:27
I do not know if this is what you want, I just came up with this right
now, it uses some physics to get the velocity, It is used with allegro,
but should not be hard to change to something different, let me know if
this is what you wanted:
#include <allegro.h>
#include <stdio.h>
main()
{
allegro_init(); //initialize allegro
install_keyboard(); //install keyboard handler
install_timer(); //install timer
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); //set graphics
mode to 640x480x256
float g= -9.81; //acceleration of gravity
float position; //current position of the ball
float time = 0; //time
float v_init = 90; //initial velocity
char text[80]; //temp string
BITMAP *ball = create_bitmap(20, 20); //graphics of the ball
BITMAP *buffer = create_bitmap(50, 480); //use double buffer
clear(ball);
clear(buffer);
circle(ball, 10, 10, 9, 15);
text_mode(0);
while (1==1) {
clear(buffer);
if (position < 0) { //make the ball bounce in loops
time = 0;
v_init-=5; //decrease the initial velocity to accomodate air
resistance
if (v_init < 0) v_init=0; //make it stop
}
// formula for position is 1/2 * g * time^2 + v_initial*time
position = (0.5)*g*time*time+v_init*time; //calculate position
draw_sprite(buffer, ball, 25, 460-(int)position);
sprintf(text, "%d", (int)position);
textout(buffer, font, text, 1, 1, 15); //print current position
if (key[KEY_ESC]) break; //make it exit when ESC pressed
blit(buffer, screen, 0, 0, 100, 0, 50, 480);
rest(5); //rest some time so you can see the ball
time+=0.1; //change time
}
}
On Sun, 2 Feb 1997, Pyro Technic wrote:
> How would I get a ball to bounce realisticly. The constant velocity and
> four directions I have noe just just. Do I need an algorithim,
> logirithm, or some other thing a newbie like myself has never heard of.
> Thanks a lot.
>
> Pyro
>
- Raw text -