Sign in to follow this  
fitfool

Collsion Detection

Recommended Posts

Hello, ive been working on a game for the past couple of days and am having trouble with some collsion detection. the collision detection works, but only on the left half of the screen. Im not sure why but it only works on the left side Here's my code...
#include <allegro.h>
#include <sstream>
#include <string>

void init();
void deinit();
int game();
void ai();
int ai_x = 0;
void collision();

      BITMAP *bmp;
      PALETTE palette;
      BITMAP *spaceship;
      BITMAP *missile;
      BITMAP *double_buffer;
      int x = 80;
      int y = 80;
      int otherx;
      int othery;
      bool draw = false;
      bool stop = false;
      bool blitenemy = true;
      int enemyx;
      int enemyy;
      BITMAP *enemy;
      BITMAP *explosion;
      BITMAP *ptr;
      int update = 0;

int main() {
	  init();
      game();
      
      
	  return 0;
}
END_OF_MAIN()



void init() 
{
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
	/* add other initializations here */
}

void deinit() 
{
	clear_keybuf();
	/* add other deinitializations here */
}

int game()
{
      
      double_buffer = create_bitmap(640, 480);
      explosion = load_bitmap("explosion.bmp", palette);
      if (!explosion)
      {
               return -1;
      }
      enemy = load_bitmap("enemy.bmp", palette);
      if (!enemy)
      {
               return -1;
      }
      
      missile = load_bitmap("missile.bmp", palette);
      if (!missile)
      {
               return -1;
      }
      spaceship = load_bitmap("helicopter.bmp", palette);
      if (!spaceship)
      {
               return -1;
      }
      bmp = load_bitmap("background.bmp", palette);
      if (!bmp)
      {
               return -1;
      }
      

      while (!key[KEY_ESC]) 
      {
	  blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);
	  draw_sprite(screen, spaceship, x, y);
      if (blitenemy == true)
      {
      draw_sprite(screen, enemy, ai_x, 380);
      }
      
      if (blitenemy == false)
      {
      draw_sprite(screen, explosion, ai_x, 380);
      }  
      
      if (stop == false)
      {
      otherx = x;
      othery = y;
      }    
      
      if (draw == true)
      {
      if(update == 0)
      {
                update++;
                otherx = x;
      }
      draw_sprite(screen, missile, otherx + 80, othery + 100);
      othery++;
      }
      
      if (othery >= 800)
      {
      stop = true;
      draw = false;
      otherx = x;
      othery = y;
      update = 0;
      }
      
      collision();
      ai();
      //Dont mind this
      /*if()
      {
      
      if(blitenemy == false) 
      {
      blitenemy = true;
      }
      
      else 
      {
      blitenemy = false;
      }
      }
      */
      if (key[KEY_D])
      {
      x = x + 1;
      rest(.9);
      }
      
      if (key[KEY_A])
      {
      x = x - 1;
      rest(.9);
      }
      
      if (key[KEY_SPACE])
      {
      stop = true;
      draw = true;
      }
      
      }
     
    destroy_bitmap(bmp);
    deinit();
}

void ai()
{
     static bool go = false;
     if(go == true)    
     {
             ai_x--;
             if(ai_x < 0)
             {
                     go = false;
             }
     }
     
     if(go == false)   
     {
             ai_x++; 
             if(ai_x > 480)  
             {
                     go = true;
             }  
     }
}

void collision()
{
     
      if(otherx < (enemy->w / 2) + 80 && otherx > (enemy->w / 2) - 80
                  && othery >=200)
      {
                  blitenemy = true;
                  
      }
      
      else 
      {
      blitenemy = false;
      
      }
}

                  
     
     
[Edited by - fitfool on November 23, 2006 5:26:33 PM]

Share this post


Link to post
Share on other sites
Is there any function you need help with in particular?

I looked at you collision function but it seems a little too complex. The missle should be able to predict its next coordinate and check if it it is clear, if not it should act accordingly (ie BOOM!).

Share this post


Link to post
Share on other sites
I'd say, give each object a rectangle (x, y, width, height) and do the following checks:

A.x + A.width < B.x
A.x > B.x + B.width
A.y + A.height < B.y
A.y > B.y + B.height

As soon as one is true, you can safely assume A and B do not collide. If, however, none of these statements are true, the rectangles surely collide.

I can make little of your code bytheway. You should use code or source tags for large bodies of code to keep the post short, plus code gets highlighted. You should also try to post only relevant code, in this case that would be your collision function.
Oh, and I'd strongly recommend using comments. I have no idea what 'otherx' stands for. Assuming it's the players x value however, it looks like you forgot to take the enemies position into account.

Just curious bytheway, are you planning to add multiple enemies to the game?

Share this post


Link to post
Share on other sites
I am new to programming games, but I love math and physics and have done my own hit collision, though it is a little weird, but in my game it suprisngly works.
find out the height and width of your image then create 2 loops one for the width and the other for your height and then do the distance formula of the two objects of each width and height loop within the collision it should work.

For instance a quick example and i hope it's ok. im using java lingo since i don't know the math library from C++ as well as I do the java one.

//the ships dimensions, and positions.
ship_x;
ship_y;
ship_width;
ship_height;

//monster dimensions, and positions
mon_x;
mon_y;
mon_width;
mon_height;

//the distnace formula with a loop to calculuate it.

for(int i=0; i<ship_width; i++)
{
dist = Math.sqrt(Math.pow((ship_y - mon_y),2) + Math.pow(ship_x+i - mon_x),2));

//if dist is this less than this then do something
if(dist <= 3)
{
do something
}
}

this should work somewhat for it checks if any of the ships width hits within the monster, then you just create 3 more one for the ships height then the monsters width and height. I have done with this with my space game and it works. A bit length, complex in mathematics unless your in high level math class then you don't mind, and does run CPU up a little I think, but I think it worth it if it's a small 2d game. Im going to try to use this on a 2d game that uses alot of game objects and see if it still runs smoothly. Hope I have helped a little, and good luck. Until then...

Take Care!

c.s. finch

Share this post


Link to post
Share on other sites
To the above poster: that's a pretty darn inefficient way to do it. What you're doing is computing a bounding circle collision for every single point on your object, within a certain resolution. If I measured my game objects in pixels (in a 2D game, for example) doing your collision detection between the 2 objects which are 100x100 pixels would require 100 million circle-circle collision detections.

Since your objects are boxes, you could reduce all of that to nothing but a few if statements by doing bounding box collision, as Captain P described.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this