Click here to Skip to main content
11,797,355 members (76,984 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ C
Hi everyone. We have a project which is given by our lecturer and we don't move on this project. Firstly our game :
Our primary programming language is C. We codified ball function but there is a some mistakes. We don't fix it and carry out the idea to cutting function. We use alleg.lib and work on Visual C++ 2010
Express.We don't have an idea about ball's moving after cutting the image. Balls move as - no vertical no horizontal - cross. They move on different places
and different way. They pass through each other and they are flicker.Double buffering doesn't work that there is two ball on the screen. Can you help us?

#include <allegro.h>
void Baslat();
void Bitir();
void moveBall()
int main() {
while (!key[KEY_ESC]) {
int width = 220;//width of box
int height = 440;//height of box
int radius = 5;//radius of ball

int x = 110;//initial position of ball
int y = 220;//initial position of ball

int tempX;
int tempY;
//Keep track of direction of motion here.
//0= northwest 1 = southwest, 2 = northeast,
//3 = southeast
int dir;
return 0;
void Baslat() {
int depth, res;
depth = desktop_color_depth();
if (depth == 0) depth = 32;
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
if (res != 0) {
void Bitir() {
void moveBall(){
  //Save current location of ball.
  tempX = x;
  tempY = y;
    case 0:
      //Direction is northwest.
      if((x <= radius) || (y <= radius)){
        //Ball has collided with either the left wall or
        // the top wall.
        //Get a new direction. Note that if the new
        // direction is the same as the old one, control
        // will come back to here to get still another
        // direction the next time the functionis called.
        dir = rand() % 4;
        //No collision, set new location for the ball
      }//end else
    case 1:
      //Direction is southwest.
      if(((x <= radius) || (y >= (height - radius)))){
        //get a new direction
        dir = rand() % 4;
        //set new location for the ball
      }//end else
    case 2:
      //Direction is northeast.
      if(((x >= (width - radius)) || (y <= radius))){
        //get a new direction
        dir = rand() % 4;
        //set new location for the ball
      }//end else
    case 3:
      //Direction is southeast
      if((((x >= (width - radius)) ||
                              (y >= (height - radius))))){
        //get a new direction
        dir = rand() % 4;
        //set new location for the ball
      }//end else

  }//end switch 

This is not working exactly. We have some problems that cutting shape and bouncing ball. We make bouncing ball but the ball is flicker. Double buffering is not work on it. Our another problem is that two balls ,which are in the shape, through pass each other. How can we fix that?
So our another problem - i think it is a big problem - that we don't cut the shape. What can we do to make it?

We don't have an idea about slicing the shape. Can you help us? And you send the function better than that
We hurry up because we should finish this project in 6 days.

Note : We are using Allegro v4.2.3.

Is there someone who help us with a simple codes?

[edit]Code block added - OriginalGriff[/edit]
Posted 21-Apr-13 4:10am
Edited 21-Apr-13 4:40am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

For collision between 2 spheres, here's the first link[^] I got from a google search for '2D ball Physics'.

The collision checks you have for the ball with the container checks only for a rectangular container. You should implement the container as a polygon. Maybe a structure which contains a list of lines which make up the edges of the polygon and keeps tab of the winding order[^].

Once you have this set up, you can check for collision with the edges using basic 2D circle - line collision[^].

Finally, the code to handle the collision, takes a random direction if the ball collides with an edge. This seems incorrect. The resultant motion vector should be computed from the circle-line collision algorithm. A couple more links from google are here[^] and here[^].

I hope this helps.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

There is an interesting tutorial for a simple physics engine[^] that includes moving, colliding balls. Check it out, it comes with great explanations and code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 475
1 OriginalGriff 375
2 Maciej Los 370
3 Abhinav S 359
4 KrunalRohit 291
0 OriginalGriff 2,092
1 Maciej Los 1,760
2 KrunalRohit 1,436
3 CPallini 1,245
4 ppolymorphe 893

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100