i need to create a ball bounce program where the ball would be of a user defined radius, it would bounce inside a rectangle of user defined length and breadth and it would start by falling from the center of the box at a certain angle. the box would be at the center of the screen. the number of bounces will also be user defined. can anyone please provide some ideas for this....would be very grateful
ball bounce using graphics.h
Page 1 of 16 Replies - 570 Views - Last Post: 12 October 2012 - 10:12 AM
Replies To: ball bounce using graphics.h
#2
Re: ball bounce using graphics.h
Posted 12 October 2012 - 12:03 AM
So it doesn't look like you're asking for all the basic code for free, how about you show us what you can achieve for yourself.
- prompting for user input
- drawing the bounding box
- drawing a ball (which doesn't move)
- drawing a ball moving in a straight line (say down the screen)
- detecting when it hits the wall.
- prompting for user input
- drawing the bounding box
- drawing a ball (which doesn't move)
- drawing a ball moving in a straight line (say down the screen)
- detecting when it hits the wall.
#3
Re: ball bounce using graphics.h
Posted 12 October 2012 - 05:44 AM
Salem_c, on 12 October 2012 - 12:03 AM, said:
So it doesn't look like you're asking for all the basic code for free, how about you show us what you can achieve for yourself.
- prompting for user input
- drawing the bounding box
- drawing a ball (which doesn't move)
- drawing a ball moving in a straight line (say down the screen)
- detecting when it hits the wall.
- prompting for user input
- drawing the bounding box
- drawing a ball (which doesn't move)
- drawing a ball moving in a straight line (say down the screen)
- detecting when it hits the wall.
i can do the first four.....its detecting when it hits the wall that i cant do
#4
Re: ball bounce using graphics.h
Posted 12 October 2012 - 06:28 AM
adityasingh95, on 12 October 2012 - 05:44 AM, said:
Salem_c, on 12 October 2012 - 12:03 AM, said:
So it doesn't look like you're asking for all the basic code for free, how about you show us what you can achieve for yourself.
- prompting for user input
- drawing the bounding box
- drawing a ball (which doesn't move)
- drawing a ball moving in a straight line (say down the screen)
- detecting when it hits the wall.
- prompting for user input
- drawing the bounding box
- drawing a ball (which doesn't move)
- drawing a ball moving in a straight line (say down the screen)
- detecting when it hits the wall.
i can do the first four.....its detecting when it hits the wall that i cant do
i can do the rest :3
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>
#define pi 3.141593
void main()
{clrscr();
int rad,length,breadth,ang;
cout<<"Enter Radius of Ball";
cin>>rad;
cout<<"Enter Length of Box";
cin>>length;
cout<<"Enter Breadth of Box";
cin>>breadth;
cout<<"Enter Angle of Fall";
cin>>ang;
float angle,xco,yco;
angle=((2*pi)/360)*ang;
int driver=DETECT,mode;
initgraph(&driver,&mode,"c:\\tc\\bgi");
for (int i=0;i<((breadth/2)-rad);i++)
{
line((320-(length/2)),(240-(breadth/2),(320+(length/2)),(240-(breadth/2)));
line((320-(length/2)),(240-(breadth/2),(320-(length/2)),(240+(breadth/2)));
line((320-(length/2)),(240+(breadth/2),(320+(length/2)),(240+(breadth/2)));
line((320+(length/2)),(240-(breadth/2),(320+(length/2)),(240+(breadth/2)));
xco=320+i*cos(angle);
yco=240+i*sin(angle);
circle(xco,yco,rad);
delay(25);
cleardevice();
}
getch();
}
#5
Re: ball bounce using graphics.h
Posted 12 October 2012 - 07:01 AM
> i can do the rest :3
Does that mean you're done then?
Does that mean you're done then?
#6
Re: ball bounce using graphics.h
Posted 12 October 2012 - 09:35 AM
#7
Re: ball bounce using graphics.h
Posted 12 October 2012 - 10:12 AM
Create 4 variables called 'left, right, top, bottom' to describe the box.
Then for moving right for example, you do
Express the velocity of the ball as dx and dy.
So for each iteration of the ball moving, it's
Finally, a bounce off a vertical wall would be
Then for moving right for example, you do
if ( xco + rad >= right ) {
// bounce
}
Express the velocity of the ball as dx and dy.
So for each iteration of the ball moving, it's
xco += dx; yco += dy;
Finally, a bounce off a vertical wall would be
dx = -dx;
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|