 |
06-15-2003, 05:44 AM
|
#1 (permalink)
| | Registered User
Join Date: Oct 2002 Location: Scotland, UK
Posts: 2,946
| » 
Bouncing ball
I am trying to make a program that will display a ball bouncing up and down with gravity also having an effect. It doesn't work right. Could one of you try it and see if you can improve it at all. You will need a 64x64 BMP file of something to bounce. Code: /* These header files are needed */
#include "SDL/SDL.h"
#include "stdio.h"
#include "stdlib.h"
int main()
{
SDL_Surface *screen;
SDL_Surface *image;
SDL_Rect rect, dest;
int x,y, yvel, xvel, gravity, count, gcount;
/* Initialise the video subsytem */
/* If it returns a non-null value, die */
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
printf("Unable to initialise SDL: %s\n",SDL_GetError());
return 1;
}
/* Make sure the program exits cleanly */
atexit(SDL_Quit);
/* Set 800x600 windowed mode */
screen = SDL_SetVideoMode(800,600,16,0);
if (screen==NULL) {
printf("Unable to set video mode: %s\n",SDL_GetError());
return 1;}
x = 5;
y = 5;
yvel = -2;
xvel = 2;
gravity = 2;
rect.x = 0;
rect.y = 0;
rect.w = 64;
rect.h = 64;
dest.x = 0;
dest.y = 0;
dest.w = 64;
dest.h = 64;
image = SDL_LoadBMP("ball.bmp");
for(gcount=0;gcount<200;gcount++) {
for(count=0;count<5;count++) {
if (x+xvel > 576) {
xvel = 0-(xvel/2);
x=576;
printf("bounce"); }
if (y+yvel > 416) {
yvel = 0-(yvel/2);
y=416;
printf("bounce");}
if (x+xvel <0) {
xvel = 0-xvel;
x=0;
printf("bounce");}
if (y+yvel < 0) {
yvel = 0;
y=0;
printf("bounce"); }
x+=xvel;
y+=yvel;
dest.x=(int)x;
dest.y=(int)y;
SDL_BlitSurface(image,&rect,screen,&dest);
SDL_UpdateRect(screen,0,0,0,0);
}
yvel+=gravity;
}
SDL_Delay(3000);
/***************************************/
/** SDL Graphics init complete **/
/***************************************/
printf("Graphics subsystem initialised\n");
printf("Quitting...\n");
return 0; }
This is my first attempt at programming anything in C for like 2 years, so go easy on me.  It uses SDL, as you can see and I built it ok using G++ on my Linux system.
__________________
_____
NuKeS
|
| |
06-15-2003, 10:46 AM
|
#2 (permalink)
| | Registered User
Join Date: Oct 2002 Location: Scotland, UK
Posts: 2,946
|
Ok, I've got that sorted a bit, I've still got some work to do, but I'll worry about that later.
Now I'm trying to get the ball to accelerate towards the mouse when the button is pressed. Any ideas? I had it in my head but can't remember how I was planing to do it. Here's what I've got now: Code: /* These header files are needed */
#include "SDL/SDL.h"
#include "stdio.h"
#include "stdlib.h"
int main()
{
SDL_Surface *screen;
SDL_Surface *image;
SDL_Surface *mouse;
SDL_Rect rect, dest;
int x,y, yvel, xvel, gravity, count, gcount, mousex, mousey, tangent;
Uint8 buttons;
/* Initialise the video subsytem */
/* If it returns a non-null value, die */
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
printf("Unable to initialise SDL: %s\n",SDL_GetError());
return 1;
}
/* Make sure the program exits cleanly */
atexit(SDL_Quit);
/* Set 800x600 windowed mode */
screen = SDL_SetVideoMode(800,600,16,0);
if (screen==NULL) {
printf("Unable to set video mode: %s\n",SDL_GetError());
return 1;}
x = 5;
y = 5;
yvel = 0;
xvel = 5;
gravity = 3;
rect.x = 0;
rect.y = 0;
rect.w = 78;
rect.h = 78;
dest.x = 0;
dest.y = 0;
dest.w = 64;
dest.h = 64;
image = SDL_LoadBMP("ball.bmp");
mouse = SDL_LoadBMP("pointer.bmp");
for(gcount=0;gcount<200;gcount++) {
for(count=0;count<5;count++) {
if (x+xvel > (800-78)) {
xvel = 3-(xvel);
x=(800-78);
printf("bounce"); }
if (y+yvel > (600-78)) {
yvel = 0-(yvel-3);
y=(600-78);
printf("bounce");}
if (x+xvel <0) {
xvel = 3-xvel;
x=0;
printf("bounce");}
if (y+yvel < 0) {
yvel = 0;
y=0;
printf("bounce"); }
x+=xvel;
y+=yvel;
dest.x=(int)x;
dest.y=(int)y;
SDL_PumpEvents();
buttons = SDL_GetMouseState(&mousex,&mousey);
// I've forgotten how I was going to do this.
//if(buttons && SDL_BUTTONS(1) != NULL) {
// tangent = (mousey-y)/(mousex-x);
//
// }
rect.w = 78;
rect.h = 78;
SDL_BlitSurface(image,&rect,screen,&dest);
dest.x=mousex;
dest.y=mousey;
rect.w=50;
rect.h=50;
SDL_BlitSurface(mouse,&rect,screen,&dest);
SDL_UpdateRect(screen,0,0,0,0);
}
yvel+=gravity;
}
SDL_Delay(3000);
/***************************************/
/** SDL Graphics init complete **/
/***************************************/
printf("Graphics subsystem initialised\n");
printf("Quitting...\n");
return 0; }
EDIT::
====
Doen't matter, I got it sorted, using dy=v*sin(theta) and dx=v*sin(theta) for working out the acceleration, where V is the standard acceleration I want, and theta is the angle between the position of the mouse and that of the ball from the horizontal.
__________________
_____
NuKeS
Last edited by nukes; 06-18-2003 at 12:19 PM.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions  | | | | | Recent Discussions  | | | | | |