home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Bouncing ball

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1540
Discussions: 200,985, Posts: 2,379,852, Members: 246,348
Old June 15th, 2003, 08:44 AM   Digg it!   #1 (permalink)
Ultimate Member
 
nukes's Avatar
 
Join Date: Oct 2002
Location: Scotland, UK
Posts: 3,221
Send a message via AIM to nukes Send a message via Yahoo to nukes
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
nukes is offline   Reply With Quote
Old June 15th, 2003, 01:46 PM     #2 (permalink)
Ultimate Member
 
nukes's Avatar
 
Join Date: Oct 2002
Location: Scotland, UK
Posts: 3,221
Send a message via AIM to nukes Send a message via Yahoo to nukes
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.

Last edited by nukes : June 18th, 2003 at 03:19 PM.
nukes is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3063)
Charges against non-tippers dropped.. (20)
Health Care Rationing (11)
Delete an OS (16)
Nvidia GTX 260 problem (9)
Laptop with wireless problem. (12)
windows vista security holes (19)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (7)
windows 7 problem (7)
Internet Lost (5)
[F@H SPAM 11/16/09] ! 1/2 months to.. (39)
Recent Discussions
[F@H SPAM 11/16/09] ! 1/2 months to r.. (39)
Share an Easy Way to Convert DVD and .. (0)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
Desktop Calendar Application (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
Nvidia GTX 260 problem (9)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
Point and Shoot Camera Suggestions. (7)
Delete an OS (16)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)
Linksys WMP54GS wireless card problem.. (5)
Help getting around port 80 for camer.. (5)
Skillsoft Network+ Study Software Que.. (10)


All times are GMT -4. The time now is 03:22 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28