6 Replies - 5535 Views - Last Post: 12 October 2012 - 10:12 AM Rate Topic: -----

#1 adityasingh95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-July 12

ball bounce using graphics.h

Posted 11 October 2012 - 11:10 PM

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
Is This A Good Question/Topic? 0
  • +

Replies To: ball bounce using graphics.h

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1689
  • View blog
  • Posts: 3,209
  • Joined: 30-May 10

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.
Was This Post Helpful? 0
  • +
  • -

#3 adityasingh95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-July 12

Re: ball bounce using graphics.h

Posted 12 October 2012 - 05:44 AM

View PostSalem_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.



i can do the first four.....its detecting when it hits the wall that i cant do :(
Was This Post Helpful? 0
  • +
  • -

#4 adityasingh95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-July 12

Re: ball bounce using graphics.h

Posted 12 October 2012 - 06:28 AM

View Postadityasingh95, on 12 October 2012 - 05:44 AM, said:

View PostSalem_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.



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();
}


Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1689
  • View blog
  • Posts: 3,209
  • Joined: 30-May 10

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?
Was This Post Helpful? 0
  • +
  • -

#6 adityasingh95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-July 12

Re: ball bounce using graphics.h

Posted 12 October 2012 - 09:35 AM

View PostSalem_c, on 12 October 2012 - 07:01 AM, said:

> i can do the rest :3
Does that mean you're done then?


no i can just move the ball across the screen....i cant get any logic to create the bouncing motion....
Was This Post Helpful? 0
  • +
  • -

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1689
  • View blog
  • Posts: 3,209
  • Joined: 30-May 10

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
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;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1